分享

关于hinstance,该如何解决

 Great2021 2021-10-18
关于hinstance
在winmain函数中有个hinstance参数,由系统传参,在给wndclass这个结构体填空时会有:
  WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
wndclass.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wndclass.hInstance=hInstance;//这一句不写则运行时,看不到窗口,但能在任务管理器里看到其进程,
  // wndclass.hInstance=NULL 当给其赋值空时,能正常运行。
  // wndclass.hInstance=hPreinstance(也就是winmain的第二个参数)这样也能正常运行,
希望各位大人给小弟我讲一个原因,我是新手;另外,winmain的几个参数到底有什么用,如何用,也请指点一二。

------解决方案--------------------
hinstance是这个应用程序的实例句柄,也就是系统将可执行文件的映象加载到进程空间的基本地址,如果需要程序内包含的资源的时候就需要这个实例句柄了。
第二个参数用于16位程序中,
第三个是程序执行参数。
第四个是程序的显示方式。
------解决方案--------------------
我用它作串口句柄
------解决方案--------------------
当你要用到你在rc文件里面定义的东西的时候,比如Icon,Bitmap,Dialog等等的时候。就要用到hInstance了。
------解决方案--------------------
msdn原文。。。
-------
The WinMain function is called by the system as the initial entry point for a Windows-based application. 

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
);
Parameters
hInstance 
[in] Handle to the current instance of the application. 
hPrevInstance 
[in] Handle to the previous instance of the application. This parameter is always NULL. 
If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the GetLastError function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first. 

lpCmdLine 
[in] Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine function. 
nCmdShow 
[in] Specifies how the window is to be shown. This parameter can be one of the following values. Value Meaning 
SW_HIDE Hides the window and activates another window. 
SW_MAXIMIZE Maximizes the specified window. 
SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order. 
SW_RESTORE 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 Activates the window and displays it in its current size and position.
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. 
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window. 
SW_SHOWMINNOACTIVE Displays the window as a minimized window. 
This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
 
SW_SHOWNA Displays the window in its current size and position. 
This value is similar to SW_SHOW, except the window is not activated.
 
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. 
This value is similar to SW_SHOWNORMAL, except the window is not actived.
 
SW_SHOWNORMAL 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. 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多