-
July 16th, 2011, 05:52 AM
#1
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
-
July 16th, 2011, 07:05 AM
#2
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
-
July 16th, 2011, 11:38 AM
#3
Re: Virtual Key Codes
I call it to provide default processing for any window messages that my application does not process.
-
July 16th, 2011, 12:49 PM
#4
Re: Virtual Key Codes
Originally Posted by TpOreilly
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
-
July 16th, 2011, 02:17 PM
#5
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.
-
July 16th, 2011, 02:32 PM
#6
Re: Virtual Key Codes
It doesn't matter.
if you have processed this message you have to return 0. Period!
Victor Nijegorodov
-
July 16th, 2011, 02:45 PM
#7
Re: Virtual Key Codes
Originally Posted by TpOreilly
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
-
July 16th, 2011, 02:47 PM
#8
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.
-
July 16th, 2011, 02:55 PM
#9
Re: Virtual Key Codes
Originally Posted by TpOreilly
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
-
July 16th, 2011, 03:23 PM
#10
-
July 16th, 2011, 03:39 PM
#11
Re: Virtual Key Codes
Originally Posted by TpOreilly
Returned 0.
Where?
Victor Nijegorodov
-
July 16th, 2011, 03:42 PM
#12
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);
}
-
July 17th, 2011, 02:27 AM
#13
Re: Virtual Key Codes
Originally Posted by TpOreilly
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
-
July 17th, 2011, 07:58 AM
#14
Re: Virtual Key Codes
I have added the .
Before I added that it did actually compile :P
Also, i still have the same problem.
-
July 17th, 2011, 08:02 AM
#15
Re: Virtual Key Codes
Originally Posted by TpOreilly
I have added the .
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|