项目属性: Configuration Properties ——> Linker ——> System ——> SubSystem ——> Windows. Configuration Properties ——> Advanced ——> Character Set ——> Use Multi-Byte Character Set.
入口函数
win32窗口编程的入口函数是 WinMain,就像是控制台应用程序的 main()。
1 2 3 4 5
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
WinMain 函数是 Windows 程序的入口点之一,它用于 Win32 应用程序的主函数。它的四个参数分别是:
HINSTANCE hInstance:表示当前应用程序实例的句柄。在 Windows 中,每个正在运行的应用程序都有一个唯一的实例句柄。这个参数指示了当前应用程序的实例句柄,可以用来识别和操作应用程序的资源,如对话框、图标、菜单等。
HINSTANCE hPrevInstance:在旧版本的 Windows 中,此参数用于指示前一个应用程序实例的句柄。在现代的 Windows 版本中,它总是被设置为 NULL,因为 Windows 不再支持多个实例共存。
Note The RegisterClass function has been superseded by the RegisterClassEx function. You can still use RegisterClass, however, if you do not need to set the class small icon.
1 2 3 4 5 6
ATOM RegisterClassA( [in] const WNDCLASSA *lpWndClass );
/*if (!RegisterClass(&wc)) return FALSE; */
参数:A pointer to a WNDCLASS structure. You must fill the structure with the appropriate class attributes before passing it to the function.
返回值类型 ATOM,其实就是使用 typedef 定义的一个别名 typedef unsigned short WORD;typedef WORD ATOM; 。
HWND CreateWindowExA( [in] DWORD dwExStyle, //窗口的扩展风格 /*The extended window style of the window being created. For a list of possible values, see Extended Window Styles.*/ [in, optional] LPCSTR lpClassName, //已经注册的窗口类的名称 /*A null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names.*/ [in, optional] LPCSTR lpWindowName, //窗口标题栏的名字 /*The window name. If the window style specifies a title bar, the window title pointed to by lpWindowName is displayed in the title bar. When using CreateWindow to create controls, such as buttons, check boxes, and static controls, use lpWindowName to specify the text of the control. When creating a static control with the SS_ICON style, use lpWindowName to specify the icon name or identifier. To specify an identifier, use the syntax "#num".*/ [in] DWORD dwStyle, //窗口的基本风格 /*The style of the window being created. This parameter can be a combination of the window style values, plus the control styles indicated in the Remarks section.*/ [in] int X, //窗口左上角距离屏幕原点的x水平坐标位置 /*The initial horizontal position of the window. For an overlapped or pop-up window, the x parameter is the initial x-coordinate of the window's upper-left corner, in screen coordinates. For a child window, x is the x-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area. If x is set to CW_USEDEFAULT, the system selects the default position for the window's upper-left corner and ignores the y parameter. CW_USEDEFAULT is valid only for overlapped windows; if it is specified for a pop-up or child window, the x and y parameters are set to zero.*/ [in] int Y, //距离屏幕原点的垂直方向的位置 /*The initial vertical position of the window. For an overlapped or pop-up window, the y parameter is the initial y-coordinate of the window's upper-left corner, in screen coordinates. For a child window, y is the initial y-coordinate of the upper-left corner of the child window relative to the upper-left corner of the parent window's client area. For a list box y is the initial y-coordinate of the upper-left corner of the list box's client area relative to the upper-left corner of the parent window's client area. If an overlapped window is created with the WS_VISIBLE style bit set and the x parameter is set to CW_USEDEFAULT, then the y parameter determines how the window is shown. If the y parameter is CW_USEDEFAULT, then the window manager calls ShowWindow with the SW_SHOW flag after the window has been created. If the y parameter is some other value, then the window manager calls ShowWindow with that value as the nCmdShow parameter. */ [in] int nWidth, //窗口的宽度 /*The width, in device units, of the window. For overlapped windows, nWidth is the window's width, in screen coordinates, or CW_USEDEFAULT. If nWidth is CW_USEDEFAULT, the system selects a default width and height for the window; the default width extends from the initial x-coordinates to the right edge of the screen; the default height extends from the initial y-coordinate to the top of the icon area. CW_USEDEFAULT is valid only for overlapped windows; if CW_USEDEFAULT is specified for a pop-up or child window, the nWidth and nHeight parameter are set to zero.*/ [in] int nHeight, //窗口的高度 /*The height, in device units, of the window. For overlapped windows, nHeight is the window's height, in screen coordinates. If the nWidth parameter is set to CW_USEDEFAULT, the system ignores nHeight.*/ [in, optional] HWND hWndParent, //父窗口的句柄 /*A handle to the parent or owner window of the window being created. To create a child window or an owned window, supply a valid window handle. This parameter is optional for pop-up windows. To create a message-only window, supply HWND_MESSAGE or a handle to an existing message-only window.*/ [in, optional] HMENU hMenu, //窗口菜单句柄 /*A handle to a menu, or specifies a child-window identifier, depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.*/ [in, optional] HINSTANCE hInstance, //应用程序实例句柄 /*A handle to the instance of the module to be associated with the window.*/ [in, optional] LPVOID lpParam //窗口创建时附加参数 /*Pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member) pointed to by the lParam param of the WM_CREATE message. This message is sent to the created window by this function before it returns. If an application calls CreateWindow to create a MDI client window, lpParam should point to a CLIENTCREATESTRUCT structure. If an MDI client window calls CreateWindow to create an MDI child window, lpParam should point to a MDICREATESTRUCT structure. lpParam may be NULL if no additional data is needed.*/ );