自动弹出窗口代码(Auto popup window code)
Guided reading:
Since the Microsoft Corp launched Windows 95 operating system, system tray applications as an attractive user interface design, by the vast number of users. Windows applications that use the system tray as a user interface are numerous, such as PowerWord, Winamp, RealPlayer, and so forth.
Do not display these running Windows running, only display an icon on the taskbar, said the program is running, the user can interact with the mouse application developers sometimes need to prepare just in a similar program running in the background, in order to run the interface does not interfere in front of the program and display the unnecessary window. The main window is not visible when the program is running. At the same time, an icon is displayed in the static notification area in the right side of the taskbar and responds to the user's mouse action. The design method of this example introduces Visual C++ development for this type of program, after compiling the program to run, if you double-click the tray icon, the program will be a message list window pops up, as long as the mouse moves over the tray icon or click (both right and left key click or double-click), produced by the message will be displayed in the window; when the mouse cursor moves to the tray icon, will display a message on the icon near; right-cli
ck pop-up context menu, open the property page should contain commands or other open windows and the icon associated with the menu command, in addition, the program can also change the tray dynamic icon. With this example, it is believed that readers can easily and easily use the system tray in their programs.
First, the realization method
In order to realize the tray procedures, the main window first to make the program is not visible, this realization is very easy, as long as the call ShowWindow (SW_HIDE) can be, the examples of the use of this method, there is a way to hide the main frame by setting a main frame window style and extended style:
BOOL CMainFrame:: PreCreateWindow (CREATESTRUCT&, CS)
{
Cs.style =WS_POPUP; / / the main window is not visible;
Cs.dwExStyle =WS_EX_TOOLWINDOW; / / does not display the task button;
Return CFrameWnd:: PreCreateWindow (CS);
}
To display icons on the taskbar, use the system API function Shell_NotifyIcon () to display an icon in the notification area of the taskbar. The prototype of this function is:
BOOL Shell_NotifyIcon (DWORD, dwMessage, PNOTIFYICONDATA, pnid);
The first parameter of this function, dwMessage, is DWORD,
which represents the action to be performed. It can be one of the following values:
NIM_ADD: add an icon to the taskbar.
NIM_MODIFY: modify the icon in the status bar area.
NIM_DELETE: deletes the icon in the status bar area.
NIM_SETFOCUS: returns the focus to the taskbar notification area. When the user interface operation is completed, the taskbar icon must use this message. For example, if the taskbar icon is displaying the context menu, but the user presses the "ESCAPE" button to cancel the operation, then the message must be returned to the taskbar notification area with this message.
NIM_SETVERSION: indicates that the taskbar works in accordance with the corresponding dynamic library version.
The second parameter, pnid, is the address of the NOTIFYICONDATA structure, whose content depends on the value of the dwMessage. This structure is defined in the SHELLAPI.H file as follows:
Typedef, struct, _NOTIFYICONDATA {
DWORD cbSize; / / structure size (sizeof struct), must be set up
HWND hWnd; / / the window handle to send notification messages
UINT uID; / / ID icon (specified by the WPARAM callback function)
UINT uFlags;
UINT uCallbackMessage; / / the message is sent to a window procedure
HICON hIcon; / / the taskbar icon handle
char sztip [64]; / / 提示文本
notifyicondata};
该结构中uflags的值分别为:
# define nif _ message 0x1 / / 表示ucallbackmessage 有效
自动弹窗代码
# define nif _ icon 0x2 / / 表示hicon 有效
# define nif _ tip 0x4 / / 表示sztip 有效
在该结构的成员中, cbsize为该结构所占的字节数, hwnd为接受该
图标所发出的消息的窗口的句柄 (鼠标在任务栏上程序图标上动作
时图标将发出消息, 这个消息用户要自己定义), uid为被显示图标
的id, uflags指明其余的几个成员 (hicon、ucallbackmessage和sztip) 的值是否有效, ucallbackmessage为一个用户自定义的消息, 当用户在该图标上作用一些鼠标动作时, 图标将向应用程序的主框
架窗口 (hwnd成员中指定的窗口) 发出该消息, 为了使程序的主框
架得到该通知消息, 需要设置notifyicondata 结构的flag成员的
值为nif _ message.hicon为将在任务栏上显示的图标句柄, sztip
鼠标停留在该图标上时显示的提示字符串.
尽管shell _ notifyicon函数简单实用, 但它毕竟是个win32 api, 为此本实例将它封装在了一个c + + 类中, 这个类叫做ctrayicon, 有了它, 托盘编程会更加轻松自如, 因为它隐藏了notifyicondata、消息代码、标志以及一些繁琐的细节.
二、编程步骤
1、启动visual c + + 6.0, 生成一个单文档的应用程序traytest, 取消文档视图支持;
2、在cmainframe类中添加自定义消息 # define wm _ my _ tray _ notification wm _ user + 0, 并在该类中为此自定义消息手动添加消息映射on _ message (wm _ my _ tray _ notification, ontraynotification) 和消息响应函数afx _ msg lresult ontraynotification (wparam wp, lparam lp);
3、设计二个图标添加到项目中, 其id标志分别为 "idi _ myicon" 、 "idi _ myicon2", 作为托盘显示时的图标;
4、在cmainframe类中添加下述变量: ctrayicon m _ trayicon (用来操作图标的类对象) 、cedit m _ wnde
dit (编辑框用来显示所跟踪到的鼠标消息) 、int m _ iwhichicon (决定当前托盘使用哪个图标) 、bool m _ bshutdown (是否关闭当前拖盘程序标志) 、bool m _ bshowtraynotifications (是否显示托盘消息标志);
5、为程序的idr _ mainframe添加处理菜单项和托盘的上下文菜单idi _ trayicon (具体的菜单项的标题和id标志符参见代码部分), 然后使用class wizard为各个菜单项添加处理函数;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。