标签:
#include <windows.h>
#include <iostream>
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
//OnPaint
void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps = { 0 };
HDC hDC = BeginPaint(hWnd, &ps);
//TDD...
//设置字体颜色
COLORREF nOldTextColor = SetTextColor(hDC, RGB(255, 0, 0));
//设置文字背景颜色
COLORREF nOldBkColor = SetBkColor(hDC, RGB(0, 0, 255));
//设置字体
HFONT hFont = CreateFont(20, 20, 0/*倾斜度*/, 0, 0, FALSE, FALSE, DEFAULT_CHARSET,
0, 0, 0, 0, 0, "宋体");
//选择字体到DC中
HFONT hOldFon = (HFONT)SelectObject(hDC, hFont);
sprintf_s(szText, 256, "Hello Text");
//TextOut
TextOut(hDC, 100, 100, szText, strlen(szText));
//设置文字背景绘制模式,为透明模式
SetBkMode(hDC, TRANSPARENT);
//DrawText
RECT rcText = { 0 };
INT nText[10] = { 30, 30, 30, 10, 30, 10, 30, 10,20, 2}; //间距,汉字的间距为0
rcText.left = 100;
rcText.top = 120;
rcText.right = 200;
rcText.bottom = 200;
Rectangle(hDC, rcText.left, rcText.top, rcText.right, rcText.bottom);
DrawText(hDC, szText, strlen(szText), &rcText, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
ExtTextOut(hDC, 100, 300, ETO_OPAQUE, NULL, szText, strlen(szText), nText);
SetTextColor(hDC, nOldTextColor); //恢复字体颜色
SetBkColor(hDC, nOldBkColor); //恢复背景颜色
//恢复字体
SelectObject(hDC, hOldFon); //取出字体
DeleteObject(hFont); //删除字体
EndPaint(hWnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_PAINT:
OnPaint(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
//ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg();
return 0;
}
标签:
原文地址:http://www.cnblogs.com/nfking/p/5573168.html