#include <windows.h>
#include<strsafe.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("hello"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
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 cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
static int cLinesMax, cLines;//客户区最大化可以容纳的行数,当前申请的内存块里面有的消息行数
static PMSG pmsg;
static RECT RectScroll;
static TCHAR szTop[] = TEXT("Message Key Char Repeat Scan Ext Alt Prev Tran");
static TCHAR szUnd[] = TEXT("_______ ___ ___ ______ ____ ___ ___ ____ ____");
static TCHAR* szFormat[2] = { TEXT("%-13s %3d %-15s%c%6u %4d %3s %4s %4s"),
TEXT("%-13s 0x%04X%1s%c %6u %4d %3s %3s %4s %4s")};
static TCHAR* szYes = TEXT("Yes");
static TCHAR* szNo = TEXT("No");
static TCHAR* szDown = TEXT("Down");
static TCHAR* szUp = TEXT("Up");
static TCHAR * szMessage[] = { TEXT("WM_KEYDONE"),TEXT("WM_KEYUP"),TEXT("WM_CHAR"),TEXT("WM_DEADCHAR"),
TEXT("WM_SYSKEYDONE"),TEXT("WM_SYSKEYUP"),TEXT("WM_SYSCHAR"),TEXT("WM_SYSDEADCHAR") };
HDC hdc;
int iType;
size_t iTarget;
PAINTSTRUCT ps;
TCHAR szBuffer[128], szKeyName[32];
TEXTMETRIC tm;
int LinesTemp;//当前需要显示的行数
switch (message)
{
case WM_CREATE:
//不管,继续执行
case WM_DISPLAYCHANGE:
//客户区最大化或者最小化会产生这个消息
cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);
hdc = GetDC(hwnd);
//设置等宽字体
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight;
ReleaseDC(hwnd, hdc);
if (pmsg)
{
free(pmsg);
}
//计算客户区最大高度可以容纳多少行
cLinesMax = cyClientMax / cyChar;
//申请一块内存,大小为客户区最大高度可以容纳的行数*一行多大(MSG类型占用多大空间)
pmsg = (PMSG)malloc( cLinesMax * sizeof(MSG));
cLines = 0;
//如果内存申请失败
if (pmsg == NULL)
{
MessageBox(hwnd, TEXT("内存申请失败"), TEXT("Error"), MB_OK);
PostQuitMessage(0);
}
case WM_SIZE:
if (message == WM_SIZE)
{
cxClient = LOWORD(lparam);
cyClient = HIWORD(lparam);
}
//计算滚动的矩形
RectScroll.left = 0;
RectScroll.right = cxClient;
RectScroll.top = cyChar;
RectScroll.bottom = cyClient;
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
//这几个消息都这么干
for (int i = cLinesMax - 1; i > 0; i--)
{
pmsg[i] = pmsg[i - 1];
}
pmsg[0].hwnd = hwnd;
pmsg[0].message = message;
pmsg[0].lParam = lparam;
pmsg[0].wParam = wparam;
//cLines这个变量的作用很难解释...
cLines = min(cLines + 1, cLinesMax);
ScrollWindow(hwnd, 0, -cyChar, & ScrollWindow, &ScrollWindow);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
//设置背景为透明色
SetBkMode(hdc, TRANSPARENT);
StringCchLength(szTop, 1024, &iTarget);
TextOut(hdc, 0, 0, szTop, iTarget);
StringCchLength(szUnd, 1024, &iTarget);
TextOut(hdc, 0, 0, szUnd, iTarget);
//当前需要显示的行数 = 当前内存块里面有的消息行数 和 当前客户区最多可以容纳的行数 中的小的那个,减一是因为最上面那一行显示的有文字
LinesTemp = min(cLines, cyClient / cyChar - 1);
for (size_t i = 0; i < min(cLines, cyClient / cyChar - 1); i++)
{
iType = (pmsg[i].message == WM_CHAR || pmsg[i].message == WM_SYSCHAR || pmsg[i].message == WM_DEADCHAR || pmsg[i].message == WM_SYSDEADCHAR);
GetKeyNameText(pmsg[i].lParam, szKeyName, 32);
StringCchPrintf(szBuffer, sizeof(szBuffer), szFormat[iType],
szMessage[pmsg[i].message - WM_KEYFIRST],
pmsg[i].wParam,
(PTSTR)(iType ? TEXT("") : szKeyName),
(TCHAR)(iType ? pmsg[i].wParam : ' '),
LOWORD(pmsg[i].lParam),
HIWORD(pmsg[i].lParam) & 0xff,
0x01000000 & pmsg[i].lParam ? szYes : szNo,
0x20000000 & pmsg[i].lParam ? szYes : szNo,
0x40000000 & pmsg[i].lParam ? szDown : szUp,
0x80000000 & pmsg[i].lParam ? szUp : szDown);
StringCchLength(szBuffer, sizeof(szBuffer), &iTarget);
TextOut(hdc, 0, (cyClient / cyChar - 1 - i)*cyChar, szBuffer, iTarget);
TextOut(hdc, 100, 100, TEXT("kkkk"), 4);
TextOut(hdc, 100, 100, TEXT("kkkk"), 4);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}