How to wirte tray application procedure

本文介绍如何使用NOTIFYICONDATA结构在Windows任务栏创建自定义图标,并通过示例代码演示了如何添加、显示及移除任务栏图标。

    I wrote an application procedure on how to tray a dialog box in the bottom-right of the screen.

 you should learn about NOTIFYICONDATA structure.The MFC describe it like this:

  typedef struct _NOTIFYICONDATA {
    DWORD cbSize;
    HWND hWnd;
    UINT uID;
    UINT uFlags;
    UINT uCallbackMessage;
    HICON hIcon;
    TCHAR szTip[64];
    DWORD dwState; //Version 5.0
    DWORD dwStateMask; //Version 5.0
    TCHAR szInfo[256]; //Version 5.0
    union {
        UINT  uTimeout; //Version 5.0
        UINT  uVersion; //Version 5.0
    } DUMMYUNIONNAME;
    TCHAR szInfoTitle[64]; //Version 5.0
    DWORD dwInfoFlags; //Version 5.0
} NOTIFYICONDATA, *PNOTIFYICONDATA;

 

Members
cbSize
Size of this structure, in bytes.
hWnd
Handle to the window that will receive notification messages associated with an icon in the taskbar status area. The shell uses hWnd and uID to identify which icon on which to operate when Shell_NotifyIcon is invoked.
uID
Application-defined identifier of the taskbar icon. The shell uses hWnd and uID to identify which icon on which to operate when Shell_NotifyIcon is invoked. You can have multiple icons associated with a single hWnd by assigning each a diffent uID.
uFlags
Array of flags that indicate which of the other members contain valid data. This member can be a combination of the following:
FlagDescription
NIF_ICON The hIcon member is valid.
NIF_MESSAGE The uCallbackMessage member is valid.
NIF_TIP The szTip member is valid.
NIF_STATE The dwState and dwStateMask members are valid.
NIF_INFO Use a balloon-style tooltip instead of a standard tooltip. The szInfo, uTimeout, szInfoTitle, and dwInfoFlags members are valid.
uCallbackMessage
Application-defined message identifier. The system uses this identifier to send notifications to the window identified in hWnd. These notifications are sent when a mouse event occurs in the bounding rectangle of the icon, or when the icon is selected or activated with the keyboard. The wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the mouse cursor moves over a taskbar icon, lParam is set to WM_MOUSEMOVE . See the Taskbar guide chapter for further discussion.
hIcon
Handle to the icon to be added, modified, or deleted.
szTip
Pointer to a NULL-terminated string with the text for a standard tooltip. It can have a maximum of 64 characters including the terminating NULL.

For Version 5.0 and later, szTip can have a maximum of 128 characters, including the terminating NULL.

dwState
Version 5.0 . State of the icon. There are two flags that can be set independently:
FlagDescription
NIS_HIDDENThe icon is hidden.
NIS_SHAREDICONThe icon is shared.
dwStateMask
Version 5.0 . A value that specifies which bits of the state member will be retrieved or modified. For example, setting this member to NIS_HIDDEN will cause only the item's hidden state to be retrieved.
szInfo
Version 5.0 . Pointer to a NULL-terminated string with the text for a balloon-style tooltip. It can have a maximum of 255 characters. To remove the tooltip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
uTimeout
The timeout value, in milliseconds, for a balloon-style tooltip. The system enforces minimum and maximum timeout values. uTimeout values that are too large are set to the maximum value and values that are too small default to the minimum value. The system minimum and maximum timeout values are currently set at 10 seconds and 30 seconds, respectively. These values may change in future versions of Windows. See the remarks for further discussion of uTimeout. Union with uVersion.
uVersion
Version 5.0 . Specifies whether the shell notify icon interface should use Windows 95 or Windows 2000 behavior. This member is only employed when using Shell_NotifyIcon to send an NIM_VERSION message. Union with uTimeout.
ValueDescription
0Use the Windows 95 behavior.
NOTIFYICON_VERSIONUse the Windows 2000 behavior.
szInfoTitle
Version 5.0 . Pointer to a NULL-terminated string containing a title for a balloon tooltip. This title will be in boldface, and placed above the text. It can have a maximum of 63 characters.
dwInfoFlags
Version 5.0 . Flags that can be set to add an icon to a balloon tooltip. It will be placed to the left of the title. If the szTitleInfo member is zero-length, the icon will not be shown.
FlagDescription
NIIF_WARNINGA warning icon.
NIIF_ERRORAn error icon.
NIIF_INFOAn information icon.
Remarks

