Windows游戏开发学习一 —— WinMain函数

本文深入解析Windows程序入口点函数WinMain和wWinMain,包括参数含义、调用约定及句柄概念,帮助理解Windows应用程序启动过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WinMain

每个Windows程序都包含一个名为WinMain或wWinMain的入口点函数。注意两者中第三个参数是不一样的

int WINAPI wWinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance, 
	PWSTR pCmdLine,
	INT nCmdShow);
{
	return 0;
}
INT WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	PSTR lpCmdLine, 
	INT nCmdShow)
{
    return 0;
}

函数类型INT后的WINAPI其实是可以省略的,但是省略之后将会有警告warning C4007: “WinMain”: 必须是“__stdcall”
转到宏定义会发现

#define CALLBACK    __stdcall
#define WINAPI      __stdcall

CALLBACKWINAPI都定义为了__stdcall__stdcall表示一种调用约定,让编译器知道应当以Windows兼容的方式来产生机器指令(__stdcall官方解释)
首先科普一下句柄,在Windows编程的基础中,一个句柄是指使用的一个唯一的整数值,即一个4字节(64位程序中为8字节)长的数值,来标识应用程序中的不同对象和同类中的不同的实例,诸如,一个窗口,按钮,图标,滚动条,输出设备,控件或者文件等。

  • HINSTANCE类型的hInstance,表示“实例句柄”或“模块句柄”(h代表参数为handle),他唯一对应一个运行中的实例,只有运行中的程序实例,才有资格分配到实例句柄。一个应用程序可以运行多个实例,每运行一个实例,系统都会给该实例分配一个句柄值,并且通过hInstance传入WinMain中。
  • HINSTANCE的hPrevInstance,表示当前实例的前一个实例句柄。在Win32环境下,没有任何意义。它用于16位Windows,但现在总是为零。只是在进行WinMain函数书写时需要传入一个参数。
  • pCmdLine,包含命令行参数作为Unicode字符串,p表示指针,CmdLine表示命令行。
  • nCmdShow,是一个标志,指示主应用程序窗口是否将被最小化,最大化或正常显示。
参数描述
SW_FORCEMINIMIZE11Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread.
SW_HIDE0Hides the window and activates another window.
SW_MAXIMIZE3Maximizes the specified window.
SW_MINIMIZE6Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE9Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW5Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT10Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED3Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED2Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE7Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA8Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated.
SW_SHOWNOACTIVATE4Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated.
SW_SHOWNORMAL1Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

我就懒得翻译了,大概应该看得懂

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值