How can I use more than one window in one program, so I would be able to control every windows messages separately.
Printable View
How can I use more than one window in one program, so I would be able to control every windows messages separately.
try with Multiple Document Interface (MDI)
Niara
What do you mean by try? Search from google?
(a little confusion), googling is an option, but you can learn mdi from the msdn site, or also on the forger's win32 tutorial (take a look at 2nd chapter, app part 4). the idea is to have a main window that will contain other child windows; those child will be able to process their messages individually, and also are able to send messages to the main window. On the forger's mdi sample, instead of using a huge edit control, you can use the controls thar you need for the app, so each child window will process the messages generated by the controls + the user activity + some other msg sended from the main window; then, on a certain case, you can send data from the child to the main wnd to process it from there.
there's another way to 'connect' windows, but I haven't done it never (and I haven't idea on how to do it): that is by transfering data betwen processes.
Niara
Call CreateWindow more than once. To allow each window to have its own message handler, your window class (WNDCLASS) must somehow know which message handler. In other words, each HWND must be bound to some message handler. You can use std::map or store the address of the message handler (a WNDPROC or something else) inside the window's extra bytes.Quote:
How can I use more than one window in one program, so I would be able to control every windows messages separately.
Well I didn't find a way to handle their messages separately...
It doesn't work...
This works, but I can only create things like thisCode:#include <windows.h>
#define B1 50001
#define B2 50002
#define B3 50003
#define B4 50004
#define B5 50005
#define B6 50006
HWND b1,b2,b3,b4,b5,b6;
HWND hwn,hwn2;
char szClassName[ ] = "WindowsApp";
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwn = CreateWindowEx (
0,
"WindowsApp",
"Window 1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL);
hwn2 = CreateWindowEx (
0,
"WindowsApp",
"Window 2",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL);
ShowWindow (hwn, nFunsterStil);
ShowWindow (hwn2, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
if(hwnd==hwn2){
b1=CreateWindowEx(0,"Button","Window 2", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 40, 100, 50, hwnd, (HMENU)B1, GetModuleHandle(NULL), NULL);
}
else if(hwnd==hwn){
b2=CreateWindowEx(0,"Button","Window", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 40, 100, 50, hwnd, (HMENU)B2, GetModuleHandle(NULL), NULL);
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Code:#include <windows.h>
#define B1 50001
#define B2 50002
#define B3 50003
#define B4 50004
#define B5 50005
#define B6 50006
HWND b1,b2,b3,b4,b5,b6;
HWND hwn,hwn2;
char szClassName[ ] = "WindowsApp";
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwn = CreateWindowEx (
0,
"WindowsApp",
"Window 1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL);
hwn2 = CreateWindowEx (
0,
"WindowsApp",
"Window 2",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL);
ShowWindow (hwn, nFunsterStil);
ShowWindow (hwn2, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
b1=CreateWindowEx(0,"Button","Window 2", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 40, 100, 50, hwn, (HMENU)B1, GetModuleHandle(NULL), NULL);
b2=CreateWindowEx(0,"Button","Window", WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 40, 100, 50, hwn2, (HMENU)B2, GetModuleHandle(NULL), NULL);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Why doesn't if(hwnd==hwn2) work?
Here is a good example of MDI:
This is only the .c file, but it's good enough.
Yes, but still, why doesn't "if(hwnd==hwn2)" work?
How does it not work? Doesn't compile? Or doesn't evaluate to true?Quote:
Originally Posted by maxorator
BTW I notice in the code you posted, you used hwn, not hwnd.
Doesn't evaluate to true. hwnd is what WindowProcedure receives, hwn is the handle of the first window, hwn2 is the handle of the second window. "if(hwnd==hwn2)" should check if the window the WM_CREATE message was sent from was the second window.
Are you checking it in the WM_CREATE message?
CreateWindow(Ex) has not returned yet.
Thanks, I got it working...