处理键盘消息实现滚动条滚动,文本滚动了,但是滚动条没有重绘
本帖最后由 weijunyu 于 2018-11-19 21:08 编辑来个大佬帮我看看什么情况,处理键盘消息的时候,文本内容重绘了,但是滚动条没重绘。
//滚动条和显示文本
#include <windows.h>;
#include "strsafe.h";
#include "SysMets.h";
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) {
HWND hwnd;
MSG msg;
static TCHAR szAppName[] = TEXT("MyWindows");//指定窗口类名,面向程序员
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;//指定窗口类名,面向程序员
RegisterClass(&wndclass);
hwnd = CreateWindow(
szAppName,
TEXT("WindowsApp"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
0,
0,
800,
600,
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) {
TCHAR szBuffer;
HDC hdc;
static int cxChar,cxCaps,cyChar,cxClient, cyClient;
static size_t iTarget,iTarget1;
TEXTMETRIC tm;
size_t i = 0;
int yPos,FirstLine,LastLine;
SCROLLINFO si;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
//获取窗口尺寸
GetTextMetrics(hdc,&tm);
//每个字符的平均宽度
cxChar = tm.tmAveCharWidth;
//每个字符的实际宽度
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
//字符的高度
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
si.cbSize = sizeof(si);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cyClient / cyChar;
SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
case WM_VSCROLL:
hdc = GetDC(hwnd);
SetTextAlign(hdc, TA_TOP | TA_RIGHT);
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
yPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_TOP:
si.nPos = si.nMin;
break;
case SB_BOTTOM:
si.nPos = si.nMax;
break;
case SB_LINEDOWN:
si.nPos++;
break;
case SB_PAGEDOWN:
si.nPos += cyClient / cyChar;
break;
case SB_PAGEUP:
si.nPos -= cyClient / cyChar;
break;
case SB_LINEUP:
si.nPos--;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
}
ReleaseDC(hwnd, hdc);
//设置滚动条的新位置
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
//获得滚动条的位置
GetScrollInfo(hwnd, SB_VERT, &si);
if (yPos != si.nPos)
{
ScrollWindow(hwnd, 0, cyChar * (yPos - si.nPos), NULL, NULL);
/*UpdateWindow(hwnd);*/
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//获得滚动条的位置
si.cbSize = sizeof(&si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
yPos = si.nPos;
//计算需要重绘的区域
FirstLine = max(0, yPos + ps.rcPaint.top / cyChar);
LastLine = min(NUMLINES - 1, yPos + ps.rcPaint.bottom / cyChar);
for (size_t i = FirstLine; i < LastLine; i++)
{
int y = cyChar * (i - yPos);
//第一列
StringCchLength(sysmetrics.szLabel, 1024, &iTarget);
TextOut(hdc , 0 , y , sysmetrics.szLabel, iTarget);
//第二列
StringCchLength(sysmetrics.szDesc, 12, &iTarget);
TextOut(hdc,17 * cxCaps, y, sysmetrics.szDesc, iTarget == 0 ? 12 : iTarget);
//第三列
SetTextAlign(hdc, TA_TOP | TA_RIGHT);
StringCchPrintf(szBuffer, 10,TEXT("%i"), GetSystemMetrics(sysmetrics.iIndex));
StringCchLength(szBuffer, 10, &iTarget);
TextOut(hdc, 40 * cxCaps, y, szBuffer, iTarget);
SetTextAlign(hdc, TA_TOP | TA_LEFT);
}
EndPaint(hwnd, &ps);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_DOWN:
SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
break;
case VK_UP:
SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
break;
case VK_HOME:
SendMessage(hwnd, WM_VSCROLL, SB_TOP, 0);
break;
case VK_END:
SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0);
break;
case VK_PRIOR:
SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
break;
case VK_NEXT:
SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
};
· 已解决,没有重绘的原因是因为SetScrollInfo()设置滚动条位置的时候,最后一个参数为false,改为true,滚动条在设置的时候就会自动重画了
页:
[1]