C/C++

C++MFC托盘图标菜单

#define MYWM_NOTIFYICON WM_USER+1
protected:
    NOTIFYICONDATA m_tnid;

int C*Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
	m_tnid.cbSize=sizeof(NOTIFYICONDATA); 
	m_tnid.hWnd=this->m_hWnd; 
	m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; 
	m_tnid.uCallbackMessage=MYWM_NOTIFYICON;    

	strcpy(m_tnid.szTip, "托盘图标实例"); 
	m_tnid.uID=IDR_MAINFRAME; 
	//HICON hIcon;   
	//hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_tnid.hIcon=m_hIcon;   
	::Shell_NotifyIcon(NIM_ADD,&m_tnid);   
	//if(hIcon)::DestroyIcon(hIcon);  

LRESULT C*Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
switch(message){
		case MYWM_NOTIFYICON:
			if(lParam==WM_RBUTTONDOWN)
			{
			CMenu menu;
			menu.LoadMenu(IDR_MENU1);


			SetForegroundWindow();
			
			CMenu* pMenu=menu.GetSubMenu(0);
			CPoint pos;
			GetCursorPos(&pos);
			pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pos.x,pos.y,AfxGetMainWnd());
			PostMessage(WM_NULL, 0,0); //用于失去焦点菜单消失
			}
			break; 
	} 
	return CDialog::WindowProc(message, wParam, lParam);
 

Image

0 条评论

发表评论

你需要登录后才可进行发表