#include <Windows.h>
#include <tchar.h>
#include <string>
#include <strsafe.h>
using namespace std;
LRESULT CALLBACK WndProc(HWND Hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
const TCHAR szWindowClass[] = L"窗口";
const TCHAR szWindowTite[] = L"标题";
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszClassName = szWindowClass;
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(
szWindowClass,
szWindowTite,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int i;
PAINTSTRUCT ps;
size_t iT;
TCHAR szBuffer[12];
TEXTMETRIC tm;
static int cxChar, cyChar;
switch (msg)
{
case WM_CREATE:
hdc = GetDC(hWnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hWnd, hdc);
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
HDC hdc = BeginPaint(hWnd, &ps);
for (i = 0; i < 10; i++)
{
StringCchPrintf(szBuffer, 12, TEXT("%d:%s"), i + 1, TEXT("heheda"));
StringCchLength(szBuffer, 12, &iT);
TextOut(hdc, cxChar, i* cyChar, szBuffer, iT);
}
EndPaint(hWnd, &ps);
return 0;
}
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}