頭文件
#pragma once
#include <Windows.h>
#define NUMLINES ((int)(sizeof sysmetrics/sizeof sysmetrics[0]))
struct {
int Index;
char szLabel[100];//第一列內(nèi)容
char szDesc[50];//第二列內(nèi)容
}sysmetrics[] = {
SM_CXSCREEN,"各位同學(xué)大家好","官方標(biāo)準(zhǔn)提示數(shù)據(jù)",
SM_CYSCREEN,"C/C++高級工程師","官方標(biāo)準(zhǔn)發(fā)布版本"
};
NUMLINES 宏定義是為了得到sysmetrics的數(shù)量
源文件
#include <Windows.h>
#include "systems.h"
LRESULT CALLBACK WndProcFun(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("CKDemoTest");
HWND hwnd;
MSG msg;
WNDCLASS windclass;
windclass.style = CS_HREDRAW | CS_VREDRAW;
windclass.lpfnWndProc = WndProcFun;
windclass.cbClsExtra = 0;
windclass.cbWndExtra = 0;
windclass.hInstance = hInstance;
windclass.hIcon = NULL;
windclass.hCursor = LoadCursor(NULL, IDC_ARROW);
windclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windclass.lpszMenuName = NULL;
windclass.lpszClassName = szAppName;
if (!RegisterClass(&windclass))
{
// 提示信息自己完成
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("第五講 鍵盤控制窗口垂直水平滾事件"), 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 WndProcFun(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
HDC hdc;
int i, x, y, iVertPos, iHorzPos=0, iPaintBeg, iPaintEnd;
PAINTSTRUCT ps;
SCROLLINFO si;
TCHAR szBuffer[20];
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE: // 應(yīng)用程序創(chuàng)建一個窗口
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)*cxChar / 2;
ReleaseDC(hwnd, hdc);
// 保存三列寬度
iMaxWidth = 40 * cxChar + 22 * cxCaps;
return 0;
case WM_SIZE:// 改變一個窗口的大小
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
// 設(shè)置垂直滾動條范圍 和 頁面大小
si.cbSize = sizeof(si);
// SIF_RANGE 設(shè)置滾動范圍值
// SIF_PAGE 設(shè)置滾動頁碼
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = NUMLINES - 1;
si.nPage = cyClient / cyChar;
// SB_VERT 設(shè)置所給定的窗體上標(biāo)準(zhǔn)垂直滾動條參數(shù)
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
// 獲取垂直滾動條位置
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
iVertPos = si.nPos;
// 查找繪制限制
iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
iPaintEnd = min(NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar);
for (i = iPaintBeg; i <= iPaintEnd; i++)
{
x = cxChar * (1 - iHorzPos);
y = cyChar * (i - iVertPos);
TextOut(hdc, x, y, sysmetrics[i].szLabel, lstrlen(sysmetrics[i].szLabel));
TextOut(hdc, x+80*cxCaps, y, sysmetrics[i].szDesc, lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc, TA_RIGHT | TA_TOP);// 指定設(shè)備環(huán)境設(shè)置文字對齊標(biāo)志
TextOut(hdc, x + 100 * cxCaps + 40 * cxChar, y, szBuffer, wsprintf(szBuffer, TEXT("%d"), GetSystemMetrics(sysmetrics[i].Index)));
SetTextAlign(hdc, TA_LEFT | TA_TOP);
}
EndPaint(hwnd, &ps);
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME: // Home
SendMessage(hwnd, WM_VSCROLL, SB_TOP, 0);// 消息發(fā)送到一個或多個窗口
break;
case VK_END:// End
SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0);
break;
case VK_PRIOR:// Page Up
SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
break;
case VK_NEXT:// Page Down
SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
break;
case VK_UP:// 箭頭向上
SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
break;
case VK_DOWN:// 箭頭向下
SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
break;
}
return 0;
case WM_VSCROLL:
// 獲取所有垂直滾動條信息
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;// 整個結(jié)構(gòu)都有效
GetScrollInfo(hwnd, SB_VERT, &si);// SB_VERT 設(shè)置所綻的窗口上標(biāo)準(zhǔn)垂直滾動條參數(shù)
iVertPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_TOP:
si.nPos = si.nMin;
break;
case SB_BOTTOM:
si.nPos = si.nMax;
break;
case SB_LINEUP:
si.nPos -= 1;
break;
case SB_LINEDOWN:
si.nPos +=1;
break;
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
GetScrollInfo(hwnd, SB_VERT, &si);
// 如果位置已經(jīng)更改,滾動窗口并且進(jìn)行更新
if (si.nPos != iVertPos)
{
ScrollWindow(hwnd, 0, cyChar*(iVertPos - si.nPos), NULL, NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
運行結(jié)果只能垂直滾動,水平滾動參考即可,
不懂得可以留言,希望可以幫助到各位朋友。