I want main window to be a dialog...

This is a discussion on I want main window to be a dialog... within the Windows Programming forums, part of the Platform Specific Boards category; I'm writing my own database program and I want the "main" to be a dialog because it's going to be ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    I want main window to be a dialog...

    I'm writing my own database program and I want the "main" to be a dialog because it's going to be easier to make and look nicer

    I'm going to start off with a window (that is really main) to choose whether they want to start a new file or edit an existing one. Well, when they choose this option, should I destroy the main window when I need to show the dialog and handle those messages? Is it a good idea to destroy the "main" window leaving just a dlg?
    1978 Silver Anniversary Corvette

  2. #2
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey Garfield,


    Ok, its pretty simple. Instead of using CreateWindow() to make you main window, you need to use CreateDialog, heres an example:


    HWND hWnd = CreateDialog(hInstance,
    (LPTSTR)IDD_DIA,
    0,
    (DLGPROC)Dialog
    );

    Dialog is your window prodecure for this, heres how you can define it:

    BOOL Dialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch(message)
    {
    case WM_CLOSE:
    {
    PostQuitMessage(0);
    return TRUE;
    }

    }
    return FALSE;
    }



    Ive also attached a prog that i made to do this if your still having problems...


    Thanks
    TNT
    Attached Files Attached Files
    TNT
    You Can Stop Me, But You Cant Stop Us All

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    A couple of things to note:

    1. Don't call DefWindowProc for default handling of modeless dialog box messages - return FALSE. See DialogProc on msdn for clarification.

    2. If you want default keyboard handling for dialog boxes eg. tabbing, ESC to quit then use IsDialogMessage fn in your message loop:

    Code:
    while (GetMessage(&Msg,NULL,0,0))
        {
        /*first check if the message is from a resource defined keyboard accelerator key*/
        if (!TranslateAccelerator(hwnd,hAccelTable,&Msg))
            {
            /*now check for default dlg keyboard keys eg TAB, ESC*/
    	if ((!IsWindow(hwnd))||(!IsDialogMessage(hwnd,&Msg)))
                {
                /*translate and dispatch other messages*/
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
                }
    	}
        }
    The above message loop will also enable you to include your own resource defined accelerator table, where hAccelTable is the handle (HACCEL) of the table.

    To check for ESC key look for IDCANCEL as LOWORD(wParam) in your WM_COMMAND handler. To check for RETURN key then check for IDOK as LOWORD(wParam) in WM_COMMAND handler.

    Use DestroyWindow to kill off the dialog.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    You can of course still create the main as a window and then hide it, or not, as the dlg is called. Giving you the benefits of a window. Or make the window small (1,1) and hide it behind the dlg.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So, I can just destroy the "main" window when the dialog is up? But, how would that work? That would PostQuitMessage(0) and end the program. Would I use that function else where?
    1978 Silver Anniversary Corvette

  6. #6
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    When you want to close the dialog use EndDialog(). Although as you have the HWND i dont see why you shoudnt use DestroyWindow()...
    TNT
    You Can Stop Me, But You Cant Stop Us All

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>When you want to close the dialog use EndDialog(). <<

    Only if it's a modal dialog box (created with DialogBox or DialogBoxParam).

    It isn't necessary to create any other window than the dialog box you have designed in your resource (unless you are using multiple dialogs, but the principle is the same).

    If you visit my wee humble website and check out the 'About Dialog Box' example in the 'Resources' section you will get a complete example of creating a main window for a dialog resource with an about box, accelerators, string tables etc etc etc.

    The 'Dialog Box and Controls' example is simpler still but has a non-fatal flaw that I have not got round to correcting yet ( if the window fails to create then the messagebox will not display string resources - these strings need to be explicit).

    http://www.foosyerdoos.fsnet.co.uk

    (sorry, I would have attached it to this post but I can't find the 'attach file' thingy )

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So what should I do with the real main window when I call the main dlg? I can't destroy it because then it would PostQuitMessage and exit the program. Should I just hide it?
    1978 Silver Anniversary Corvette

  9. #9
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Yer, never call ShowWindow, UpdateWindow. Or just call ShowWindow(hWnd, SW_HIDE); from WM_CREATE in the main windows loop. You should just hide it though when the dialog is the main window.

    Hope that helps
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, so I handle the main window's WM_CREATE function and then call DialogBox? I guess I should just do this. Thanks!
    1978 Silver Anniversary Corvette

  11. #11
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    THe simplest way I can think to do all this, but you'd need a resource:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   PSTR lpCmdLine, int nShowCmd)
    {
    	DialogBox(hInstance,MAKEINTRESOURCE(MAINDLG), NULL, DlgProc);
    	return 0;
    }
    and...

    Code:
    BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		return true;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDCANCEL:
    			EndDialog(hDlg,0); 
    			return true;
    		}
    		break;
    	}	
    	return false;
    }

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I think I finally understand what you are asking, Garfield?

    You have a main, normal window that is made with CreateWindow/CreateWindowEx. When the user selects a menu or takes some other action you want a dialog to appear to get some input from the user. You want to know what you should do about your main window when the dialog appears?

    Just leave the window as it is - it isn't a good idea to destroy it unless you have a particular reason for doing so.

    There are two kinds of dialog boxes you can cause to appear:

    1. Modal Dialogs - these basically demand attention and don't let you interact with the 'main' window until you have dealt with them. Use DialogBox or DialogBoxParam to create, and EndDialog to kill off. -KEN-'s succinct example demonstrates one of these.
    2. Modeless dialogs - these let you interact with the main window ie you can shift the focus between the dialogbox and the main window. Use CreateDialog or CreateDialogParam to create, and DestroyWindow to kill off.

    The example I directed to you earlier creates a modeless dialog box as a main window. When a menu item is selected a modal 'About' dialog box is shown.

  13. #13
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I'm going to want a modeless window because it should be as much like a normal window that let's you shift focus. So I would use CreateDialog? Alright, I think that's how I'm going to do it. Thanks!

    What do you guys normally do for a program that requires lots of controls on the 'opening' window? Do you just go straight to a dialog, or do you hard code the controls on the window?
    1978 Silver Anniversary Corvette

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Lots of my ctrls are realy HDC's drawn to look or act as ctrls (on my main window). Others are drawn on the fly. More than 6 or so and I use a dlg, even if I have to paste it to the screen.

    I open larger dlgs and just show them above my main window.

    Some smaller apps use a main window that never is show, just used to get the progam displayed in the task bar. I just create them 1,1 and move the main window so it is always behind the dlg.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > Lots of my ctrls are realy HDC's drawn to look or act as ctrls

    What do you mean?
    1978 Silver Anniversary Corvette

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  2. Dialog window size
    By xkrja in forum Windows Programming
    Replies: 1
    Last Post: 11-22-2006, 05:07 AM
  3. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  4. how to set this dialog to this
    By stallion in forum Windows Programming
    Replies: 3
    Last Post: 01-31-2003, 04:19 PM
  5. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 07:58 PM

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