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

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_FORCEMINIMIZE 11 Minimizes 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_HIDE 0 Hides the window and activates another window.
SW_MAXIMIZE 3 Maximizes the specified window.
SW_MINIMIZE 6 Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE 9 Activates 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_SHOW 5 Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT 10 Sets 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_SHOWMAXIMIZED 3 Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED 2 Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE 7 Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA 8 Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated.
SW_SHOWNOACTIVATE 4 Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated.
SW_SHOWNORMAL 1 Activates 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.

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

你可能感兴趣的:(Windows游戏编程)