📄 example13-1.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>例13-1</title>
</head>
<body>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">程序代码如下:</font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
<windows.h></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
<stdlib.h></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#define
BUFFER(x,y) *(pBuffer + y * cxBuffer + x)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">LRESULT
CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int
WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">PSTR
szCmdLine, int iCmdShow)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static
char szAppName[] = "Typer" ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">HWND
hwnd ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">MSG
msg ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">WNDCLASSEX
wndclass ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbSize
= sizeof (wndclass) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.style
= CS_HREDRAW | CS_VREDRAW ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpfnWndProc
= WndProc ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbClsExtra
= 0 ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbWndExtra
= 0 ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hInstance
= hInstance ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hIcon
= LoadIcon (NULL, IDI_APPLICATION) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hCursor
= LoadCursor (NULL, IDC_ARROW) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hbrBackground
= (HBRUSH) GetStockObject (WHITE_BRUSH) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpszMenuName
= NULL ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpszClassName
= szAppName ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hIconSm
= LoadIcon (NULL, IDI_APPLICATION) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">RegisterClassEx
(&wndclass) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">hwnd
= CreateWindow (szAppName, "<font FACE="??ì?,SimSun" LANG="ZH-CN">键盘输入示例</font>",
WS_OVERLAPPEDWINDOW,</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">NULL,
NULL, hInstance, NULL) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ShowWindow
(hwnd, iCmdShow) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">UpdateWindow
(hwnd) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">while
(GetMessage (&msg, NULL, 0, 0))</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">TranslateMessage
(&msg) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DispatchMessage
(&msg) ;</font></p>
</blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">return
msg.wParam ;</font></p>
</blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">LRESULT
CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static
char *pBuffer = NULL ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static
int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">xCaret,
yCaret ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">HDC
hdc ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int
x, y, i ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">PAINTSTRUCT
ps ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">TEXTMETRIC
tm ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">switch
(iMsg)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">case
WM_CREATE :</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">hdc
= GetDC (hwnd) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">SelectObject
(hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">GetTextMetrics
(hdc, &tm) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cxChar
= tm.tmAveCharWidth ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cyChar
= tm.tmHeight ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ReleaseDC
(hwnd, hdc) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">return
0 ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">case
WM_SIZE :</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cxClient
= LOWORD (lParam) ; // <font FACE="??ì?,SimSun" LANG="ZH-CN">获取窗口大小</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cyClient
= HIWORD (lParam) ;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cxBuffer
= max (1, cxClient / cxChar) ; //<font FACE="??ì?,SimSun" LANG="ZH-CN">计算窗口所能容纳的字符数</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cyBuffer
= max (1, cyClient / cyChar) ;</font></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -