简单介绍一个VC小技巧,让程序在启动时不显示GUI界面,对于以TrayIcon为主要操作方式的监控类程序,可能希望在程序启动时不显示任何界面,而是直接进入监控状态:比如一个邮箱监视程序,设置好必要的信息之后,我希望每次启动之后,让她直接进入监控状态,检测到有新邮件时,只要给我气泡弹出提示就可以了
如何实现这么一个小功能,网上的方法很多,个人原理不同嘛,呵呵!
我这里只是简单介绍我使用的方法,使用该方法,绝对不会有任何启动时窗口一闪而过的现象,完全静默状态,我们要做的就是拦截WM_WINDOWPOSCHANGING消息,做必要的参数改动就可以了
1.在窗口消息宏里添加ON_WM_WINDOWPOSCHANGING(),使我们能够拦截到该消息,使用该消息后你必须使用默认的函数名void OnWindowPosChanging(WINDOWPOS FAR* lpwndpos),如果很想换一个函数名,那就使用ON_MESSAGE拦吧,很随意,呵呵
2.在对应的窗口类的h中,添加函数声明,比如我的:
void OnWindowPosChanging(WINDOWPOS FAR* lpwndpos);
3.在窗口类的实现文件cpp中,添加函数实现:
void CXjtuNetCheckDlg::OnWindowPosChanging( WINDOWPOS FAR* lpwndpos )
{
if(bSilentMode)
{
lpwndpos->flags ~= SWP_SHOWWINDOW;
}
CDialog::OnWindowPosChanging(lpwndpos);
}
这里的bSilentMode如果是真的时候,就不显示窗口,为什么要有该参数?你肯定不想自己的窗口永远都不显示吧,呵呵,在要显示窗口的时候将其只为否,一般的用法是在窗口初始化的时候也就是在WM_CREATE或者WM_INITDIALOG中将其设置为TRUE,在需要现实窗口的时候,将其置为FALSE,否则你的窗口无论如何也不会再见到你了,切记切记。
一下截取MSDN中关于WM_WINDOWPOSCHANGING的描述:
WM_WINDOWPOSCHANGING Notification
The WM_WINDOWPOSCHANGING message is sent to a window whose size, position, or place in the Z order is about to change as a result of a call to the function or another window-management function.
A window receives this message through its function.
Syntax
WM_WINDOWPOSCHANGING WPARAM wParam LPARAM lParam;
Parameters
wParam
This parameter is not used.
lParam
Pointer to a structure that contains information about the window's new size and position.
Return Value
If an application processes this message, it should return zero.
Remarks
For a window with the or style, the function sends the message to the window. This is done to validate the new size and position of the window and to enforce the and client styles. By not passing the WM_WINDOWPOSCHANGING message to the DefWindowProc function, an application can override these defaults.
While this message is being processed, modifying any of the values in affects the window's new size, position, or place in the Z order. An application can prevent changes to the window by setting or clearing the appropriate bits in the flags member of WINDOWPOS.