Thread: Manipulating window from a dialog box

  1. #1
    george7378
    Guest

    Manipulating window from a dialog box

    Hi,

    If I have a program with a dialog box as the main interface, is it possible to use buttons on the dialog box to create, update and destroy a window? For example, if my dialog box has a button on it with 'create window', how would I go about creating a window when I press it? Would I still need to register and create the window/message loop in the WINAPI WinMain() section, or would I need to do something else? Is it possible to close a window if I press a 'close window' button on my dialog box, and can I update the contents of the window (with WM_PAINT) if I have an 'update' button on the dialog box?

    Right now, I have a BOOL CALLBACK DlgProc() section for handling the dialog box, an LRESULT CALLBACK WndProc() section for handling the window, and all that I have in the int WINAPI WinMain() section is the following code:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MENUWINDOW), NULL, DlgProc);
    }
    ...this creates the main menu dialog box with the buttons on it, but I am not really sure what to put where to accomplish my task

    Thanks!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to use a "modeless" dialog box and handle the message loop yourself. Here's an example (not the best coding style, but you get the idea). Just make a dialog resource with an OK button (IDOK) and cancel button (IDCANCEL). Pushing the OK button toggles the window. Cancel closes the app.
    Code:
    #include <windows.h>
    
    #define CLASS_NAME "AClass"
    
    HWND      g_hwndWnd;
    HWND      g_hwndDlg;
    HINSTANCE g_hinst;
    
    INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    	switch (uMsg) {
    	case WM_INITDIALOG:
    		return TRUE;
    	case WM_COMMAND:
    		switch (LOWORD(wParam)) {
    		case IDOK:
    			if (g_hwndWnd == 0) {
    				g_hwndWnd = CreateWindowEx(
    					0, CLASS_NAME, "Example",
    					WS_OVERLAPPEDWINDOW,
    					200, 0, 300, 300,
    					NULL, NULL, g_hinst, NULL
    					);
    				if (g_hwndWnd == NULL) {
    					MessageBox(0, "Can't create window", 0, 0);
    					return TRUE;
    				}
    				ShowWindow(g_hwndWnd, SW_SHOW);
    			}else{
    				DestroyWindow(g_hwndWnd);
    				g_hwndWnd = 0;
    			}
    			return TRUE;
    		case IDCANCEL:
    			DestroyWindow(g_hwndDlg);
    			g_hwndDlg = 0;
    			PostQuitMessage(0);
    			return TRUE;
    		}
    	}
    	return FALSE;
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    	switch (uMsg) {
    	case WM_CLOSE:
    		DestroyWindow(g_hwndWnd);
    		g_hwndWnd = 0;
    		return 0;
    	}
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int WinMain(HINSTANCE hinst, HINSTANCE unused, LPSTR cmd, int show) {
    	WNDCLASS wc = { 0 };
    	MSG msg;
    	BOOL bRet;
    
    	g_hinst = hinst;
    
    	wc.style         = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc   = WindowProc;
    	wc.hInstance     = hinst;
    	wc.lpszClassName = CLASS_NAME;
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	RegisterClass(&wc);
    
    	g_hwndDlg = CreateDialog(hinst, MAKEINTRESOURCE(1001), 0, DialogProc);
    
    	while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
    		if (bRet == -1) {
    			MessageBox(0, "Error in GetMessage", 0, 0);
    			return 0;
    		}
    		else if (!IsWindow(g_hwndDlg) || !IsDialogMessage(g_hwndDlg, &msg)) {
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    george7378
    Guest
    Thanks for the reply. I am wondering if the same thing applies to timers? I have a timer which activates when I press a button on the dialog in the DialogProc() section, and it doesn't seem to be possible to detect the ticking of the timer by using a WM_TIMER command in the CALLBACK WindowProc() section. Is this also a problem caused by modal dialogs?

    Thanks!

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You choose which window procedure will handle the timer by passing SetTimer the desired hwnd:
    Code:
    SetTimer(g_hwndWnd, 1, 200, 0);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting Dialog window to main window
    By vopo in forum Windows Programming
    Replies: 1
    Last Post: 11-26-2007, 07:55 PM
  2. Embedding a window within a dialog window
    By deathscythe in forum Windows Programming
    Replies: 5
    Last Post: 07-30-2006, 02:55 AM
  3. Window Dialog Problem
    By GuardianBarak in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2004, 09:29 AM
  4. Window (dialog) w/o border
    By il96 in forum Windows Programming
    Replies: 1
    Last Post: 03-02-2004, 12:54 PM
  5. opening a dialog window from inside a dialog window
    By uvacow in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2002, 09:27 AM