|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <windows.h>
#include <windowsx.h>
#include "strsafe.h"
#define N 10
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);
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)
{
HDC hdc;
PAINTSTRUCT ps;
int i,j;
size_t iTarget;
TEXTMETRIC tm;
static int cxChar, cyChar;
RECT rect;
static TCHAR szBuffer[128] ;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for (i = 1; i <= N; i++)
{
StringCchPrintf(szBuffer, 128, TEXT(""));
for (j = 0; j < 2 * i - 1; j++)
{
StringCchCat(szBuffer, 128, TEXT("x"));
}
StringCchLength(szBuffer, 128, &iTarget);
GetClientRect(hwnd, &rect);
SetTextAlign(hdc, GetTextAlign(hdc) | TA_CENTER);
标记处 TextOut(hdc, (rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2 - (N/2 - i +1) * cyChar, szBuffer, iTarget);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
//case WM_LBUTTONUP:
//MessageBox(hwnd, TEXT("点击来自主人的食指"), TEXT("唔,被按了一下~"), MB_OK);
// return 0;
//case WM_RBUTTONUP:
// MessageBox(hwnd, TEXT("感受到主人中指的力度"), TEXT("唔,被按了一下~"), MB_OK);
// return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
标记出的(rect.bottom - rect.top)/ 2 - (N/2 - i + 1) * cyChar 不太理解(N = 10)
(rect.bottom - rect.top)/ 2 ,这部分是高度的一半
(N/2 - i + 1) ,i从1到N改变,所以这部分应当是从N/2到-N/2+1改变,此处声明N为10,也就是让值从5到-4变化
cyChar,是字符的高度
综上,该表达式求的是纵坐标,在客户区长度的中点处,自下方5个字符高度处向上方4个字符高度处,依次输出字符串。
|
|