Advertisement

WM_CHAR working right?

Started by March 12, 2002 09:02 PM
6 comments, last by Lohrno 22 years, 6 months ago
Hi, I think I''m misusing WM_CHAR somehow, as this program does not seem to work. I was trying to make a simple program that displays the key you pressed in the GDI, but it doesnt seem to work:
  
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

HWND g_hwnd=0;

char c[100] = "This is a test";
PAINTSTRUCT PaintStruct;
HDC hDC;
RECT rect;

long CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
//	c[0]=''a'';

 switch (uMessage)
 {
 case WM_CHAR:
 {
 	c[1]=wParam;       
	return 0;

 }	 
 case WM_PAINT:
	 {
		 hDC=BeginPaint(hWnd,&PaintStruct);
         GetClientRect(hWnd,▭);
		 TextOut(hDC,10,10,c,5);
		 EndPaint(hWnd, &PaintStruct);
	    
	 return 0;
	 }
 case WM_DESTROY:
	 {
		 PostQuitMessage(2);
		 return 0;
	 }
 case WM_QUIT:
	 {
	  PostQuitMessage(2);
	  return 0;
	 }
 case WM_CLOSE:
	 {
	  PostQuitMessage(2);
	 }


  default: 
	 {
		 return DefWindowProc(hWnd, uMessage, wParam, lParam);
	 }
 }

}


HWND MakeWindowClass(WNDCLASSEX wc, char* name, HINSTANCE hinst,int x,int y,int x2,int y2)
{
	wc.cbSize = sizeof(WNDCLASSEX); // tell window how big it is

	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // set how new window will react to events.

	wc.cbClsExtra = 0;  //stuff we can ignore

	wc.cbWndExtra = 0;  // more stuff we can ignore

	wc.lpfnWndProc = WndProc; // the function to call for processing the messageloop of this window

	wc.hInstance = hinst;  // the handle to this instance of this application

	wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);  //for app icon

	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //also for app icon

	wc.hCursor = LoadCursor (NULL, IDC_ARROW);  // set to standard cursor when mouse is over our window

	wc.lpszMenuName = NULL; // No stinkin menus

    wc.lpszClassName = name; // The name of this class

    //fill in done

	RegisterClassEx(&wc);  //Registers our new window (always need to register new windows)


	return CreateWindowEx(NULL, name, name, WS_EX_TOPMOST, x, y, x2, y2, NULL, NULL, hinst, NULL);
	
 	//CreateWindowEx(

}

HWND MakeWindow(int x, int y, int x2, int y2, char* name, HINSTANCE hinst,WNDCLASSEX wc, int numcmd)
{
	if (x2==0) x2=GetSystemMetrics(SM_CXSCREEN);
	if (y2==0) y2=GetSystemMetrics(SM_CYSCREEN);
	HWND hwind = MakeWindowClass(wc, name, hinst, x,y,x2,y2);
	ShowWindow(hwind, numcmd);  //Shows window (hopefully)

    UpdateWindow(hwind); // Updates the window

    
	return hwind;
}

int APIENTRY WinMain(HINSTANCE hinst,HINSTANCE hprev,LPSTR lpcmd,int numcmd)
{

	MSG msg;
	WNDCLASSEX wc;
    g_hwnd = MakeWindow(100,100,300,300,"The name of this app", hinst, wc, numcmd);
    
  
	while (1)
	{
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT) break;
				TranslateMessage(&msg);  //Message pump

        		DispatchMessage(&msg);


		}
    	if (GetAsyncKeyState(VK_ESCAPE)) PostQuitMessage(0);		    
		UpdateWindow(g_hwnd);
	}
return msg.wParam;
}

  
I''m not sure if it''s not setting c right, or if its not drawing it right...am I using WM_CHAR right? or have I done something totally wacky with it? =( -=Lohrno
I would help if you''d tell what it *is* doing...


Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Advertisement
Oh yeah...I guess that''d be helpful =D...well it displays a window that says in it:

"This"

Even when I press a character, it doesnt change h to whatever the key pressed was.

-=Lohrno
You could *try* casting wParam (which is a long) to a char, but I doubt that''d help. Otherwise, I''m stumped, unless you''re not redrawing the text at all.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

I have the text redrawn in WM_PAINT which should redraw it every time windows thinks it needs to be redrawn right?

-=Lohrno
The only time Windows will think your program needs to be redrawn is if another window is moved across it or part of the window is moved off or onto the screen or something like that. Just because you change the value of a variable somewhere doesn't mean Windows will automatically update the window screen. If you minimize your window and then restore it, you might see the change.

Use the InvalidateRect function to update your window.

[edited by - SilentCoder on March 13, 2002 4:03:29 PM]
Advertisement
Ahhhh......That would explain a lot! =D
Heh I already had it working right in my DX program last night though! =D But thats always good info to know! =D

-=Lohrno
Ahhhh......That would explain a lot! =D
Heh I already had it working right in my DX program last night though! =D But thats always good info to know! =D

-=Lohrno

This topic is closed to new replies.

Advertisement