如何创建透明的 32 位托盘图标(使用 GDI)?
我正在尝试创建一个在系统托盘中显示一段文本的图标。 (显然,它不会长于几个字符。)
到目前为止,我已经尝试过:
#include <tchar.h>
#include <Windows.h>
#include <Windowsx.h>
static HICON CreateIcon(LPCTSTR txt) {
HICON hIcon = NULL;
HDC hDC = NULL; {
HDC hDCScreen = GetDC(NULL);
if (hDCScreen != NULL) {
__try { hDC = CreateCompatibleDC(hDCScreen); }
__finally { ReleaseDC(NULL, hDCScreen); }
}
}
if (hDC != NULL) {
__try {
HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);
if (hFont != NULL) {
__try { SelectFont(hDC, hFont); }
__finally { DeleteFont(hFont); }
}
int width = GetSystemMetrics(SM_CXSMICON),
height = GetSystemMetrics(SM_CYSMICON);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, width, height);
if (hBmp != NULL) {
__try {
HBITMAP hMonoBmp =
CreateCompatibleBitmap(hDC, width, height);
if (hMonoBmp != NULL) {
__try {
RECT rect = { 0, 0, width, height };
HGDIOBJ prev = SelectObject(hDC, hBmp);
__try {
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 255));
ICONINFO ii = { TRUE, 0, 0, hMonoBmp, hBmp };
int textHeight =
DrawText(hDC, txt, _tcslen(txt), &rect, 0);
if (textHeight != 0) {
hIcon = CreateIconIndirect(&ii);
}
} __finally { SelectObject(hDC, prev); }
} __finally { DeleteObject(hMonoBmp); }
}
} __finally { DeleteObject(hBmp); }
}
} __finally { DeleteDC(hDC); }
}
return hIcon;
}
使用这段代码:
static void _tmain(int argc, TCHAR* argv[]) {
HICON hIcon = CreateIcon(_T("Hi"));
if (hIcon != NULL) {
__try {
NOTIFYICONDATA nid = { sizeof(nid) };
nid.hWnd = GetConsoleWindow();
BOOL success = Shell_NotifyIcon(NIM_ADD, &nid);
if (success) {
nid.uFlags = NIF_ICON;
nid.hIcon = hIcon;
success = Shell_NotifyIcon(NIM_MODIFY, &nid);
}
} __finally { DestroyIcon(hIcon); }
}
}
但我得到的只是一个单色位图,在 a 上以白色文本显示 Hi
黑色背景。 (如果我稍微更改 RGB(255, 255, 255)
,例如 RGB(255, 255, 254)
,它就会变成黑色,因此它是单色的。)
有什么想法吗?
(*注意:我不寻找MFC、ATL或任何其他库解决方案,只是Win32/GDI调用。)
编辑:
这是当前的样子:
I'm trying to create an icon that displays a piece of text in the system tray. (Obviously, it won't be longer than a couple of characters.)
So far I've tried:
#include <tchar.h>
#include <Windows.h>
#include <Windowsx.h>
static HICON CreateIcon(LPCTSTR txt) {
HICON hIcon = NULL;
HDC hDC = NULL; {
HDC hDCScreen = GetDC(NULL);
if (hDCScreen != NULL) {
__try { hDC = CreateCompatibleDC(hDCScreen); }
__finally { ReleaseDC(NULL, hDCScreen); }
}
}
if (hDC != NULL) {
__try {
HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);
if (hFont != NULL) {
__try { SelectFont(hDC, hFont); }
__finally { DeleteFont(hFont); }
}
int width = GetSystemMetrics(SM_CXSMICON),
height = GetSystemMetrics(SM_CYSMICON);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, width, height);
if (hBmp != NULL) {
__try {
HBITMAP hMonoBmp =
CreateCompatibleBitmap(hDC, width, height);
if (hMonoBmp != NULL) {
__try {
RECT rect = { 0, 0, width, height };
HGDIOBJ prev = SelectObject(hDC, hBmp);
__try {
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 255));
ICONINFO ii = { TRUE, 0, 0, hMonoBmp, hBmp };
int textHeight =
DrawText(hDC, txt, _tcslen(txt), &rect, 0);
if (textHeight != 0) {
hIcon = CreateIconIndirect(&ii);
}
} __finally { SelectObject(hDC, prev); }
} __finally { DeleteObject(hMonoBmp); }
}
} __finally { DeleteObject(hBmp); }
}
} __finally { DeleteDC(hDC); }
}
return hIcon;
}
with this code:
static void _tmain(int argc, TCHAR* argv[]) {
HICON hIcon = CreateIcon(_T("Hi"));
if (hIcon != NULL) {
__try {
NOTIFYICONDATA nid = { sizeof(nid) };
nid.hWnd = GetConsoleWindow();
BOOL success = Shell_NotifyIcon(NIM_ADD, &nid);
if (success) {
nid.uFlags = NIF_ICON;
nid.hIcon = hIcon;
success = Shell_NotifyIcon(NIM_MODIFY, &nid);
}
} __finally { DestroyIcon(hIcon); }
}
}
but all I get is a monochrome bitmap that says Hi
in white text on a black background. (If I change the RGB(255, 255, 255)
even slightly, say to RGB(255, 255, 254)
, it becomes black, so it's monochrome.)
Any ideas?
(*Note: I'm not looking for MFC, ATL, or any other library solutions, just Win32/GDI calls.)
Edit:
Here's what it looks like currently:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,一个部分透明的图标(我认为这就是想要的)的遮罩有一个单色位图。该掩码恰好被忽略,但您仍然必须提供它。您没有创建单色位图,您似乎正在创建 32bpp 位图。我也没有看到在任何地方初始化主位图的 alpha 值,以便您不写入的区域是透明的。
此处提供了代码示例:如何在 Windows XP 中创建 Alpha 混合光标或图标
If I recall correctly, a partially transparent icon (which I think is what want) has a monochrome bitmap for its mask. This mask happens to be ignored but you still have to supply it. You aren't creating a monochrome bitmap, you appear to be creating a 32bpp bitmap. I also don't see anywhere where you initialise the alpha values for you main bitmap so that the areas which you don't write to are transparent.
An example with code is provided here: How To Create an Alpha Blended Cursor or Icon in Windows XP