// HelloWnd.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "HelloWnd.h"
/*
// 注意定义的结构方式
struct
{
int index;
TCHAR *szLabel;
TCHAR *szDesc;
}
sysmetrics [] =
{
SM_CXSCREEN, TEXT("SM_CXSCREEN"),
TEXT("Screen width in pixels"),
SM_CYSCREEN, TEXT("SM_CYSCREEN"),
TEXT("Screen height in pixels")
};
*/
#include "SysMets.h" // 将以上结构移到头文件中
LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
static int cxAveCharWidth; // 小写字符的宽度
static int cyAveCharHeight;// 字符的高度
static int cxAveUpperCharWidth; // 大写字符的宽度
TEXTMETRIC tm; // 保存了字体信息
static int cxClient; // 客户端的宽度和高度,在窗口发生变化的时候,windows都会发送WM_SIZE消息,该消息的lParam高位保存高度,地位保存宽度
static int cyClient;
// cyAveCharHeight / cyClient 可以显示字符的行数
WCHAR wcTmp[MAX_PATH];
switch(uMsg)
{
case WM_CREATE:
{
hdc = GetDC(hwnd); // 在非WM_PAINT消息里面调用此方法获得HDC
GetTextMetrics(hdc, &tm);
cxAveCharWidth = tm.tmAveCharWidth; // 小写字符的加权平均宽度
cyAveCharHeight= tm.tmHeight + tm.tmExternalLeading; // 字符的高度 + 两行字符之间的额外高度
cxAveUpperCharWidth = (tm.tmPitchAndFamily & 0x01 ? 3 : 2) * cxAveCharWidth / 2; // tmPitchAndFamily 低位决定字体是否是宽字体,1代表宽字体,0代表等宽字体
ReleaseDC(hwnd, hdc);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps); // WM_PAINT 消息里面使用该方法获得HDC
GetClientRect(hwnd, &rt);
// 绘制文本函数
//DrawText(hdc, _T("Hello Windows!"), -1, &rt, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
// 输出字符的高度和宽度
//ZeroMemory(wcTmp, MAX_PATH);
//int sLen = swprintf_s(wcTmp, MAX_PATH, _T("lower case width: %d; upper case wdith: %d; height: %d "), cxAveCharWidth, cyAveCharHeight, cxAveUpperCharWidth); // 注意返回值
//TextOut(hdc, 0, 0, wcTmp, sLen);
for(int i=0; i<NUMLINES; i++)
{
// lstrlen 获取字符串的长度,其中不包括terminating null character
SetTextAlign(hdc, TA_LEFT | TA_TOP);
TextOut(hdc, 0, cyAveCharHeight * i, sysmetrics[i].szLabel, lstrlen(sysmetrics[i].szLabel));
TextOut(hdc, 22 * cxAveUpperCharWidth, cyAveCharHeight * i, sysmetrics[i].szDesc, lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
ZeroMemory(wcTmp, MAX_PATH);
TextOut(hdc, 22 * cxAveUpperCharWidth + 40 * cxAveCharWidth, cyAveCharHeight * i, wcTmp, swprintf_s(wcTmp, MAX_PATH, _T("%d"), GetSystemMetrics(sysmetrics[i].iIndex)));
}
EndPaint(hwnd,&ps);
}
break;
case WM_SIZE:
{
// 在窗口每次发生变化的时候,获取客户端高度和宽度
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
}
break;
case WM_CLOSE:
{
if(IDOK == MessageBox(hwnd, _T("是否关闭窗口"), _T("提示"), MB_OK | MB_OKCANCEL))
{
DestroyWindow(hwnd); // 销毁窗口
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(0); // 发送 WM_CLOSE, 形参 0 将会传递给 WPARAM
}
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// 设计一个窗口类
WNDCLASS wndclass;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = HelloWndProc; // 由windows系统调用
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_WINLOGO));
wndclass.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = _T("hellownd"); // 此处是指定窗口类名称,用于关联窗口,CreateWindow第一个参数
// 注册窗口类
RegisterClass(&wndclass);
// 创建窗口
HWND hwnd = CreateWindow(_T("hellownd"), _T("hellownd"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); // WM_CREATE
// 显示窗口
ShowWindow(hwnd, SW_SHOW); // WM_SIZE
// 更新窗口
UpdateWindow(hwnd); // WM_PAINT
// 消息循环
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(_T("hellownd"), hInstance);
return msg.wParam; // 来自与PostQuitMessage中的参数
}
简单文本输出
最新推荐文章于 2021-05-26 15:29:50 发布