Page 1 of 1

Win32 - Handling two windows (Whats wrong with this code?)

Posted: Mon Feb 17, 2014 5:31 pm
by va!n
This code should create and handle two windows. window1 is the main window and is visible.
When the user press the button "show", the second (hidden) window should be shown up.
When the user press the button "ok", the second window should be again hidden.

Any idea, whats wrong with my code?

Code: Select all



;   EnableExplicit
    
    Global wClass1.WNDCLASSEX;
    Global wClass2.WNDCLASSEX;
    
    Declare WindowCallback_Window1(Window, Message, wParam, lParam); 
    Declare WindowCallback_Window2(Window, Message, wParam, lParam);  
    
    Define hInst;
    
    Global hWnd1;
    Global hWnd2;
    
    Define msg.MSG;
    
   
;// ------------------------------------------------------------
;// Some MACROS
;// ------------------------------------------------------------      

    Macro LOWORD(dwValue) : dwValue & $FFFF : EndMacro;
    Macro HIWORD(dwValue) : dwValue >> 16 : EndMacro;
    
    Macro MAKELPARAM( loWord, hiWord) : (hiWord << 16 | loWord) : EndMacro;
    Macro MAKEWPARAM( loWord, hiWord) : (hiWord << 16 | loWord) : EndMacro;


;// ------------------------------------------------------------
;// Define all IDs
;// ------------------------------------------------------------      
    
Enumeration 200
    #IDC_WINDOW1_SHOW
    #IDC_WINDOW2_OK
EndEnumeration
    

