duilib中托盘图标实现右键菜单

1.菜单注册

LRESULT ViewForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_USER:  //自己写的标志位,详情请看上一篇托盘的那一篇
	{
		if (WM_RBUTTONDOWN == lParam)
		{
			MenuWnd* pMenu = new MenuWnd();
			pMenu->Create(m_hWnd, L"menu", WS_POPUP, WS_EX_TOOLWINDOW | WS_EX_TOPMOST, true);
			pMenu->CMenuWindow();
			pMenu->ShowWindow();
		}
	}
	break;	
	default:
		return   __super::HandleMessage(uMsg, wParam, lParam);
	}
}

2.新建一个菜单的文件 分别为.h和。cpp

//#include "stdafx.h"
#ifndef _MENUWND_H__
#define _MENUWND_H__
//#include "win_impl_base.hpp"
#include 

class MenuWnd : public ui::WindowImplBase
{
public:
	MenuWnd();
	~MenuWnd();
	POINT pt;
	void CMenuWindow();
	virtual std::wstring GetSkinFolder() override;
	virtual std::wstring GetSkinFile() override;
	virtual std::wstring GetWindowClassName() const override;
	static const std::wstring ClassName;
	virtual void InitWindow() override;
	virtual void Close(UINT nRet = IDOK) override;
	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
private:
	ui::Button* btn_set_;
	ui::Button* btn_feedback_;
	ui::Button* btn_help_;
	ui::Button* btn_about_;
	ui::Button* btn_return_te_;
public:
	bool OnClicked_Set(ui::EventArgs* msg);
	bool OnClicked_Feedback(ui::EventArgs* msg);
	bool OnClicked_Help(ui::EventArgs* msg);
	bool OnClicked_About(ui::EventArgs* msg);
	bool OnClicked_Return(ui::EventArgs* msg);
	//void Notify(UINT uMsg);
};
#endif
#pragma once

文件.cpp文件如下

#include "stdafx.h"
#include "view_form.h"
#include "resource.h"
#include "menu.h"
#define  WM_SHOWTASK  WM_USER+10
#define WM_TRAYICON_NOTIFY   WM_USER + 1
const std::wstring MenuWnd::ClassName = L"MenuWnd";

MenuWnd::MenuWnd()
{
}
MenuWnd::~MenuWnd()
{
}
std::wstring MenuWnd::GetSkinFolder()
{
	return L"menu";
}
std::wstring MenuWnd::GetSkinFile()
{
	return L"menu.xml";
}
std::wstring MenuWnd::GetWindowClassName() const
{
	return _T("Menu");
}
void MenuWnd::InitWindow()
{
	btn_set_ = dynamic_cast<ui::Button*>(FindControl(L"set"));
	btn_set_->AttachClick(nbase::Bind(&MenuWnd::OnClicked_Set, this, std::placeholders::_1));
	btn_feedback_ = dynamic_cast<ui::Button*>(FindControl(L"feedback"));
	btn_feedback_->AttachClick(nbase::Bind(&MenuWnd::OnClicked_Feedback, this, std::placeholders::_1));
	btn_help_ = dynamic_cast<ui::Button*>(FindControl(L"help"));
	btn_help_->AttachClick(nbase::Bind(&MenuWnd::OnClicked_Help, this, std::placeholders::_1));
	btn_about_ = dynamic_cast<ui::Button*>(FindControl(L"about"));
	btn_about_->AttachClick(nbase::Bind(&MenuWnd::OnClicked_About, this, std::placeholders::_1));
	btn_return_te_ = dynamic_cast<ui::Button*>(FindControl(L"return_te"));
	btn_return_te_->AttachClick(nbase::Bind(&MenuWnd::OnClicked_Return, this, std::placeholders::_1));
	//this->GetFocus();

}
bool MenuWnd::OnClicked_Set(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"set")
	{
		MessageBox(NULL, _T("hello"), _T("界面"), NULL);
		return true;
	}
}
bool MenuWnd::OnClicked_Feedback(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"feedback")
	{
		MessageBox(NULL, _T("hello"), _T("界面"), NULL);
		return true;
	}
}
bool MenuWnd::OnClicked_Help(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"help")
	{
		MessageBox(NULL, _T("hello"), _T("界面"), NULL);
		return true;
	}
}
bool MenuWnd::OnClicked_About(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"about")
	{
		MessageBox(NULL, _T("hello"), _T("界面"), NULL);
		return true;
	}
}
bool MenuWnd::OnClicked_Return(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"return_te")
	{
		MessageBox(NULL, _T("hello"), _T("界面"), NULL);
		return true;
	}
}
void MenuWnd::CMenuWindow()
{
	POINT pt;
	GetCursorPos(&pt);
	int xLeft = pt.x-3;
	RECT rc;
	GetWindowRect(m_hWnd, &rc);
	int yTop = pt.y - (rc.bottom - rc.top);
	::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
LRESULT MenuWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_KILLFOCUS)
	{
		Close();
		return 0;
	}
	else if (uMsg == WM_KEYDOWN)
	{
		if (wParam == VK_ESCAPE)
		{
			Close();
		}
	}
		return   __super::HandleMessage(uMsg, wParam, lParam);	
}
void MenuWnd::Close(UINT nRet)
{
	// 提示框关闭之前先Enable父窗口,防止父窗口隐到后面去。
	HWND hWndParent = GetWindowOwner(m_hWnd);
	if (hWndParent)
	{
		::EnableWindow(hWndParent, TRUE);
		::SetFocus(hWndParent);
	}
	__super::Close(nRet);
}

搞定,如有帮助点个赞。

你可能感兴趣的:(NIM,Duilib,C++)