If you set the NIF_INFO flag in the uFlags member, the standard tooltip, will be replaced by a balloon tooltip. For more discussion of balloon tooltips, see the Tooltip Controls chapter.

No more than one balloon tooltip at at time will be displayed for the taskbar. If an application attempts to display a tooltip when one is already being displayed, the tooltip will not appear until the existing balloon tooltip has been visible for at least the system minum timeout value. For example, a balloon tooltip with uTimeout set to 30 seconds has been visible for seven seconds when another application attempts to display a balloon tooltip. If the system minimum timeout is ten seconds, the first tooltip will be displayed for an additional three seconds before being replaced by the second tooltip.

Note that several members of this structure are only supported for Shell32.dll versions 5.0 and later. To enable these members, include the following in your header:

#define _WIN32_IE 0x0500

However, you must initialize the structure with its size. If you use the size of the currently defined structure, the application may not run with the earlier versions of Shell32.dll, which expect a smaller structure. You can run your application on pre-5.0 versions of Shell32.dll by defining the appropriate version number (see Shell and Common Controls Versions). However, this may cause problems if your application also needs to run on systems with more recent versions.

Your application can remain compatible with all Shell32.dll versions while still using the current header files by setting the size of the NOTIFYICONDATA structure appropriately. Before initializing the structure, use the DllGetVersion function to determine which Shell32.dll version is installed on the system. If it is version 5.0 or greater, initialize the cbSize member with:

nid.cbSize = sizeof(NOTIFYICONDATA);

Setting cbSize to this value will enable all the version 5.0 enhancements. For earlier versions, the size of the pre-5.0 structure is given by the NOTIFYICONDATA_V1_SIZE constant. Initialize the cbSize member with:

nid.cbSize = NOTIFYICONDATA_V1_SIZE;
Using this value for cbSize will allow your application to use NOTIFYICONDATA with earlier Shell32.dll versions, although without the version 5.0 enhancements

 

But how to use it. here,I give you a simple sample as following code:

 

 1,define message by yourself in application procedure like this:

/****************************************************************************/
#define WM_MYTRAYMESSAGE (WM_USER+1)       //define message by yourself^^!
#define WM_MYMESSAGE (WM_USER+2)
/****************************************************************************/

 2, define some variable for your need in Dialog Box ;

xxx.h

   class CTrayMessageDemoDlg : public CDialog

{

   .....

   public:

    NOTIFYICONDATA m_myTray;

    HWND hwnd;
    CMenu m_myMenu;
    void InitTray();

  

}

xxx.cpp

  void CTrayMessageDemoDlg::InitTray()  
{
 m_myMenu.LoadMenu(IDR_MENU1);
 m_myTray.cbSize = sizeof(NOTIFYICONDATA);
 m_myTray.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  //load icon resource for tray
 m_myTray.hWnd = m_hWnd;                                 //Set window handle
 //char *m_str = _T("xiaohe");
 //strncpy(m_myTray.szTip,m_str,strlen(m_str)+1);
 m_myTray.uCallbackMessage = WM_MYTRAYMESSAGE;
 m_myTray.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
 m_myTray.uID = IDR_MAINFRAME;
 Shell_NotifyIcon(NIM_ADD,&m_myTray);


}

 

3 add code in OnSysCommand as the following describtion.

 

void CTrayMessageDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
/************Add this code for minimize *****************************************************/
 else if ((nID & 0xFFF0)==SC_MINIMIZE)
 {
  ShowWindow(SW_HIDE);
  Shell_NotifyIcon(NIM_ADD,&m_myTray);
 }
 else if ((nID & 0xFFF0) == SC_CLOSE)
 {
  CDialog::OnCancel();                     //destroy the dialog box
  Shell_NotifyIcon(NIM_DELETE,&m_myTray);  //delete tray icon while dialog box destroyed^^!
  //PostNcDestroy();
 }