;// ------------------------------------------------------------
;// MAIN 
;// ------------------------------------------------------------  

	;// First: We need to declarate WNDCLASSEX we want to use
	wClass1.WNDCLASSEX;
	wClass2.WNDCLASSEX;
	
	;// Clear them
    ZeroMemory_(wClass1, SizeOf(WNDCLASSEX));
	ZeroMemory_(wClass2, SizeOf(WNDCLASSEX));
	
	;// Setup them
	
	;// first one
	wClass1\cbSize          = SizeOf(WNDCLASSEX);
	wClass1\style           = #CS_HREDRAW | #CS_VREDRAW;
	wClass1\lpfnWndProc     = @WindowCallback_Window1();
	wClass1\cbClsExtra      = #Null;
	wClass1\cbWndExtra      = #Null;
	wClass1\hInstance       = hInst;
	wClass1\hIcon           = #Null;
	wClass1\hCursor         = LoadCursor_(#Null, #IDC_ARROW);
	wClass1\hbrBackground   = #COLOR_WINDOW;
	wClass1\lpszMenuName    = #Null;
	wClass1\lpszClassName   = @"Window Class1";
	wClass1\hIconSm         = #Null;
	
	;// Second one
	wClass2\cbSize          = SizeOf(WNDCLASSEX);
	wClass2\style           = #CS_HREDRAW | #CS_VREDRAW;
	wClass2\lpfnWndProc     = @WindowCallback_Window2();
	wClass2\cbClsExtra      = #Null;
	wClass2\cbWndExtra      = #Null;
	wClass2\hInstance       = hInst;
	wClass2\hIcon           = #Null;
	wClass2\hCursor         = LoadCursor_(#Null, #IDC_ARROW);
	wClass2\hbrBackground   = #COLOR_WINDOW;
	wClass2\lpszMenuName    = #Null;
	wClass2\lpszClassName   = @"Window Class2";
	wClass2\hIconSm         = #Null;
	
	
	RegisterClassEx_( @wClass1 );
	RegisterClassEx_( @wClass2 );
	
	;//Second: register them
; 	If(!RegisterClassEx(&wClass1)){
; 		MessageBox(NULL,"Could not register class1","Window Class Failed",MB_ICONERROR);
; 	}
; 	If(!RegisterClassEx(&wClass2)){
; 		MessageBox(NULL,"Could not register class2","Window Class Failed",MB_ICONERROR);
; 	}


    #Style      = #WS_VISIBLE |  #WS_SYSMENU | #WS_MAXIMIZEBOX | #WS_MINIMIZEBOX | #WS_SIZEBOX
;   #StyleEx    = #WS_EX_OVERLAPPEDWINDOW 


 	;//Now we can proceed With window creation
	hWnd1 = CreateWindowEx_( #WS_EX_OVERLAPPEDWINDOW ,"Window Class1", "Window 1", #Style,100,100,800,600,#Null,#Null,GetModuleHandle_(0),#Null);
	hWnd2 = CreateWindowEx_( #WS_EX_TOPMOST ,"Window Class2", "Window 2", #WS_VISIBLE | #WS_POPUPWINDOW	,100,100,500,279,hWnd2,#Null,hInst,#Null);
		
    SetClassLongPtr_(hWnd2,#GCL_STYLE,$00020000) ; Add Shadow Effect
   	    
	;//Check If could create both window
; 	If(!window1){
; 		MessageBox(NULL,"Could not create window1","Window Creation Failed",MB_ICONERROR);
; 	}
; 	If(!window2){
; 		MessageBox(NULL,"Could not create window2","Window Creation Failed",MB_ICONERROR);
; 	}

	;//Now we can show some windows
	ShowWindow_( hWnd1,  #SW_SHOW );
	ShowWindow_( hWnd2, #SW_HIDE );
	
	;//Last Step: handle message
	msg.MSG;
    ZeroMemory_(msg, SizeOf(MSG));
    
	While(GetMessage_( msg, #Null, 0, 0)) 
		TranslateMessage_( msg );
		DispatchMessage_( msg );
	Wend
	
End	


;// ------------------------------------------------------------
;//  WINDOWCALLBACK - HANDLING ALL EVENTS  
;// ------------------------------------------------------------       
	
Procedure WindowCallback_Window1(Window, Message, wParam, lParam) 
    Protected Result
    
    Select Message 
            
        Case #WM_COMMAND            
            Select LOWORD(wParam)
                Case #IDC_WINDOW1_SHOW
                    ShowWindow_( hWnd2, #SW_SHOW)
                    
            EndSelect
            
            
        Case #WM_CREATE
            hBtnShow = CreateWindowEx_(0, "button", "Show 2nd window", #WS_CHILD | #WS_VISIBLE | #WS_BORDER, 10, 10, 400, 80, Window, #IDC_WINDOW2_OK, 0, 0); 
    
            
            
        Case #WM_CLOSE             
            
            ;     UnregisterClass_(Appname,hInstance)  ??
            
            If MessageBox_(Window, "Exit program?", "EXIT", #MB_YESNO) = #IDYES 
                DestroyWindow_(Window)                 
            Else 
                Result  = 0 
            EndIf 
            
        Case #WM_DESTROY            
            PostQuitMessage_(0)
            
        Default 
            Result  = DefWindowProc_(Window, Message, wParam, lParam) 
    EndSelect 
    
    ProcedureReturn Result 
    
EndProcedure


Procedure WindowCallback_Window2(Window, Message, wParam, lParam) 
    Protected Result
    Protected hBtnOK  
    
    Select Message 
            
        Case #WM_CREATE
            hBtnOK = CreateWindowEx_(0, "button", "OK", #WS_CHILD | #WS_VISIBLE | #WS_BORDER, 10, 10, 100, 25, hWnd2, #IDC_WINDOW2_OK, 0, 0); 
            
           
        Case #WM_COMMAND            
            Select LOWORD(wParam)
                Case #IDC_WINDOW2_OK
                    ShowWindow_( hWnd2, #SW_HIDE )
                    
            EndSelect            
            
        Case #WM_CLOSE              
            DestroyWindow_(Window) 
                        
        Case #WM_DESTROY            
            
        Default                     
            Result = DefWindowProc_(Window, Message, wParam, lParam) 
            
    EndSelect 
    
    ProcedureReturn Result 
    
EndProcedure

Re: Win32 - Handling two windows (Whats wrong with this code

Posted: Mon Feb 17, 2014 5:47 pm
by TI-994A
va!n wrote:Any idea, whats wrong with my code?

Code: Select all

Procedure WindowCallback_Window1(Window, Message, wParam, lParam) 
    Protected Result
    
    Select Message 
            
        Case #WM_COMMAND            
            Select LOWORD(wParam)
                Case #IDC_WINDOW1_SHOW
                    ShowWindow_( hWnd2, #SW_SHOW)
                    
            EndSelect
            
            
        Case #WM_CREATE
            hBtnShow = CreateWindowEx_(0, "button", "Show 2nd window", #WS_CHILD | #WS_VISIBLE | #WS_BORDER, 10, 10, 400, 80, Window, #IDC_WINDOW2_OK, 0, 0); 
Hi va!n. Firstly, you've mislabelled the button in the first window callback; I believe that the button should be #IDC_WINDOW1_SHOW and not #IDC_WINDOW2_OK.

Code: Select all

Procedure WindowCallback_Window2(Window, Message, wParam, lParam) 
    Protected Result
    Protected hBtnOK  
    
    Select Message 
            
        Case #WM_CREATE
            hBtnOK = CreateWindowEx_(0, "button", "OK", #WS_CHILD | #WS_VISIBLE | #WS_BORDER, 10, 10, 100, 25, hWnd2, #IDC_WINDOW2_OK, 0, 0);
And secondly, in the second window callback, the parent window parameter in the button creation should be Window and not hWnd2.

Change these two, and it works fine. :)

Re: Win32 - Handling two windows (Whats wrong with this code

Posted: Mon Feb 17, 2014 7:22 pm
by va!n
Arghhhh, you are absolutly right. Thanks for your help ;)