vs2017 win32 hello world

新建项目

在这里插入图片描述

在这里插入图片描述

代码

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nShowCmd)
{
    
    
	static TCHAR lpszAppName[] = TEXT("HelloWin");
	HWND      hwnd;
	MSG       msg;
	WNDCLASS  wc;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = lpszAppName;

	// 注册窗口类
	if (!RegisterClass(&wc))
	{
    
    
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			lpszAppName, MB_ICONERROR);
		return 0;
	}

	// 创建应用程序主窗口
	hwnd = CreateWindow(lpszAppName,
		TEXT("The Hello Program"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL);

	// 显示窗口
	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	// 消息循环
	while (GetMessage(&msg, NULL, 0, 0))
	{
    
    
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

//
// 窗口过程函数
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
    
	HDC         hdc;
	PAINTSTRUCT ps;
	RECT        rect;

	switch (message)
	{
    
    
	case WM_CREATE:
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd, &rect);
		DrawText(hdc, TEXT("Hello World!"), -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);
}

效果

在这里插入图片描述

代码歇息

WNDCLASS

老版本

typedef struct tagWNDCLASSW {
    UINT        style;                 // 类风格
    WNDPROC     lpfnWndProc;           // 窗口的处理过程
    int         cbClsExtra;            // 指定紧随在 WNDCLASS 数据结构后分配的字节数
    int         cbWndExtra;            // 指定紧随在窗口实例之后分配的字节数            
    HINSTANCE   hInstance;             // 窗口类所在模块的实例句柄
    HICON       hIcon;                 // 窗口类的图标
    HCURSOR     hCursor;               // 窗口类的光标
    HBRUSH      hbrBackground;         // 窗口类的背景画刷
    LPCWSTR     lpszMenuName;          // 窗口类的菜单资源名
    LPCWSTR     lpszClassName;         // 窗口类的名称
} WNDCLASSW

新版本

typedef struct tagWNDCLASSEXW {
    UINT        cbSize;                // 窗口类结构体的内存大小
    /* Win 3.x */
    UINT        style;             
    WNDPROC     lpfnWndProc;           
    int         cbClsExtra;      
    int         cbWndExtra;         
    HINSTANCE   hInstance;           
    HICON       hIcon;            
    HCURSOR     hCursor;             
    HBRUSH      hbrBackground;        
    LPCWSTR     lpszMenuName;        
    LPCWSTR     lpszClassName;         
    /* Win 4.0 */
    HICON       hIconSm;               // 窗口类的任务栏图标
} WNDCLASSEXW

窗口风格

设置值 说明
CS_HREDRAW 窗口客户区宽度发生改变时重绘窗口
CS_VREDRAW 窗口客户区高度发生改变时重绘窗口
CS_DBLCLKS 鼠标双击时系统所发的消息
CS_NOCLOSE 禁用系统菜单中的“关闭”命令
CS_OWNDC 为该窗口类的各窗口分配各自独立的设备环境
CS_CLASSDC 为该窗口类的各窗口分配一个共享的设备环境
CS_PARENtdC 指定子窗口继承其父窗口的设备环境
CS_SAVEBITS 把被窗口覆盖的屏幕部分作为位图保存起来。 当窗口被刷新时,系统使用被保存的位图来重画窗口。
CS_BYTEALIGNCLIENT Aligns the window’s client area on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
CS_BYTEALIGNWINDOW Aligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
CS_GLOBALCLASS Specifies that the window class is an application global class. For more information, see Application Global Classes.

猜你喜欢

转载自blog.csdn.net/m0_46533764/article/details/111568424
今日推荐