Thread: MultiThreaded GUI.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    137

    MultiThreaded GUI.

    Ok, i have two programs.
    One launches the other at some point.
    And i have my programs in classes for win32.

    Now what i want to do, is combine them in some sort of thread. I can do this easily using CreateThread etc... However, i dont want them in same window.

    I want them to be opened and LOOK like as if they are 2 seperate programs. So i can switch between program 1 and 2. However, i want it all in one exe...

    Is this at all possible? I think it is, as i've seen some other programs do it. Right now my program just launches the other program using CreateProcess.
    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I'ts not a good idea to try to put a GUI in two different threads. You can however create as many top level windows as you want and have worker threads to send mesages to each so it might look like seperate programs.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    But in the taskbar on bottom, will it look like 2 different programs? Because mainly i want to be able to switch around with it.

    I understand building GUI in same spot is better (I have no idea why tho), i could just send createstruct to other functions but anyway.

    How do i create extra top level windows?? Any tutorials on that? I know dialog boxes but they are part of the main program and don't look like a seperate window.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Here is a quick example. As you can see both windows are added to the task bar and the user won't know if it is the same program or seperate programs (well he can guese because the windows look similar and have the same icon but you can change that).
    Code:
    #include <windows.h>
    
    const char szClassName[] = "Window Class";
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        MSG Msg;
    
        
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style  = 0;
        wc.lpfnWndProc = &WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = szClassName;
        wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
    	RegisterClassEx(&wc);
    
    	HWND hwnd1 = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, "Window 1", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, hInstance, NULL);
    	HWND hWnd2 = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, "Window 2", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, hInstance, NULL);
    
    	ShowWindow(hwnd1, nCmdShow);
        UpdateWindow(hwnd1);
    	ShowWindow(hWnd2, nCmdShow);
    	UpdateWindow(hWnd2);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    You will probably want to create seperate window procedures for each window.
    Last edited by Quantum1024; 05-14-2006 at 05:34 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Thanks a lot for the help.

    I'll tell you how it goes when i do it.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Ok, seems like this is a wee bit more complicated for me. I'm using a more complex class, like the one in foosyerdoos.fsnet.co.uk and it seems like it's not working as i have been debugging for a couple hours.

    I got to a point where it says "failed to create wnd". Which means i can register WNDCLASSEX, but i cannot create the window or something. I guess something to do with something getting lost because of having such complicated classes.

    So i have two classes. One is a windows program that works fine, then at a certain part a function is activated, which activates WINAPI WinMain2 (the other program), and tries to make it. However things aren't going too well.

    Ill keep debugging.

    EDIT:
    Here's exactly what I did. It compiled and linked great (im using DevC++), but it fails to "create wnd". The following code is like the main.cpp of my FIRST program. It's suppose to launch the second program, which is included in another class Called CppWnd2.

    The class functions i used are self-explanatory and work great.

    Code:
    CppWnd2 App2;
    int CppWnd::LaunchSecondProgram(){
        HINSTANCE hInstance;
        App2.SetInstance(hInstance);
    //give the wnd a class name to register with os
           App2.SetWndClassName(TEXT("WndClass"));
    //give the wnd a caption
           App2.SetWndCaption(TEXT("Second Program"));
           CoInitialize(NULL);
    //register and create wnd,checking the return value to ensure that a valid wnd handle has
    //been made.
           if (App2.Create(hAppWnd)) // Parent Handle.
        {
        MSG Msg;    //a simple structure for storing message information
        //start message loop
        while (GetMessage(&Msg,NULL,0,0))
            {
                //translate and dispatch other messages
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
            }
        CoUninitialize();
        return Msg.wParam;
        }
        return 0;
    }
    LRESULT CALLBACK MainWndProc2(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
    //just send the message and other parameters to the class function for handling
    return App2.CppWndProc2(hwnd,Message,wParam,lParam);
    }
    EDIT AGAIN: i fixed the code, so no errors anymore. There is no error. The only problem is when i launch the other program, it just closes the parent window on me and doesn't show any other window. The program just dissappears!!! Everything gets interpretted till the CreateWindowEx is called for this window.

    I set window style to: WS_CAPTION | WS_POPUPWINDOW;
    and class style to: CS_HREDRAW|CS_VREDRAW;

    The hwnd that is returned... works great, no error messages, and hwnd is NOT null. However, they just dissappear... I think something to do with the styles i set.
    Well i hear the error message, i hear a sound that's familiar to a messagebox MB_ICONERROR. However, i can't see it O_O.
    Last edited by execute; 05-17-2006 at 05:52 PM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Ok it's fixed.

    I shouldn't have redefined hInstance. HINSTANCE hInstance;
    I just had to give the original HINSTANCE of the first program .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Programming...
    By ShadeS_07 in forum C++ Programming
    Replies: 12
    Last Post: 12-28-2008, 04:58 PM
  2. Convert Windows GUI to Linux GUI
    By BobS0327 in forum Linux Programming
    Replies: 21
    Last Post: 11-27-2005, 04:39 AM
  3. .NET And GUI Programming :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2002, 04:22 PM
  4. GUI Programming :: C++ Exclusive
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2002, 03:22 PM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM