Using several windows

This is a discussion on Using several windows within the Windows Programming forums, part of the Platform Specific Boards category; How can I use more than one window in one program, so I would be able to control every windows ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Using several windows

    How can I use more than one window in one program, so I would be able to control every windows messages separately.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    try with Multiple Document Interface (MDI)
    Niara

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What do you mean by try? Search from google?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    (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

  5. #5
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    How can I use more than one window in one program, so I would be able to control every windows messages separately.
    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.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well I didn't find a way to handle their messages separately...

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It doesn't work...
    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:
                 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;
    }
    This works, but I can only create things like this
    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;
    }
    Last edited by maxorator; 10-03-2005 at 01:15 AM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why doesn't if(hwnd==hwn2) work?

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Here is a good example of MDI:

    This is only the .c file, but it's good enough.
    Attached Files Attached Files

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yes, but still, why doesn't "if(hwnd==hwn2)" work?

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by maxorator
    Yes, but still, why doesn't "if(hwnd==hwn2)" work?
    How does it not work? Doesn't compile? Or doesn't evaluate to true?

    BTW I notice in the code you posted, you used hwn, not hwnd.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.

  13. #13
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Are you checking it in the WM_CREATE message?
    CreateWindow(Ex) has not returned yet.

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thanks, I got it working...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21