Thread: Opening Windows from Menus

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    Opening Windows from Menus

    Okay, I'm learning basic Windows Programming on a very steep learning curve due to a totally over optimistic networking assignment for university. I've struck upon a problem not covered in the texts I have to hand and really needsome guidance to getting this one of the ground.

    I've typed out my code for the WINMAIN function, created a menu for it, and set up the WinProc for processing the messages, but I've got a problem with the menu.

    Menu works fine and all but I have the File Menu with the options "Login" and "Register", these should open a window with a couple of EditBoxes and a button, the problem is that I can't get my head around how to create the Windows for it. Since the parent window is initialised and created in WinMain I'm not sure I can create another function to open the windows.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Put the parent window handle in a global variable so that it can be accessed by other functions.

    gg

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    I'll try that asap.

    Cheers for the advice.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    When the menu item is clicked it will sent a WM_COMMAND to the callback containing a BN_CLICKED msg. The first param of the callback is the HWND.

    Use this HWND to open the new window.

    OR

    Open all the windows at init and hide them all. (use ShowWindow() )
    When the menu item is clicked show the correct dialog.
    Hide it again when the user closes it.
    "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
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    Okay I'm a dolt because I've tried various methods so I'll post the code and hope I can get specific help. As you can hopefully see, the current state of play is that the Menu button being pressed access a function, the function (not here) does all the windows crap for the other window (creates the class, initializes, registers so on) I'm guessing this is not a good way to do it because I'm having problems with my instance handle for that window.



    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>		//For windows functions
    #include <windowsx.h>		//related macros
    #include <stdio.h>
    #include <math.h>
    #include "Resource.h"		//Resources, menus etc.
    
    // GLOBAL VARIABLES 
    
    
    
    HWND hwnd;
    
    
    
    // FUNCTION PROTOTYPES 
    
    
    LRESULT CALLBACK WindowProc(HWND hwnd,
    UINT msg,
    WPARAM wparam,
    LPARAM lparam);
    
    
    
    
    int Login_Window();
    
    
    // WINMAIN 
    
    /*This is the WinMain function in which the main window is initiated and created*/
    
    
    int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance, LPSTR lpcmdline, int ncmdshow)
    {
    
    	//------------------------------- VARIABLES ------------------
    	//------------------------------------------------------------
    WNDCLASSEX winclass;	//Blank Windows class
    MSG msg;				//Storage for messages
    
    //--------------------- WINDOW CLASS DEFINITION & REGISTRATION
    //------------------------------------------------------------
    winclass.cbSize = sizeof(WNDCLASSEX);			
    winclass.style = CS_VREDRAW |
    CS_HREDRAW |
    CS_OWNDC |
    CS_DBLCLKS;
    winclass.lpfnWndProc = WindowProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
    winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    winclass.lpszMenuName = MAKEINTRESOURCE(MainMenu);
    winclass.lpszClassName = "BIGWINDOW";
    winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    RegisterClassEx(&winclass);
    					
    //---------------------------------- WINDOW CREATION----------
    
    if( ! (hwnd = CreateWindowEx( NULL,
    "BIGWINDOW","Draughts",
    WS_OVERLAPPEDWINDOW |
    WS_VISIBLE,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    450,450,
    NULL,
    LoadMenu(hinstance, 
    MAKEINTRESOURCE(MainMenu)),hinstance,NULL)))
    
    return(0);
    
    
    	//---------------------------------- MAIN EVENT LOOP ---------
    
    	while(true)
    	{
    		if(PeekMessage(  					
    		&msg,								
    		NULL,						
    		0,						
    		0,						
    		PM_REMOVE))						
    		{
    			if (msg.message == WM_QUIT)
    				break;
    			TranslateMessage(&msg);	
    			DispatchMessage(&msg);	
    		}				
    	}										
    
    	return(0);
    
    }  //End WINMAIN
    
    
    // WINPROC FUNCTION 
    
    /*This function is the WinProc function which handles all the action messages that
    are sent from the window by the user interacting.  It then takes the corresponding
    action to the message (e.g. when the "exit" option is selected, closes the window)*/
    
    //Function to process messages sent to the window
    LRESULT CALLBACK WindowProc(HWND hwnd,			
    							UINT msg,		
    							WPARAM wparam,	
    							LPARAM lparam)		
    {
    	PAINTSTRUCT		ps;			
    	HDC				hdc;
    
    	switch(msg)				
    	{
    		
    		//--------INITIALISATION--------//
    		case WM_CREATE:			
    		{
    			return(0);	
    		} break;		
    
    		
    		//--------MENU COMMANDS--------//
    		case WM_COMMAND:				
    		{
    
    			switch(LOWORD(wparam))		
    			{
    
    				//FILE MENU
    				case MENU_FILE_ID_LOGIN:	
    				{
    					Login_Window();
    					//PostQuitMessage(0);
    				} break;
    
    				
    				case MENU_FILE_ID_REGISTER:		
    				{
    					PostQuitMessage(0);		
    				} break;
    
    
    				case MENU_FILE_ID_EXIT:			
    				{
    					PostQuitMessage(0);	
    				} break;
    
    				
    				//HELP MENU
    				case MENU_HELP_ID_ABOUT:		
    				{	
    MessageBox(NULL,"Draughts v1.0","Draughts Information",MB_OK | MB_ICONEXCLAMATION);
    				}
    			}			
    		}				
    
    		//--------WINDOW REPAINT--------//
    		case WM_PAINT:			
    		{
    			hdc = BeginPaint(hwnd,&ps); 
    			EndPaint(hwnd,&ps);
    			return(0);			
    		} break;				
    		
    		//--------EXIT COMMAND--------/
    		case WM_DESTROY:		
    		{
    			PostQuitMessage(0);	
    			return(0);		
    		} break;				
    		default: break;			
    	}
    	return (DefWindowProc(hwnd, msg, wparam, lparam));
    
    }	//End WinProc
    Last edited by Goonlecker; 11-21-2003 at 01:20 PM.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    I've tried to make that code as readable as possible.

    My only decent reference to hand is LaMothe's "Tricks of the Windows.." and "Programming Windows 5th Ed" by Petzold.

    Basically what I've gone on is what Lamothe said:

    if you want a different WinProc for each window you must create more than one Windows class and create each window with a different class.
    That's what I did. My problem is that calling up the function to open the second class seems to create a problem. It opens the window fine and all but the window takes two clicks to close and that shuts both windows. I tried altering this using the hide function but I've still got the background problem of taking two clicks to close the main window. I'm guessing this has something to do with the messages being passed to the right WinProc. I thought it might be because one, essentially runs out of the other due to the window function opening from there.

    Ack.. I can't handle this headache... <headbutts keyboard>

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Is this a realtime game?

    Otherwise don't use PeekMessage() as this will use 100% CPU.

    GetMessage() waits for a msg, PeekMessage() returns and will be called again and again.

    Your code looks good apart from that. No second wnd class registered. You are not trying to send all msg's thru the same wndproc?

    Each dialog needs a wndproc. Dialogs made in the resource editor do not need to be registered.

    Is your message pump catching the WM_QUIT from the logon screen and quiting the whole app?

    Code:
    //dialog filter
    if (FALSE == IsDialogMessage(hDlg, &msg))
    {
    	TranslateMessage(&msg) ;
    	DispatchMessage(&msg) ;
    }
    
    //normal app loop, not checking for error in GetMessage() ie 
    while (GetMessage(&msg, (HWND) NULL, 0, 0)) 
    { 
    			if(!TranslateAccelerator(msg.hwnd, hAccel, &msg))
    			{ 
    				TranslateMessage(&msg); 
    				DispatchMessage(&msg); 
    			} 
    }

    send in a HWND to the log on screen, use the first param of the callback. Create the log on with this as its parent.

    Login_Window(hwnd);

    make the HINSTANCE global or use ......bugger I have forgotten the name of the function ..



    As it is a logon screen I would use a modal screen. No other processing can take place until this screen is dealt with.
    look at

    DialogBox()
    "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

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    Cheers for the advice. I'd set up the individual winprocs for all the different windows I was trying to open but somewhere along the line the quit messages were not registering properly because they were not in the right place. I sorted the problem after a bit of fiddling an it all works fine now.

    Now the world of hurt that is opening a Bitmap multiple times in the window.

    Does this eer get any easier???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone using Windows 7?
    By Sharke in forum General Discussions
    Replies: 60
    Last Post: 07-12-2009, 08:05 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. How come this only works in Windows nt/2000?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2002, 06:54 PM