PDA

Click to See Complete Forum and Search --> : Pos and size help


Oct 20th, 2000, 02:42 PM
Ok, all this app is supposed to do, at the moment, is load at the pos and size from which it was exited at. But for some reason, whenever you start it, its bigger. The top-right corner is in the right place, but the window itself is bigger for some reason. Then if you exit and start it again, itll be even bigger. Heres the code, if i didnt explain it right, let me know.


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>

HINSTANCE hAppInstance;
HWND hMainWindow;
int WindowX1;
int WindowX2;
int WindowY1;
int WindowY2;

void LoadVar();
void SaveVar();
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
MSG Msg;
hAppInstance = hInstance;

LoadVar();

wc.cbClsExtra = NULL;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
wc.hIcon = LoadIcon(hAppInstance, IDI_APPLICATION);
wc.hIconSm = LoadIcon(hAppInstance, IDI_APPLICATION);
wc.hInstance = hAppInstance;
wc.lpfnWndProc = MainWindowProc;
wc.lpszClassName = "MainWindowClass";
wc.lpszMenuName = NULL;
wc.style = NULL;

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Could not register 'MainWindowClass'.\nShuttin down...", "Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}

hMainWindow = CreateWindowEx(NULL, "MainWindowClass", "HeroDev", WS_OVERLAPPEDWINDOW, WindowX1, WindowY1, WindowX2, WindowY2, NULL, NULL, hAppInstance, NULL);

if(!hMainWindow)
{
MessageBox(NULL, "Could not create window 'hMainWindow'.\nShutting down...", "Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}

ShowWindow(hMainWindow, nShowCmd);
UpdateWindow(hMainWindow);

while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
}

LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;

case WM_DESTROY:
SaveVar();
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}


void LoadVar()
{
WindowX1 = GetPrivateProfileInt("Position", "X1", 200, "herodev.ini");
WindowY1 = GetPrivateProfileInt("Position", "Y1", 200, "herodev.ini");
WindowX2 = GetPrivateProfileInt("Position", "X2", 200, "herodev.ini");
WindowY2 = GetPrivateProfileInt("Position", "Y2", 200, "herodev.ini");
}

void SaveVar()
{
RECT MainWindow;
char x1[sizeof(WindowX1)];
char y1[sizeof(WindowY1)];
char x2[sizeof(WindowX2)];
char y2[sizeof(WindowY2)];

GetWindowRect(hMainWindow, &MainWindow);
sprintf(x1, "%d", MainWindow.left);
sprintf(y1, "%d", MainWindow.top);
sprintf(x2, "%d", MainWindow.right);
sprintf(y2, "%d", MainWindow.bottom);
WritePrivateProfileString("Position", "X1", x1, "herodev.ini");
WritePrivateProfileString("Position", "Y1", y1, "herodev.ini");
WritePrivateProfileString("Position", "X2", x2, "herodev.ini");
WritePrivateProfileString("Position", "Y2", y2, "herodev.ini");
}

parksie
Oct 20th, 2000, 05:27 PM
I think you're getting confused with your sizeofs near the bottom. That simply tells you the size of a pointer...

Try something like this:

void SaveVar() {
RECT MainWindow;
char x1[6];
char y1[6];
char x2[6];
char y2[6];

GetWindowRect(hMainWindow, &MainWindow);
sprintf(x1, "%d", MainWindow.left);
sprintf(y1, "%d", MainWindow.top);
sprintf(x2, "%d", MainWindow.right);
sprintf(y2, "%d", MainWindow.bottom);
WritePrivateProfileString("Position", "X1", x1, "herodev.ini");
WritePrivateProfileString("Position", "Y1", y1, "herodev.ini");
WritePrivateProfileString("Position", "X2", x2, "herodev.ini");
WritePrivateProfileString("Position", "Y2", y2, "herodev.ini");
}

...hmm. Why not use the registry? That's what it's there for, and plus it makes it a lot easier to store numbers :).

Here's some code: http://www.parksie.uklinux.net/Registry.h & http://www.parksie.uklinux.net/Registry.cpp

Oct 20th, 2000, 06:18 PM
All the sizeof() function does is return the size of a variable in bytes (i think its bytes), that part doesnt have anything to do with the window. I know this because i figured it out. The problem had to do with that the values that were be returned were from the top right corner of the screen, not the window. I solved this with just two lines of code...


MainWindow.right = MainWindow.right - MainWindow.left;
MainWindow.bottom = MainWindow.bottom - MainWindow.top;


But thanks for posting though. and ill check out those files. The reason im using ini files is cuz im still new to saving variables.

parksie
Oct 21st, 2000, 03:56 AM
sizeof is actually a compiler directive that returns the size in bytes of any data structure. It's been used in portable code for aeons to check the size of int, which is architecture-dependent, as opposed to types like long and short which are fixed at 4 and 2 bytes. BTW...on a 32-bit system, int = long :).

The registry is actually easier (especially if you check out those classes), but if you want to use .ini files then that's okay.