Hello everybody. I am trying to code something to:
1)On WM_MOVE save my Main Window x/y coordinates to vars.
2)Write this values to my ini file(using WritePrivateProfileString).
And when i open my program:
1)Read the ini keys for position, load values to vars.
2)MoveWindow to this coordinates.
My progrram already does the read/Write from/to ini normally for Edit Controls content, CheckBox status, etc. I have just added the Main Window position stuff now and im having a weird bug: It saves the values incorrectly: Basically, my code is adding +3 to X and +21 to Y position everytime i open/clode my app, even if i dont move the window.
Now heres come the code, see what you think of it, and keep in mind im only a beginner, so dont be cruel :)
char mainxpos[10]; //This are global
char mainypos[10]; //vars, all of them
int xpos; //declared at
int ypos; //main.h
case WM_CREATE:
IniRead();
...Creates All controls...
xpos=atoi(mainxpos);
ypos=atoi(mainypos);
MoveWindow(mainWindow, xpos, ypos, 369, 490, FALSE);
=>The lines of IniWrite() and IniWrite() that deal with xpos and ypos:
int IniWrite() {
WritePrivateProfileString("positions", "mainwndx", mainxpos, ".\\obs.ini");
WritePrivateProfileString("positions", "mainwndy", mainypos, ".\\obs.ini");
...
return 0;
}
Well thats how things look right now. I could not find out a better way to do it. It works but theres that bug i was telling you about. If anyone got any better idea or a way to improve it, that would be great :)
/Effenberg0x0
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What are you actually writing to your ini-file??
I would suggest that you use:
case WM_MOVE:
RECT rcs;
GetWindowRect(hwnd,&rcs);
xpos = rcs.left;
ypos=rcs.top;
---snip---
I wrote this out to my statusbar and it seems to work ok...
/Roger
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Wow, that was very helpfull. So instead of using WritePrivateProfileString I should use WriteProfileString. Now the coordinate values i store will be absolutely correct...
Congratulations: Youre very smart.
I will probably solve the problem of saving non-exact values if i use the registry instead of the ini without any major modification on the rest of the code.
Oh how havent i seen it before.
...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everybody. I am trying to code something to:
1)On WM_MOVE save my Main Window x/y coordinates to vars.
2)Write this values to my ini file(using WritePrivateProfileString).
And when i open my program:
1)Read the ini keys for position, load values to vars.
2)MoveWindow to this coordinates.
My progrram already does the read/Write from/to ini normally for Edit Controls content, CheckBox status, etc. I have just added the Main Window position stuff now and im having a weird bug: It saves the values incorrectly: Basically, my code is adding +3 to X and +21 to Y position everytime i open/clode my app, even if i dont move the window.
Now heres come the code, see what you think of it, and keep in mind im only a beginner, so dont be cruel :)
char mainxpos[10]; //This are global
char mainypos[10]; //vars, all of them
int xpos; //declared at
int ypos; //main.h
case WM_CREATE:
IniRead();
...Creates All controls...
xpos=atoi(mainxpos);
ypos=atoi(mainypos);
MoveWindow(mainWindow, xpos, ypos, 369, 490, FALSE);
case WM_MOVE:
xpos = LOWORD(lParam);
ypos = HIWORD(lParam);
IniWrite();
return 0;
case WM_DESTROY:
sprintf(mainxpos, "%d", xpos);
sprintf(mainypos, "%d", ypos);
IniWrite();
...
return 0;
=>The lines of IniWrite() and IniWrite() that deal with xpos and ypos:
int IniWrite() {
WritePrivateProfileString("positions", "mainwndx", mainxpos, ".\\obs.ini");
WritePrivateProfileString("positions", "mainwndy", mainypos, ".\\obs.ini");
...
return 0;
}
int IniRead() {
GetPrivateProfileString("positions", "mainwndx", "200", mainxpos, 10, ".\\obs.ini");
GetPrivateProfileString("positions", "mainwndy", "200", mainypos, 10, ".\\obs.ini");
...
return 0;
}
Well thats how things look right now. I could not find out a better way to do it. It works but theres that bug i was telling you about. If anyone got any better idea or a way to improve it, that would be great :)
/Effenberg0x0
What are you actually writing to your ini-file??
I would suggest that you use:
case WM_MOVE:
RECT rcs;
GetWindowRect(hwnd,&rcs);
xpos = rcs.left;
ypos=rcs.top;
---snip---
I wrote this out to my statusbar and it seems to work ok...
/Roger
use the registry that is what it is for..
Greggy
Wow, that was very helpfull. So instead of using WritePrivateProfileString I should use WriteProfileString. Now the coordinate values i store will be absolutely correct...
Congratulations: Youre very smart.
I will probably solve the problem of saving non-exact values if i use the registry instead of the ini without any major modification on the rest of the code.
Oh how havent i seen it before.
...