Create Win32 Window

/* Main File */

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

TCHAR szWindowClass[] = _T("Win32app");
TCHAR szTitle[] = _T("Title");
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

/* *******************************
* WinMain
* Entry Function
* *******************************/
int WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
// RegisterClassEx
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;// LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}

// Store instance handle in our global variable
hInst = hInstance;

//CreateWindow
HWND hWnd = CreateWindowEx(
WS_EX_TRANSPARENT,
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
100, 100,
500, 300,
NULL,
NULL,
hInstance,
NULL
);

if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);

return 1;
}

//ShowWindow
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);

//Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}

// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.//
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello, World!");

switch (message)
{
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
HPEN hPen, hOldPen;
HBRUSH hBrush, hOldBrush;
BYTE bRed = 0;
BYTE bGreen = 0;
BYTE bBlue = 0;
COLORREF colorPen = RGB(0, 153, 51);
COLORREF colorBrush = RGB(bRed, bGreen, bBlue);
hPen = CreatePen(PS_SOLID, 1, colorPen);
hBrush = CreateSolidBrush(colorBrush);
hOldPen = (HPEN)SelectObject(hdc, hPen);
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Rectangle(hdc, 0, 0, 492, 270);
MoveToEx(hdc, 10, 20, NULL);
LineTo(hdc, 492, 270);
SelectObject(hdc, hOldPen);
SelectObject(hdc, hOldBrush);
DeleteObject(hPen);
DeleteObject(hBrush);

HFONT hFont, hOldFont;
hFont = CreateFont(20,20,0,0,FW_THIN,FALSE,FALSE,FALSE,CHINESEBIG5_CHARSET,OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,FF_MODERN,_T("微软雅黑"));
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(0,153,51));
hOldFont = (HFONT)SelectObject(hdc, hFont);
TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
SelectObject(hdc, hOldFont);
DeleteObject(hFont);

EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}

return 0;
}

原文地址:https://www.cnblogs.com/waterair/p/7284167.html