无法在输出窗口上打印输入字符 [英] Unable to print input characters on output window

查看:96
本文介绍了无法在输出窗口上打印输入字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


尊敬的老年人,
我正在尝试使用sendmessage函数在输出窗口上打印通过键盘输入的字符.假设我按了一个键"a",则在输出窗口上它应该显示按键被按下" a"消息.但是我看不到.您能否建议我一种通过修改现有代码来按需打印的方法.


Dear Seniors,
I am trying to print the characters entered through key board on output window using sendmessage function. As soon as i hit a key suppose ''a'', on output window it should display "Key pressed ''a''" message. But i dont see it. Can you please suggest me a way to print it as desired by modifying the existing code.

#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
				    PSTR szCmdLine, int iCmdShow )
{
	HWND		hwnd;
	WNDCLASS	wc;
	MSG			msg;
	TCHAR		szAppName[] = TEXT("MyWin");

	wc.style   		 = CS_HREDRAW | CS_VREDRAW;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.hIcon		 = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor		 = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = ( HBRUSH ) GetStockObject( WHITE_BRUSH );
	wc.lpszMenuName	 = NULL;
	wc.lpszClassName = szAppName;
	wc.hInstance	 = hInstance;

	if( !RegisterClass( &wc ) )
	{
		MessageBox( NULL, TEXT("Class registration failed!"), szAppName, MB_ICONERROR );
		return 1;
	}

	hwnd = CreateWindow( szAppName,
						 szAppName,
						 WS_OVERLAPPEDWINDOW,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 NULL,
						 NULL,
						 hInstance,
						 NULL );

	ShowWindow( hwnd, iCmdShow );
	UpdateWindow( hwnd );

	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}

	return msg.wParam;
}


LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	static int			cyChar, cyClient, iPos;
	static TCHAR		szBuffer[50];
	HDC					hdc;
	PAINTSTRUCT			ps;
	TEXTMETRIC			tm;
	SCROLLINFO			si;

	switch( message )
	{
		case WM_CREATE:

			// get height of each character //
			hdc = GetDC( hwnd );
			GetTextMetrics( hdc, &tm );
			cyChar = tm.tmHeight + tm.tmExternalLeading ;
			ReleaseDC( hwnd, hdc );
		
			break;

		case WM_SIZE:
			
			cyClient = HIWORD( lParam );
			
			break;

		case WM_CHAR:

			if( wParam )
			{
				wsprintf( szBuffer, TEXT("Key pressed: %c"), wParam );
				SendMessage( hwnd, WM_PAINT, wParam, 0 );
				
			}

			break;

		case WM_PAINT:

			hdc = BeginPaint( hwnd, &ps );
			TextOut( hdc, 0, cyChar * iPos++ , szBuffer, lstrlen(szBuffer) );
			EndPaint( hwnd, &ps );

			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}

	return DefWindowProc( hwnd, message, wParam, lParam );
}


问候,
Kiran


Regards,
Kiran

推荐答案

您不应将WM_PAINT发送到窗口;如果要启动绘画操作,则应使用 InvalidateRect() [ ^ ]函数.

每次调用 BeginPaint时,您还需要保存需要显示的每一行. () [ ^ ],该窗口将被清除,您需要从窗口顶部重新绘制每一行.使用数组或向量保存文本行,然后在绘制过程中对其进行迭代,依次绘制每行.

如果行数超出了窗口可见部分的行数,则还需要添加代码来管理滚动.
You should not be sending a WM_PAINT to your window; if you want to initiate a paint operation you should use the InvalidateRect()[^] function.

You also need to save each line that needs to be displayed, as every time you call BeginPaint()[^] the window will get erased, and you need to repaint each line from the top of the window. Use an array or a vector to save your lines of text and then iterate through them in your paint procedure, painting each one in turn.

If you have more lines than will fit in the visible portion of your window you will also need to add code to manage scrolling.


您只需要处理WM_CHAR [基本的Win32示例 [
You only need to process WM_CHAR[^], the wParam contains the char code.

Take a look at this basic Win32 sample[^].
You can remove the WM_COMMAND, WM_LBUTTONDOWN, WM_RBUTTONDOWN.


这篇关于无法在输出窗口上打印输入字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