|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <Windows.h>
#include <strsafe.h>
//#include "metrics.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int iCmdShow)
{
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 = TEXT("AAA");
if (!RegisterClass(&wndclass))
return 0;
hwnd = CreateWindow(
wndclass.lpszClassName,
TEXT("Element"),
WS_OVERLAPPEDWINDOW |WS_VSCROLL|WS_HSCROLL,//加滚动条增加按位与参数:|WS_VSCROLL |WS_HSCROLL
400,
300,
273, // 240 两边的各有8像素边框 +17上下滚动条
376, // 320 下边占8像素,上面标题栏占31像素 +17左右滚动条
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //这是非队列化消息,直接发送WM_PAINT消息给客户区,以后会在分支中再次用到
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;
static int cxClient, cyClient,cxChar,cyChar,myVscrollPos; //静态,估计是作用于全局WinMain的支持区:WndProc
TEXTMETRIC charWH; //010课内容:见字符的成员图片
TCHAR szBuffer[32];//存字符串用
size_t bufferTarget;//配合安全字符函数StringCchLength,写入字符长度之用,见WM_SIZE消息中的实践
int i,y;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
//复习010课 获取字高、行距细节,应当放到创建消息中
GetTextMetrics(hdc, &charWH);//得到字高等信息,图片:008-API的字符成员
cxChar = charWH.tmAveCharWidth;//字体平均宽度
cyChar = charWH.tmHeight + charWH.tmExternalLeading; //得到结构的字高+字底到空白边缘的总距离
//复习010课
cxClient = LOWORD(lParam) - 10; //全局变量,给WM_VSCROLL消息使用
cyClient = HIWORD(lParam) - HIWORD(lParam) + 10;
//014课:
SetScrollRange(hwnd, SB_VERT, 0, 100, FALSE);
SetScrollPos(hwnd, SB_VERT, 0, TRUE);
ReleaseDC(hwnd, hdc);
return 0;
case WM_VSCROLL:
hdc = GetDC(hwnd);
SetTextAlign(hdc, TA_TOP | TA_RIGHT);//设置文字(矩形框)的基准点为右上,见下方240
switch (LOWORD(wParam))
{
case SB_LINEUP:
//TextOut(hdc, cxClient, cyClient, TEXT("向上滚动一行..."), 9);
myVscrollPos -= 1;
break;
case SB_LINEDOWN:
//TextOut(hdc, cxClient, cyClient, TEXT("向下滚动一行..."), 9);
myVscrollPos += 1;
break;
case SB_PAGEUP:
//TextOut(hdc, cxClient, cyClient, TEXT("向上滚动一页..."), 9);
//客户区高度除以每行字(加上空行)的高度,得出每页的高度
myVscrollPos -= (cyClient / cyChar);
break;
case SB_PAGEDOWN:
//TextOut(hdc, cxClient, cyClient, TEXT("向下滚动一页..."), 9);
myVscrollPos += (cyClient / cyChar);
break;
case SB_THUMBTRACK:
//TextOut(hdc, cxClient, cyClient, TEXT("别抓着我不放..."), 9);
myVscrollPos = HIWORD(wParam);//客户区高度
break;
}
ReleaseDC(hwnd, hdc);
myVscrollPos = max(0, min(myVscrollPos,100-1 ) );//总行数为100行,使上拉出现空白行
if (myVscrollPos != GetScrollPos(hwnd, SB_VERT))
{
SetScrollPos(hwnd, SB_VERT, myVscrollPos, TRUE);
//以下两条成对使用,可以保证即时刷新窗口:
InvalidateRect(hwnd, NULL, TRUE); //使客户区失效,Windows在消息对列为空时,才会发送WM_PAINT消息
UpdateWindow(hwnd); //如果计算机一直很忙,就会导致不刷新,有延迟,会有卡的感觉
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
/*
GetClientRect(hwnd, &rect); //获得当前客户区矩形
cxClient = GetSystemMetrics(SM_CXSCREEN); //新学内容,返回的是整型
cyClient = GetSystemMetrics(SM_CYSCREEN);
StringCchPrintf(szBuffer, 32, TEXT("%d %d pixel"), cxClient, cyClient); //字符数组,格式化,写入
StringCchLength(szBuffer, 32, &bufferTarget); //计算写入的长度
//for (int i = 0; i < 100; i++)
TextOut(hdc, 10, 10, szBuffer, bufferTarget); //投放文字时要指出长度
//DrawText(hdc, szBuffer, -1, &rect, 0 | 0); //画出
*/
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("Nokia Screen"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//20 | 1 | 4 DT_SINGLELINE | DT_CENTER | DT_VCENTER
for (i = 0; i < 100; i++)
{
y = cyChar*(i - myVscrollPos);
//StringCchPrintf(szBuffer, 32, TEXT("%d * %d pixel %d"), LOWORD(lParam), HIWORD(lParam), i);
StringCchPrintf(szBuffer, 32, TEXT("%d * %d pixel %d"), cxClient, cyClient, i);
StringCchLength(szBuffer, 32, &bufferTarget);
TextOut(hdc, 10, y, szBuffer, bufferTarget); //不猜高度为15,忘记了010课,映射模式概念
}
EndPaint(hwnd, &ps);
return 0;
case WM_SIZE: //需要记得此分支中的函数使用概念:获得当前客户区最大宽、最大高需要在WM_SIZE消息中取得
hdc = GetDC(hwnd);
cxClient = LOWORD(lParam); //全局变量,给WM_VSCROLL消息使用
cyClient = HIWORD(lParam);
ReleaseDC(hwnd, hdc);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
/*
GetSystemMetrics 用于返回 图形项 图标、鼠标指针、标题栏、滚动条等。
引出术语:设备独立性
返回的值的所有单位,都是像素
*/ |
|