|
-
Aug 27th, 2001, 11:02 PM
#1
Thread Starter
Lively Member
Stop controls from floating around (and yes, they are children of the main form)
PHP Code:
#include <windows.h>
HINSTANCE trackinst;
HWND myform, pushbutton, editbox;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE myinst, HINSTANCE beforeinst, LPSTR cmds, int nFunsterStil) {
WNDCLASSEX wndcl;
MSG messages;
char szClassName[ ] = "WindowsApp";
trackinst = myinst;
wndcl.cbClsExtra = 0;
wndcl.cbSize = sizeof(WNDCLASSEX);
wndcl.cbWndExtra = 0;
wndcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wndcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndcl.hInstance = myinst;
wndcl.lpfnWndProc = WindowProc;
wndcl.lpszClassName = szClassName;
wndcl.lpszMenuName = NULL;
wndcl.style = 0;
if (!RegisterClassEx(&wndcl)) return 0;
myform = CreateWindowEx(0,
szClassName,
"Push the Button",
WS_BORDER || WS_CAPTION || WS_VISIBLE,
500, 500, 600, 300,
HWND_DESKTOP,
NULL,
myinst,
NULL);
ShowWindow(myform, nFunsterStil);
pushbutton = CreateWindowEx(0,
"BUTTON",
"Click me!",
WS_CHILD || WS_VISIBLE,
400, 75, 76, 150,
myform,
NULL,
myinst,
NULL);
ShowWindow(pushbutton, SW_SHOW);
editbox = CreateWindowEx(0,
"EDIT",
"jj",
WS_CHILD || WS_VISIBLE,
0, 100, 600, 26,
myform,
NULL,
myinst,
NULL);
ShowWindow(editbox, SW_SHOW);
while (GetMessage(&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
switch (wMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
if((HWND)lParam == pushbutton) {
SetWindowText(editbox, "Hello, world!");
}
}
return 0;
}
(Yes, I know some C++: I worked mainly with consoles but now am trying to go to Win32. With that, the difficulty level went way up!)
That was the entire code, I personally think it has something to do with the ShowWindow function for creating the form. I'll check back tomorrow, see ya!
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Aug 28th, 2001, 02:49 AM
#2
I don't understand your problem, but here are some things I noticed in your code:
1) Why do you use CreateWindowEx if you don't use the dwStyleEx parameter?
2) Do you want the main window as a child of the desktop? If so, add the WS_CHILD style. If not, supply NULL as hwndParent.
3) You should avoid global variables as much as possible. It's not necessary to use globals for all these windows.
4) Create the child windows in the message handler for WM_CREATE, and save their handles in static variables of the window functions (maybe this even solves you problem, not sure however)
5) Use either WS_VISIBLE or ShowWindow, not both. Usually you use ShowWindow (and UpdateWindow) for the main window, and WS_VISIBLE for it's childs.
6) Use while(GetMessage(...) > 0). This is a trick I just recently learned and that honors the fact that GetMessage returns 0 in case of a failure.
7) Use a different approach to test if the button was clicked:
Code:
// instead of
case WM_LBUTTONDOWN:
.
.
// use
case WM_COMMAND:
switch(HIWORD(wParam))
{
case EN_CLICKED:
if(lParam == hwndButton)
{
// do whatever you want
}
break;
}
This has the advantage of better extensibility and avoids an unnecessary hit test (windows performs it's own hit test regardless of what you do)
That's all for now
All the buzzt
CornedBee
-
Aug 28th, 2001, 03:28 AM
#3
Also, you should assign an ID to the child windows (using the hMenu parameter)
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|