Thread: Unexpected Output, modal window!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Unexpected Output, modal window!

    Hi all, I am new to Windows programming. I am trying to make a modal window from a main window on press of a button. The main window should be disabled and the modal window should be enabled, but instead I end up getting both the windows disabled in my output. Here's the code:

    Code:
    #include<windows.h>
    
    LRESULT CALLBACK WndProc      (HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;   //Child window procedure//
    
    #define ID_BUTTON1 1
    int CreateChildWindow(HINSTANCE, HWND);                        //Funtion to create child window//
    
    //global variables
    static MSG msg1;
    HWND b1,childHwnd;
    static TCHAR chappname[]= TEXT("Child");
    WNDCLASS child_window;
    static HINSTANCE ChildInstance;
    HWND hwnd;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
    {
    	static TCHAR szAppName[] = TEXT ("Modal_Window") ;
    	MSG msg ;
        WNDCLASS wndclass ;
        wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc   = WndProc ;
        wndclass.cbClsExtra    = 0 ;
        wndclass.cbWndExtra    = 0 ;
        wndclass.hInstance     = hInstance ;
        wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;   	 
        wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
      	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    	wndclass.lpszMenuName  = szAppName ;
    	wndclass.lpszClassName = szAppName ;
         
        if (!RegisterClass (&wndclass))
        {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd=CreateWindow (szAppName, TEXT ("About Modal Window"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
    	   
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ; 	 
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
    	 static HINSTANCE hInstance ;
    	
         switch (message)
         {
    	 case WM_CREATE : b1=CreateWindow(TEXT("Button"),TEXT("Button1"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,10,20,50,30,hwnd,(HMENU) ID_BUTTON1,hInstance,0);
    		              return 0 ;
              
         case WM_COMMAND : 
    		      {
    			    switch(LOWORD(wParam))
    			    {
    		   	      case ID_BUTTON1: CreateChildWindow(ChildInstance,hwnd);
    				                   break;
    				  default : break;
    			    }
                  return 0 ;
    		     }
         case WM_DESTROY : PostQuitMessage (0) ;
    			          return 0; 
    	 }  //switch close
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    int CreateChildWindow(HINSTANCE ChildInstance, HWND hwnd)
    {
    	//int error,i;
    	child_window.style         = CS_HREDRAW | CS_VREDRAW ;
    	child_window.lpfnWndProc   = ChildWndProc ;
    	child_window.cbClsExtra    = 0 ;
    	child_window.cbWndExtra    = 0 ;
    	child_window.hInstance     = ChildInstance ;
    	child_window.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    	child_window.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    	child_window.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
    	child_window.lpszMenuName  = NULL;
    	child_window.lpszClassName = chappname;
    	
    	if (!RegisterClass (&child_window))
        {
    		MessageBox (hwnd, TEXT ("This program requires Windows NT!"), chappname, MB_ICONERROR) ;       //Problem here!//
    						  
    		return -1;
    	}
    			
    	// CreateChildWindow(ChildInstance,hwnd);
            childHwnd = CreateWindow (chappname, TEXT ("child Modal Window"),      
    		  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                             600, 600,
                              hwnd, NULL,ChildInstance , NULL) ;
    
    	 //error = GetLastError ();
    	 
    	 EnableWindow(hwnd,FALSE);               //disable main window
    	  return 0;
    }  
    
    LRESULT CALLBACK ChildWndProc(HWND childHwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    	switch(message)
    	{
    	case WM_CREATE: break;
    	case WM_PAINT: break;
    	case WM_DESTROY :PostQuitMessage (0) ;
    			            EnableWindow(hwnd,TRUE);
                                        return 0;
    	default: break;
    	}
    	return DefWindowProc(childHwnd, message, wParam, lParam) ;
    }
    I tried again and what I found is that my child window is constrained inside the main window.. so when my main window is disabled the child becomes disabled. I can't find how to remove this so that my child window can move outside my main window as well.

    Well, I have removed this problem too.. Now the only issue is that when I close my modal window to enable the main window again and press the button to again create the modal window, I am unable to register it.. Code has been edited to show the changes done.

    Any suggestions would be greatly appreciated! Please reply!
    Last edited by gaurav_13191; 06-23-2011 at 11:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify a Modal window?
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 07-25-2008, 12:27 PM
  2. Unexpected Output in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 10:50 PM
  3. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM
  4. unexpected ( unwanted ) output in a file
    By Rpog in forum C Programming
    Replies: 2
    Last Post: 04-15-2004, 07:33 AM