dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: A question regarding creating a window

  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding creating a window

    This is a typical way to create a window,
    Code:
    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;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        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 = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    At the end, we always call GetMessage to handle the windows message. It is noted that there is a while loop which means unless we close the window, the program won't proceed. I wonder if there is any way to display the window and at the mean time, the program executes the code after while loop? I know multithreaded programming can be an option. Is there any other way than multithreaded programming to achieve that? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: A question regarding creating a window

    Have you ever thought about how splash screens or modeless dialogs are implemented?

    If you research how these are done, you will find your answer.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding creating a window

    the 'normal' behaviour for a messageloop is to loop around geting and processing messages.

    if you make a more elaborate loop, you can do "background" processing while still handling the window. you do need to be carefull though, you're still expected to call and handle messages in a timely fashion or your window will be marked as "application hung".
    For some things this works and may be easier than multithreading, but it has issues of it's own.

    Better solution: multithreading.

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding creating a window

    Quote Originally Posted by Arjay View Post
    Have you ever thought about how splash screens or modeless dialogs are implemented?

    If you research how these are done, you will find your answer.
    Ok, I created a modoless dialog,
    Code:
    HWND g_hDlg = NULL;
    
    			g_hDlg = CreateDialogA(NULL, MAKEINTRESOURCEA(IDD_MYDLG), NULL, (DLGPROC)DlgProc);
    
    			DWORD ret = GetLastError();
    			char s[64];
    
    			sprintf(s, "%d\n", ret);
    			MessageBoxA(NULL, NULL, s, MB_OK);
    
    			if (g_hDlg != NULL)
    			{
    				MessageBoxA(NULL, NULL, "hi there", MB_OK);
    				ShowWindow(g_hDlg, SW_SHOW);
    			}
    
    			MSG msg = {};
    
    			while (GetMessage(&msg, NULL, 0, 0))
    			{
    				if (!IsDialogMessage(g_hDlg, &msg))
    				{
    					TranslateMessage(&msg);
    					DispatchMessage(&msg);
    				}
    			}
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch (Message)
    	{
    	case WM_INITDIALOG:
    		return TRUE;
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case IDOK:
    			EndDialog(hwnd, IDOK);
    			break;
    		case IDCANCEL:
    			EndDialog(hwnd, IDCANCEL);
    			break;
    		}
    		break;
    
    	default:
    		return FALSE;
    	}
    	return TRUE;
    }
    But the modeless dialog isn't displayed. I tried to get error code from calling CreateDialogA since CreateDialogA returns NULL. The error code is 1813 which means ERROR_RESOURCE_TYPE_NOT_FOUND. I couldn't figure out why I got this error. Thanks.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: A question regarding creating a window

    Quote Originally Posted by LarryChen View Post
    But the modeless dialog isn't displayed. I tried to get error code from calling CreateDialogA since CreateDialogA returns NULL. The error code is 1813 which means ERROR_RESOURCE_TYPE_NOT_FOUND. I couldn't figure out why I got this error. Thanks.
    When I asked you to research creating a modeless dialog, what did you find in terms of documentation? The code you posted seems far different from what I came up with. I searched for "how to create a modeless dialog with winapi" and came up with Using Dialog Boxes in msdn.

    About half way down is a section on modeless dialogs. I suggest that you create a sample project and copy the code exactly to see how to create a modeless dialog.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,997

    Re: A question regarding creating a window

    The error code is 1813 which means ERROR_RESOURCE_TYPE_NOT_FOUND. I couldn't figure out why I got this error.
    Have you got a problem with your resource definitions?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: A question regarding creating a window

    Quote Originally Posted by LarryChen View Post
    Ok, I created a modoless dialog,

    But the modeless dialog isn't displayed.
    Unlike to modal dialog, modeless dialog will be shown only in case your dialog template includes WS_VISIBLE style, or otherwise the dialog was forced to show up with ShowWindow()

    Creating Modeless Dialog
    Best regards,
    Igor

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding creating a window

    Thanks for your replies. I think the problem is that when I called CreateDialog, it returns NULL handle because of the error ERROR_RESOURCE_TYPE_NOT_FOUND. I already included the dialog template in my project and defined its ID. What could be the reason for ERROR_RESOURCE_TYPE_NOT_FOUND when CreateDialog is called?

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,594

    Re: A question regarding creating a window

    Did you also include the .rc file to the project?
    Victor Nijegorodov

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding creating a window

    Quote Originally Posted by VictorN View Post
    Did you also include the .rc file to the project?
    Yes. Is there other things I should be aware of? Thanks.

  11. #11
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding creating a window

    Quote Originally Posted by OReubens View Post
    the 'normal' behaviour for a messageloop is to loop around geting and processing messages.

    if you make a more elaborate loop, you can do "background" processing while still handling the window. you do need to be carefull though, you're still expected to call and handle messages in a timely fashion or your window will be marked as "application hung".
    For some things this works and may be easier than multithreading, but it has issues of it's own.

    Better solution: multithreading.
    It turns out the first argument of CreateDialog wasn't set up properly. Now I am able to create the dialog. But modeless dialog is not a solution. Basically in order to display the dialog all the time, the while loop shouldn't be broken. As the result, if I want to display the dialog, I am still not able to process any code after while loop. Could you explain "background" processing in more details? Are you sure "background" processing will solve my problem even without using multithreaded programming? Thanks.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: A question regarding creating a window

    You should revise your approach and stop thinking about executing some code after message pump loop. Instead, you should focus on other possibilities. GUI programming really differs from what you might be accustomed to being writing console apps. Your code execution must not conflict with or hamper anyhow message pumping, this what you always focus on when programming GUI. Actually, multithreading is the answer, and I do not understand why you so actively reject moving that way.

    Background processing is a real ancient technique came from Win16 programming. I can see no point in learning that, as it even more complicates the code compared to regular multithreading. In fact you should split your logic to rather short iterations and take care about timely yielding for the sake of proper message pumping. 'Ugly' is the word that exactly describes the trick.
    Best regards,
    Igor

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: A question regarding creating a window

    The best thing for Larry to do is to pick up a book like Petzold and learn the Win32 windowing concepts. Learning by trial and error rarely ever results in picking up the necessary background.

  14. #14
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding creating a window

    Quote Originally Posted by LarryChen View Post
    Could you explain "background" processing in more details? Are you sure "background" processing will solve my problem even without using multithreaded programming? Thanks.
    explaining it is easy...
    instead of the normal type messageloop:
    Code:
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    you change that to an peek-loop
    Code:
    while (WM_QUIT != uMsg.message)
    {
         while (PeekMessage (&uMsg, NULL, 0, 0, PM_REMOVE) > 0) //Or use an if statement
         {
              TranslateMessage (&uMsg);
              DispatchMessage (&uMsg);
         }
         
        // Background processing goes here
    }
    note that "background processing " in this case needs to be very "small". A few milisec at most (less than a milisec prefered), any more will cause UI interaction problems.

    You will find loops like this in most DirectX based games. It's still an "easy way out" for such type of approaches.

    as said, the better approach is multithreading. The above has all sorts of issues of it's own.


    THere's no way I can be "sure" this will "solve" your problems. you'll have to evaluate/try that on your own.
    It can solve some problems, but it's not a magic bullet.
    Last edited by OReubens; January 19th, 2015 at 07:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)