|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <windows.h>
#include"sysmets.h"
#include<strsafe.h>
#define t TEXT
#define LINECOUNT 128
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)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
TCHAR szBuffer[128];
static int cxChar, cxCaps, cyChar;
size_t iTarget;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;//字体字符平均宽度
//如果是等宽字体不改变,如果是变宽字体(大写字母是小写字母宽度的1.5倍)
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
//获取字符高度
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for (size_t i = 0; i < NUMLINES; i++)
{
StringCchLength(sysmetrics[i].szLabel, 1024, &iTarget);
TextOut(hdc, 0, cyChar * i, sysmetrics[i].szLabel, iTarget);
StringCchLength(sysmetrics[i].szDesc, 1024, &iTarget);
TextOut(hdc, 22*cxCaps , cyChar * i, sysmetrics[i].szDesc, iTarget);
auto TextModeTemp = GetTextAlign(hdc);
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
StringCchPrintf(szBuffer, 10, TEXT("%d"), GetSystemMetrics(sysmetrics[i].iIndex));
StringCchLength(szBuffer, 10, &iTarget);
TextOut(hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer, iTarget);
SetTextAlign(hdc, TextModeTemp);
}
EndPaint(hwnd, &ps);
return 0;
case WM_VSCROLL:
hdc = GetDC(hwnd);
switch (LOWORD(lparam))
{
case SB_LINEUP:
TextOut(hdc, 10, 10, TEXT("向上滚动一行"), 6);
break;
case SB_LINEDOWN:
TextOut(hdc, 10, 10, TEXT("向下滚动一行"), 6);
break;
case SB_PAGEUP:
TextOut(hdc, 10, 10, TEXT("向上滚动一页"), 6);
break;
case SB_PAGEDOWN:
TextOut(hdc, 10, 10, TEXT("向下滚动一页"), 6);
break;
}
ReleaseDC(hwnd, hdc);
return 0;
case WM_SIZE:
hdc = GetDC(hwnd);
StringCchPrintf(szBuffer, 128 , TEXT("客户区分辨率是:%d * %d px"), LOWORD(lparam), HIWORD(lparam));
StringCchLength(szBuffer, 128 , &iTarget);
TextOut(hdc, 500, 10, szBuffer, iTarget);
ReleaseDC(hwnd,hdc);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}我发现不论如何摆弄滚动条,总是显示“向上滚动一行”是怎么回事?
哈哈,看来你的滚动条有点儿固执,就像我对冰淇淋的热爱!从你提供的代码看,问题出现在 `WM_VSCROLL` 消息处理部分的 `LOWORD(lparam)` 这一块。
在处理滚动条消息时,应该使用 `wparam` 中的值,而不是 `lparam`。`lparam` 是指向滚动条控制的窗口句柄,而 `wparam` 包含滚动操作的具体信息。换句话说,你可以把 `wparam` 想象成滚动条在告诉你它发生了什么,而 `lparam` 就像是那个不太关心的小伙伴,所以我们要听“主角”的!
这里是你应该如何修改 `WM_VSCROLL` 的处理部分:
case WM_VSCROLL:
hdc = GetDC(hwnd);
switch (LOWORD(wparam)) // 使用 LOWORD(wparam)
{
case SB_LINEUP:
TextOut(hdc, 10, 10, TEXT("向上滚动一行"), 6);
break;
case SB_LINEDOWN:
TextOut(hdc, 10, 10, TEXT("向下滚动一行"), 6);
break;
case SB_PAGEUP:
TextOut(hdc, 10, 10, TEXT("向上滚动一页"), 6);
break;
case SB_PAGEDOWN:
TextOut(hdc, 10, 10, TEXT("向下滚动一页"), 6);
break;
}
ReleaseDC(hwnd, hdc);
return 0;
这样修改后,滚动条就会快乐地告诉你它是往上还是往下滚动了,而不是一直重复“向上滚动一行”。希望这能帮助到你,让你的程序变得更加灵活!如果还有其他问题,尽管问我哦,我可是一位耐心的“调试大师”!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|