newbie modal question

This is a discussion on newbie modal question within the Windows Programming forums, part of the Platform Specific Boards category; Is there a way to make a window modal, or is that only for dialog boxes? Thanks in advance...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    newbie modal question

    Is there a way to make a window modal, or is that only for dialog boxes?

    Thanks in advance

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Yes, by disabling the owner window using EnableWindow. Then when your modal window is destroyed, you re-enable your owner window and give it the focus. Enabling and disabling windows is accomplished with the EnableWindow function.

    Here's a quick example. You click on the owner window's client area to lauch the modal window.

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK Owner_WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
    {
      static HWND hwndModal;
      
      switch(msg)  
      {
        case WM_CREATE:
          return 0;
          
        case WM_LBUTTONDOWN:
          hwndModal = CreateWindow( "DialogWnd", 
                                    "Modal Window",
                                    WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE,
                                    (GetSystemMetrics( SM_CXSCREEN )-200)/2, 
                                    (GetSystemMetrics( SM_CYSCREEN )-100)/2, 
                                    200, 100,
                                    hwnd, 0, GetModuleHandle(0), 0 );
          return 0;                                
    
        case WM_DESTROY:
          PostQuitMessage( 0 );
          return 0;
    
        default:
          return DefWindowProc(hwnd,msg,wParam,lParam);
      }
    }
    
    LRESULT CALLBACK Modal_WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam ) 
    {
      static HWND hwndOwner;
      
      switch( msg )
      {
        case WM_CREATE:
           hwndOwner = GetWindow(hwnd,GW_OWNER);
           EnableWindow( hwndOwner, FALSE );
           return 0;
    
        case WM_DESTROY:
           EnableWindow( hwndOwner, TRUE );
           SetFocus( hwndOwner );
           return 0;
           
        default:
           return DefWindowProc( hwnd, msg, wParam, lParam );
      } 
    }
    
    int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
    {
      WNDCLASS wc = {0};
      
      // Register the owner window class.
      wc.lpszClassName = TEXT( "OwnerWnd" );
      wc.hInstance     = hInst ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = Owner_WndProc ;
      wc.hCursor       = LoadCursor(0,IDC_ARROW);
      RegisterClass(&wc);
      
      // Register the modal dialog window's class.
      wc.lpszClassName = TEXT( "DialogWnd" );
      wc.lpfnWndProc   = Modal_WndProc ; 
      RegisterClass(&wc);
      
      // Create the owner window.
      CreateWindow("OwnerWnd",TEXT("Owner Window"),
                   WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                   (GetSystemMetrics( SM_CXSCREEN )-400)/2,
                   (GetSystemMetrics( SM_CYSCREEN )-300)/2,
                   400,300, 0,0, hInst, 0);
      
      MSG  msg ;  
      while( GetMessage(&msg,0,0,0) > 0 ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int)msg.wParam;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    51
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 05:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 09:00 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

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