/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#include <strsafe.h>
#include "SysMet.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("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
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;
RECT rect;
TCHAR szBuffer[128] = TEXT("12345");
TEXTMETRIC tm;
SCROLLINFO si;
int pos = 0;
static int cxChar, cxCaps, cyChar, cxClient, cyClient, xClientMax;
size_t iTarget;
int i;
int xPos=0,yPos = 0;
int displayPos,displayxPos = 0;
int FirstLine,LastLine;
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);
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;
//获取纵向滚动条的位置
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd,SB_HORZ,&si);
xPos = si.nPos;
//计数需要重绘的行
FirstLine = max(0, yPos + ps.rcPaint.top / cyChar);
LastLine = min(NUMLINES - 1, yPos + ps.rcPaint.bottom / cyChar);
for (i = FirstLine; i <= LastLine; i++)
{
SetTextAlign(hdc, TA_LEFT | TA_TOP);
displayPos = (i-yPos) * cyChar; //计数重绘行的纵向位置
displayxPos = -xPos* cxChar;
StringCchLength(sysmetrics[i].szLabel, 1024, &iTarget);
TextOut(hdc, displayxPos,displayPos, sysmetrics[i].szLabel, iTarget);
displayxPos = (22 * cxCaps) - xPos* cxChar;
StringCchLength(sysmetrics[i].szDesc, 1024, &iTarget);
TextOut(hdc, displayxPos, displayPos, sysmetrics[i].szDesc, iTarget);
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
displayxPos = (25 * cxCaps + 40 * cxChar) - xPos* cxChar;
StringCchPrintf(szBuffer, 128, TEXT("%5d------%d"), GetSystemMetrics(sysmetrics[i].iIndex),i);
StringCchLength(szBuffer, 128, &iTarget);
TextOut(hdc, displayxPos, displayPos, szBuffer, iTarget);
}
//绘制完毕
EndPaint(hwnd, &ps);
xClientMax = 75*cxChar;//这里最好是测算出来,而不是写死,后面再说吧。
return 0;
case WM_SIZE:
hdc = GetDC(hwnd);
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
StringCchPrintf(szBuffer,128,TEXT("%d * %d"),cxClient,cyClient);
SetTextAlign(hdc, TA_TOP | TA_RIGHT);
TextOut(hdc, cxClient - 40, 30,szBuffer , lstrlen(szBuffer));
ReleaseDC(hwnd,hdc);
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = NUMLINES-1;
si.nPage = cyClient/cyChar;
si.nPos = 0;
SetScrollInfo(hwnd,SB_VERT,&si,FALSE);
//设置横向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = xClientMax/cxChar;
si.nPage = cxClient/cxChar;
si.nPos = 0;
SetScrollInfo(hwnd,SB_HORZ,&si,FALSE);
return 0;
case WM_LBUTTONDOWN:
return 0;
case WM_HSCROLL:
//获取横向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_HORZ,&si);
xPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINELEFT:
xPos = si.nPos;
break;
case SB_LINERIGHT:
si.nPos++;
break;
case SB_PAGELEFT:
si.nPos -= si.nPage;
if(si.nPos < 0) si.nPos = 0;
break;
case SB_PAGERIGHT:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
}
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
SetScrollInfo(hwnd,SB_HORZ,&si,TRUE);
GetScrollInfo(hwnd,SB_HORZ,&si);
if(xPos - si.nPos != 0)
{
ScrollWindow(hwnd,cxChar*(xPos - si.nPos),0,NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_VSCROLL:
//获取纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_VERT,&si);
yPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINEUP:
yPos = si.nPos;
si.nPos--;
break;
case SB_LINEDOWN:
si.nPos++;
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;
}
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
//InvalidateRect(hwnd,NULL,TRUE);
GetScrollInfo(hwnd,SB_VERT,&si);
if(yPos - si.nPos != 0)
{
rect.left = 0;
rect.top = 0;
rect.right = (22 * cxCaps);
rect.bottom = cyClient;
ScrollWindow(hwnd, 0, cyChar*(yPos - si.nPos),&rect,NULL);
//ScrollWindow(hwnd, 0, cyChar*(yPos - si.nPos),NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}