/**********************************************************************************************/

4, create menu and its ID is IDR_MENU1

5,define message like this.

xxx.h

  /************************************************************/
 afx_msg LRESULT OnMyTray(WPARAM wParam,LPARAM lParam);
/************************************************************/

xxx.cpp

/*****************************************************************/
 ON_MESSAGE(WM_MYTRAYMESSAGE,OnMyTray)
/******************************************************************/

LRESULT CTrayMessageDemoDlg::OnMyTray(WPARAM wParam,LPARAM lParam)
{
 if (lParam == WM_LBUTTONDOWN)
 {
  ShowWindow(SW_RESTORE);
 }
 else if (lParam == WM_RBUTTONDOWN)
 {
 // MessageBox("hello");
  CPoint point;
  ::GetCursorPos(&point);
  CMenu *pSubMenu = (CMenu*)m_myMenu.GetSubMenu(0);
  pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,
   AfxGetApp()->m_pMainWnd,TPM_LEFTALIGN);
 }
    return TRUE;
}
/********************************************************************************************/

 

      

   

<think> 嗯,用户遇到了一个具体的技术问题:Codesys Control Tray应用程序已在运行的提示。这通常意味着后台进程未正确终止导致的冲突。从用户引用的资料看,ta可能已经尝试过一些常规方法(如重启),但问题依旧存在。 用户提供的引用中,[4]和[5]特别值得注意:[4]提到了dll文件冲突的排查思路,[5]则展示了服务配置的修改案例。虽然场景不同,但方法论可以迁移——即通过进程清理和配置文件调整来解决服务冲突。 深层需求其实很明确:用户需要稳定运行Codesys开发环境。考虑到Codesys在工业控制领域的重要性,这个问题可能直接影响ta的设备调试工作。ta的提问简洁专业,应该是具备一定技术背景的工程师。 解决方案需要分层处理:首先用基础方法解决大多数常见情况(如强制终止进程),再针对顽固案例提供高级方案(如清理注册表)。特别要注意提醒用户注册表操作的风险——毕竟引用[3]里也强调了"检查解决方案"的重要性。 安全提示必须前置:修改注册表前务必备份。这个警告不仅要放在文档里,执行步骤前还要再强调一次,毕竟工业控制软件涉及的参数都很关键。 </think> 针对"Codesys Control Tray应用程序已在运行"的问题,根本原因是后台进程未完全终止导致的新实例冲突。请按以下优先级逐步解决: --- ### **解决方案** #### 1️⃣ **强制终止进程(推荐首选)** ```powershell # 以管理员身份运行PowerShell执行: taskkill /F /IM "CodesysControlTray.exe" /T Stop-Process -Name "Codesys*" -Force ``` > ✓ 强制关闭所有残留进程 > ✓ `/T`参数终止子进程 > ✓ 覆盖常见进程名变异情况 #### 2️⃣ **清理系统启动项** 1. 按 `Win+R` 输入 `msconfig` 2. 在【服务】选项卡中: - 勾选"隐藏所有Microsoft服务" - 取消勾选CODESYS相关服务 3. 在【启动】选项卡禁用CODESYS启动项 4. 重启系统 #### 3️⃣ **重置用户配置文件** 1. 关闭Codesys所有程序 2. 删除配置文件(路径示例): ``` C:\Users\<用户名>\AppData\Local\CODESYS\ C:\ProgramData\CODESYS\ ``` 3. 重启后重新配置(系统将自动重建文件) #### 4️⃣ **注册表清理(高级操作)⚠️** ```regedit # 删除残留启动项(备份注册表!) HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run ``` > 查找并删除含"CodesysControlTray"的键值 --- ### **预防措施** - **服务冲突检测**:安装时关闭杀毒软件实时防护 - **权限配置**:始终以管理员身份运行安装程序 - **版本兼容性**:确认Windows更新未破坏兼容性(可尝试兼容模式运行) - **端口释放**:若提示端口占用,执行 `netstat -ano | findstr :1217` 查进程 > 📌 根据西门子官方文档,该错误多由进程锁文件(`.lock`)残留引起[^4],建议定期清理 `%TEMP%` 目录 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值