CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Virtual Key Codes

    Hi, here is my windows procedure:

    LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

    {

    static int X = 250;
    static int Y = 250;
    HINSTANCE hInstance;
    hInstance = LoadLibrary (TEXT("Application01.exe"));

    switch(message)

    {
    case WM_CREATE:

    int MessageBoxID;
    MessageBoxID = MessageBox(hWnd, TEXT(" Are ya ready kids?"), TEXT("Are ya ready kids?"),
    MB_ICONQUESTION | MB_YESNO);

    if (MessageBoxID == IDNO)
    {
    PostQuitMessage(0);
    }

    if (MessageBoxID == IDYES)
    {
    UpdateWindow(hWnd);
    }

    return FALSE;



    case WM_PAINT:

    HICON hIcon;
    hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    HDC handleDeviceContext;
    PAINTSTRUCT PaintSt;
    handleDeviceContext = BeginPaint(hWnd, &PaintSt);
    DrawIcon(handleDeviceContext, X, Y, hIcon);
    EndPaint(hWnd, &PaintSt);
    break;


    case WM_KEYDOWN:

    switch(wParam)

    case VK_LEFT:
    {
    X -= 5;
    InvalidateRect(hWnd, NULL, TRUE);
    break;
    }
    case VK_RIGHT:
    {
    X += 5;
    InvalidateRect(hWnd, NULL, TRUE);
    break;
    }

    break;

    case WM_DESTROY:
    PostQuitMessage(0);
    break;


    }


    return DefWindowProc (hWnd, message, wParam, lParam);

    }



    The X and Y variables are integers which are used here:

    DrawIcon(handleDeviceContext, X, Y, hIcon);

    So the X and Y are the location of the icon.

    The problem I am having is that when i press the left arrow key, nothing happens, and when i press the right arrow key it DOES go right, but it also goes right when i press the up and down kep :S

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Did you debug your code top be sure WM_KEYDOWN is processed for both VK_LEFT and VK_RIGHT?
    Besides, why do you call DefWindowProc if you want to process this message in your own procedure? From MSDN:
    WM_KEYDOWN Message
    ...
    Return Value

    An application should return zero if it processes this message.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    I call it to provide default processing for any window messages that my application does not process.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    I call it to provide default processing for any window messages that my application does not process.
    But your application does process WM_KEYDOWN message when either VK_LEFT or VK_RIGHT code was supplied!
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    I think i might be confused lol...

    I havnt got "return DefWindowProc (hWnd, message, wParam, lParam);" in the case of the WM_KEYDOWN message, i have "DefWindwProc" right at the very end on the windows procedure.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    It doesn't matter.
    if you have processed this message you have to return 0. Period!
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    Hi, here is my windows procedure:
    Over 60 posts and you don't know how to use code tags?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    Oh ok.

    I have done what you said, but that results in my window not showing when i run the application. It doesnt show any errors either.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    I have done what you said, but that results in my window not showing when i run the application. It doesnt show any errors either.
    What did you do? And how?
    Victor Nijegorodov

  10. #10
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    Returned 0.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    Returned 0.

    Where?
    Victor Nijegorodov

  12. #12
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    Start again As I am confused, could you correct my mistakes in my original code below and re-post it please

    Code:
    LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static int X = 250;
    	static int Y = 250;
    	HINSTANCE hInstance;
    	hInstance = LoadLibrary (TEXT("Application01.exe"));
    
    	switch(message)
    	{
    		case WM_CREATE:
    
    			int MessageBoxID;
    			MessageBoxID = MessageBox(hWnd, TEXT("         Are ya ready kids?"), TEXT("Are ya ready kids?"),
                MB_ICONQUESTION | MB_YESNO);
    
    			if (MessageBoxID == IDNO)
    			{
    				PostQuitMessage(0);
    			}
    
    			if (MessageBoxID == IDYES)
    			{
    				UpdateWindow(hWnd);
    			}
    
    			return FALSE;
    
    			
    
    		case WM_PAINT:
    				
    				HICON hIcon;
    				hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    				HDC handleDeviceContext; 
    				PAINTSTRUCT PaintSt;
    				handleDeviceContext = BeginPaint(hWnd, &PaintSt);
    				DrawIcon(handleDeviceContext, X, Y, hIcon);
    				EndPaint(hWnd, &PaintSt);
    				break;
    			
    
    		case WM_KEYDOWN:
    
    			switch(wParam)
    			
    				case VK_LEFT:
    			{
    					X -= 5;
    					InvalidateRect(hWnd, NULL, TRUE);
    					break;
    			}
    				case VK_RIGHT:
    			{
    					X += 5;
    					InvalidateRect(hWnd, NULL, TRUE);
    					break;
    			}
    
    					
    
    
    
    		case WM_DESTROY:
                PostQuitMessage(0);
    			break; 
    		
    
    		
    	}
    
    
    			return DefWindowProc (hWnd, message, wParam, lParam);
    	
    }

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    Start again As I am confused, could you correct my mistakes in my original code below and re-post it please

    Code:
    LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static int X = 250;
    	static int Y = 250;
    	HINSTANCE hInstance;
    	hInstance = LoadLibrary (TEXT("Application01.exe"));
    
    	switch(message)
    	{
    		....
    		case WM_KEYDOWN:
    			switch(wParam)
    				case VK_LEFT:
    			{
    					X -= 5;
    					InvalidateRect(hWnd, NULL, TRUE);
    					break;
    			}
    				case VK_RIGHT:
    			{
    					X += 5;
    					InvalidateRect(hWnd, NULL, TRUE);
    					break;
    			}
    
    		case WM_DESTROY:
                PostQuitMessage(0);
    			break; 
    	}
    			return DefWindowProc (hWnd, message, wParam, lParam);
    }
    Does it compile?
    Note also that your case WM_KEYDOWN does not have break, so after each WM_KEYDOWN this code would call PostQuitMessage(0) and app would exit. So I wonder how it could "work" at all!
    Victor Nijegorodov

  14. #14
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Re: Virtual Key Codes

    I have added the
    Code:
    break;
    .

    Before I added that it did actually compile :P

    Also, i still have the same problem.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Virtual Key Codes

    Quote Originally Posted by TpOreilly View Post
    I have added the
    Code:
    break;
    .

    Before I added that it did actually compile :P

    Also, i still have the same problem.
    Because if you have processed WM_KEYDOWN message you have to return 0, what you don't do!
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured