【问题】C++执行文件exe图标没切换成功

解决方法

确认资源文件Resource.h文件内第一个图标是否就是你需要使用的图标。

给exe执行文件添加图标

1 添加icon文件

右击资源文件,添加资源
【问题】C++执行文件exe图标没切换成功_第1张图片
导入你需要打icon
【问题】C++执行文件exe图标没切换成功_第2张图片
打开Resource.h文件,找到你刚刚导入的icon资源【问题】C++执行文件exe图标没切换成功_第3张图片

确保需要的图标放在第一个!这个很重要。

2、通知进程修改图标

CSystemTray				m_TrayIcon; //托盘对象
HINSTANCE 				g_hInstance;//全局HINSTANCE
HWND					g_hWnd;		//全局窗口句柄
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	g_hInstance = hInstance;
	// 显示处理创建窗口(自己的exe窗口) 如果是mfc获取一下自己的 HINSTANCE  方法 AfxGetInstanceHandle( );
	// g_hWnd mfc 是GetSafeWnd();
	g_hWnd = ::CreateWindowExW(...);
	
	......
}

主界面初始化方法里面

void CxxxWnd::InitWindow()
{
	//1 加载+注册设置
	HICON hIcon = ::LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));
	//2 通过消息机制设置
	::SendMessage(g_hWnd,STM_SETICON,IMAGE_ICON,(LPARAM)(UINT)hIcon);
	SetIcon(IDI_ICON1);
	//3 托盘图标
	m_trayIcon.CreateTrayIcon(g_hWnd, IDI_ICON1, _T("xxxx系统"));
}

3、托盘

CTrayIcon.h

#pragma once
#ifndef __UITRAICON_H__
#define __UITRAICON_H__

#include 
#include 
using namespace std;
#define UIMSG_TRAYICON  WM_USER + 1// 托盘消息

class  CTrayIcon
{
public:
	CTrayIcon(void);
	~CTrayIcon(void);

public:
	void CreateTrayIcon(HINSTANCE hInstance, HWND _RecvHwnd, UINT _IconIDResource, LPCTSTR _ToolTipText = NULL, UINT _Message = NULL);
	void DeleteTrayIcon();
	bool SetTooltipText(LPCTSTR _ToolTipText);
	bool SetTooltipText(UINT _IDResource);
	string GetTooltipText() const;

	bool SetIcon(HICON _Hicon);
	bool SetIcon(LPCTSTR _IconFile);
	bool SetIcon(UINT _IDResource);
	HICON GetIcon() const;
	void SetHideIcon();
	void SetShowIcon();
	void RemoveIcon();
	bool Enabled() { return m_bEnabled; };
	bool IsVisible() { return !m_bVisible; };

private:
	bool m_bEnabled;
	bool m_bVisible;
	HWND m_hWnd;
	UINT m_uMessage;
	HICON m_hIcon;
	NOTIFYICONDATA	m_trayData;
	HINSTANCE m_hInstance;
};

#endif // 

CTrayIcon.cpp

//#include "StdAfx.h"
#include "TrayIcon.h"

#include 
#include   
#include "ShellAPI.h"  

CTrayIcon::CTrayIcon(void)
{
	memset(&m_trayData, 0, sizeof(m_trayData));
	m_bEnabled = false;
	m_bVisible = false;
	m_hWnd = NULL;
	m_uMessage = UIMSG_TRAYICON;
}

CTrayIcon::~CTrayIcon(void)
{
	DeleteTrayIcon();
}

void CTrayIcon::CreateTrayIcon(HINSTANCE hInstance, HWND _RecvHwnd, UINT _IconIDResource, LPCTSTR _ToolTipText, UINT _Message)
{
	if (!_RecvHwnd || _IconIDResource <= 0) {
		return;
	}
	if (_Message != 0) m_uMessage = _Message;
	m_hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(_IconIDResource));
	m_hInstance = hInstance;
	m_trayData.cbSize = sizeof(NOTIFYICONDATA);
	m_trayData.hWnd = _RecvHwnd;
	m_trayData.uID = _IconIDResource;
	m_trayData.hIcon = m_hIcon;
	m_trayData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	m_trayData.uCallbackMessage = m_uMessage;
	if (_ToolTipText) _tcscpy(m_trayData.szTip, _ToolTipText);
	Shell_NotifyIcon(NIM_ADD, &m_trayData);
	m_bEnabled = true;
}

void CTrayIcon::DeleteTrayIcon()
{
	Shell_NotifyIcon(NIM_DELETE, &m_trayData);
	m_bEnabled = false;
	m_bVisible = false;
	m_hWnd = NULL;
	m_uMessage = UIMSG_TRAYICON;
}

bool CTrayIcon::SetTooltipText(LPCTSTR _ToolTipText)
{
	if (_ToolTipText) _tcscpy(m_trayData.szTip, _ToolTipText);
	if (!m_bEnabled) return FALSE;
	m_trayData.uFlags = NIF_TIP;
	return Shell_NotifyIcon(NIM_MODIFY, &m_trayData) == TRUE;
}

bool CTrayIcon::SetTooltipText(UINT _IDResource)
{
	TCHAR mbuf[256] = { 0 };
	LoadString(m_hInstance, _IDResource, mbuf, 256);
	return SetTooltipText(mbuf);
}

string CTrayIcon::GetTooltipText() const
{
	return m_trayData.szTip;
}

bool CTrayIcon::SetIcon(HICON _Hicon)
{
	if (_Hicon) m_hIcon = _Hicon;
	m_trayData.uFlags = NIF_ICON;
	m_trayData.hIcon = _Hicon;

	if (!m_bEnabled) return FALSE;
	return Shell_NotifyIcon(NIM_MODIFY, &m_trayData) == TRUE;

	return false;
}

bool CTrayIcon::SetIcon(LPCTSTR _IconFile)
{
	HICON hIcon = LoadIcon(m_hInstance, _IconFile);
	return SetIcon(hIcon);
}

bool CTrayIcon::SetIcon(UINT _IDResource)
{
	HICON hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(_IDResource));
	return SetIcon(hIcon);
}

HICON CTrayIcon::GetIcon() const
{
	HICON hIcon = NULL;
	hIcon = m_trayData.hIcon;
	return hIcon;
}

void CTrayIcon::SetHideIcon()
{
	if (IsVisible()) {
		SetIcon((HICON)NULL);
		m_bVisible = TRUE;
	}
}

void CTrayIcon::SetShowIcon()
{
	if (!IsVisible()) {
		SetIcon(m_hIcon);
		m_bVisible = FALSE;
	}
}

void CTrayIcon::RemoveIcon()
{
	m_trayData.uFlags = 0;
	Shell_NotifyIcon(NIM_DELETE, &m_trayData);
	m_bEnabled = FALSE;
}


你可能感兴趣的:(c++,开发语言)