【代码】
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
TextOut(hdc, 0, 0, "Window Text", 11);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = "ActiveWindow";
wcex.lpszMenuName = NULL;
wcex.style = NULL;
RegisterClassEx(&wcex);
HWND hwnd = CreateWindow(wcex.lpszClassName, "Active Window", WS_OVERLAPPEDWINDOW | WS_VSCROLL, CW_USEDEFAULT, 0, 217, 127, NULL, NULL, hInstance, NULL);
if (!hwnd)
return 1;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
【运行效果】
注意,由于这个窗口是程序的主窗口,所以左上角的图标无法去掉。因为Windows就是这样设计的。