一个经典的WIN32窗口应用
源自图书《精通Windows程序设计–基于VisualC++实现》
这里使用**visual studio2019 **
//1. 新建一个cmd控制台应用程序
//2.将项目->属性->连接器->系统->子系统(窗口)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200205211619522.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIwMTQ0NTk3,size_16,color_FFFFFF,t_70)
#ifndef __cmd_win32__
#define __cmd_win32__
#include <Windows.h>
#include <iostream>
#include "cmd_win32.h"
using namespace std;
#define MAX_LOADSTRING 100 //废弃的
static TCHAR szWindowClass[] = TEXT("黑暗过后的黎明");
static TCHAR szTitle[] = TEXT("黎明来了");
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HACCEL hAccelTable = LoadAccelerators(hInstance, szTitle);
RegisterClass(hInstance); //注册窗口
if (!InitInstance(hInstance, nCmdShow))return FALSE;
while (GetMessage(&msg, NULL, 0, 0)) //进入消息循环
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}return msg.wParam;
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
ATOM MyRegisterClass( HINSTANCE hInstance) {
WNDCLASS wndclass;
//wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_EXCLAMATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
//wndclass.lpszMenuName = (LPCTSTR)IDC_CHAP2EXA2;
wndclass.lpszClassName = szWindowClass;
//wndclass.hIconSm = LoadIconW(wndclass.hInstance, (LPCTSTR)IDI_WARNING);
return MyRegisterClass(&wndclass);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
//显示
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wPARAM, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
DrawText(hdc, TEXT("大家好,这是我的第一个窗口程序!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wPARAM, lParam);
}
#endif !__cmd_win32__