windows下一种图标合成和显示方式

本程序用于在任务栏合成显示自定义图标。

#pragma once


#include <windows.h>



HICON myCreateIcon(HWND hwnd, HICON hicon);

VOID setOverLay(HWND hWnd, HICON hIcon, const WCHAR* description);

VOID resetOverLay(HWND hWnd);

void setMyIcon(HWND hwnd);

//int __stdcall iconthread();



#include "overlapIcon.h"

#include "apifuns.h"

#include "main.h"

#include <windows.h>
#include <wingdi.h>
#include "resource.h"

#pragma pack(1)


typedef struct
{
	//biSizeImage表示掩码数据区的大小(XOR和AND
	BITMAPINFOHEADER icHeader; // DIB header

//像素数据的排列次序:
//icColors[256]   //假设是32x32的256色的icon
//icXOR[1024] //32x32
//icAND[128]  //32x32/8每个像素之要1bit //icAND = x*y/8

//dwBytesInres 16936 = 40 + biSizeImage
//biSizeImage:16896,64*64*4=16384, and table 512 bytes

// 	RGBQUAD icColors[1]; // Color table //颜色表的大小取决于icon文件中所用到的颜色数对于16,24位或者32位色的图像就不需要颜色表
// 	BYTE icXOR[1]; // DIB bits for XOR mask
// 	BYTE icAND[1]; // DIB bits for AND mask
} ICONIMAGE, * LPICONIMAGE;

typedef struct
{
	BYTE bWidth; // Width, in pixels, of the image
	BYTE bHeight; //biHeight在这里是像素高度的两倍,因为是XOR掩码区和AND掩码区高度之和
	BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
	BYTE bReserved; // Reserved ( must be 0)
	WORD wPlanes; // Color Planes
	WORD wBitCount; // Bits per pixel
	DWORD dwBytesInRes; // How many bytes in this resource?
	DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, * LPICONDIRENTRY;

typedef struct
{
	WORD idReserved; // Reserved (must be 0)
	WORD idType; // Resource Type (1 for icons)
	WORD idCount; // How many images?
	ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
} ICONDIR, * LPICONDIR;




#pragma pack()

HICON getAppIcon(HWND hwnd);
int copyScanline(BYTE* dst, BYTE* src, int src_line_size, int dst_line_size, int height);
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
int blendPixel(BYTE* dst, BYTE* src, int src_line_size, int dst_line_size, int src_height, int backgroundWeigh);




#define PIXERL_BLEND_APP_ICON_WEIGH 120




int compressBmp(BYTE* line, int line_size, int bytesperpix, int src_height) {
	DWORD* src_ptr = (DWORD*)line;
	DWORD* dst_ptr = (DWORD*)(line + bytesperpix);
	for (int i = 0; i < src_height; i++)
	{
		for (int j = 0; j < line_size / bytesperpix / 2; j += 2)
		{
			src_ptr[j] = (src_ptr[j] + dst_ptr[j]) / 2;
		}
		dst_ptr += line_size;
		src_ptr += line_size;
	}

	src_ptr = (DWORD*)line;
	dst_ptr = (DWORD*)line + line_size;
	for (int i = 0; i < src_height / 2; i++)
	{
		for (int j = 0; j < line_size / sizeof(DWORD) / 2; j++)
		{
			src_ptr[j] = (src_ptr[j] + dst_ptr[j]) / 2;
		}
		dst_ptr = dst_ptr + line_size * 2;
		src_ptr = src_ptr + line_size * 2;
	}
	return TRUE;
}

int copyScanline(BYTE* dst, BYTE* src, int src_line_size, int dst_line_size, int src_height) {
	BYTE* dst_ptr = dst;
	BYTE* src_ptr = src;
	for (int i = 0; i < src_height; i++)
	{
		memcpy(dst_ptr, src_ptr, src_line_size);
		dst_ptr += dst_line_size;
		src_ptr += src_line_size;
	}
	return TRUE;
}


int blendScanline(BYTE* dst, BYTE* src, int src_line_size, int dst_line_size, int src_height, int backgroundWeigh) {
	BYTE* dst_ptr = dst;
	BYTE* src_ptr = src;
	for (int i = 0; i < src_height; i++)
	{
		for (int j = 0; j < src_line_size; j++)
		{
			BYTE src_byte = src_ptr[j];
			BYTE dst_byte = dst_ptr[j];
			BYTE v = (src_byte * (255 - backgroundWeigh) + dst_byte * backgroundWeigh) / 255;

			dst_ptr[j] = v;
		}

		dst_ptr += dst_line_size;
		src_ptr += src_line_size;
	}
	return TRUE;
}



int blendPixel(BYTE* dst, BYTE* src, int src_line_size, int dst_line_size, int src_height, int backgroundWeigh) {
	if (backgroundWeigh)
	{
		return blendScanline(dst, src, src_line_size, dst_line_size, src_height, backgroundWeigh);
	}
	else {
		return copyScanline(dst, src, src_line_size, dst_line_size, src_height);
	}
	return 0;
}


BOOL CALLBACK myEnumWindowsProc(HWND hwnd, LPARAM lParam)
{
	/*
	* Remarks
	The EnumWindows function does not enumerate child windows,
	with the exception of a few top-level windows owned by the
	system that have the WS_CHILD style.
	*/
	//    EnumChildWindows(hwnd, EnumChildProc, lParam); 接着获取子窗口
	int result = 0;

	HWND hwnd_owner = GetWindow(hwnd, GW_OWNER);
	int isvisible = IsWindowVisible(hwnd);

	if (hwnd_owner == 0 && isvisible)
	{
		wchar_t title[256];
		GetWindowTextW(hwnd, title, 256);

		wchar_t classname[256];
		GetClassName(hwnd, classname, 256);

		if (wcscmp(classname, L"Progman") == 0 || wcscmp(classname, L"WorkerW") == 0)
		{
			return false;
		}

		WCHAR procsid[MAX_PATH];
		WCHAR procimage[MAX_PATH];
		ULONG sessionid = 0;
		WCHAR procboxname[64] = { 0 };
		DWORD dwProcessID = 0;

		result = GetWindowThreadProcessId(hwnd, &dwProcessID);
		result = SbieApi_QueryProcess((HANDLE)dwProcessID, procboxname, procimage, procsid, &sessionid);

		// 		mylog(L"window class name:%ws,title:%ws,visible:%d,owner:%x,pid:%d,boxname:%ws,session id:%d,procsid:%ws,image:%ws\r\n",
		// 			classname, title, isvisible, hwnd_owner, dwProcessID, procboxname, sessionid, procsid, procimage);

		if (procboxname[0] == 0)
		{
			//mylog(L"process:%ws,pid:%d already in box:%ws\r\n", procimage, msg->process_id, procboxname);
			return FALSE;
		}

		myCreateIcon(hwnd, 0);
	}

	return true;
}


// int __stdcall iconthread() {
// 	while (TRUE)
// 	{
// 		Sleep(2000);
// 		EnumWindows(myEnumWindowsProc, 0);
// 	}
// }


HICON getAppIcon(HWND hwnd) {

	HICON hicon_app = reinterpret_cast<HICON>(::SendMessageW(hwnd, WM_GETICON, ICON_BIG, 0));

	if (hicon_app == 0) {
#ifdef _WIN64
		hicon_app = (HICON)GetClassLongPtr(hwnd, GCLP_HICON);
#else
		hicon_app = (HICON)GetClassLongPtr(hwnd, GCL_HICON);
#endif	
	}

	if (hicon_app == 0) {
		hicon_app = ::LoadIcon(GetModuleHandleW(0), MAKEINTRESOURCE(0));
	}

	if (hicon_app == 0) {
		hicon_app = ::LoadIcon(0, IDI_APPLICATION);
	}
	return hicon_app;
}





/*
CreateDIBSection 该函数创建应用程序可以直接写入的、与设备无关的位图(DIB)
使用这个函数前,要先建立兼容的内存设备上下文m_hdc,准备好位图信息BITMAPINFO m_bmi;
它怎么使用内存呢? 这个函数会在m_hdc内存块上将void * m_pImageData指向一块信息头后面的区域,指定为数据区。
事实上还没分配内存空间,指针后面的m_ZoomSize.x*m_ZoomSize.x*4大小的空间并没有为数据预留,在真正赋值的时候才会占用空间。
那么怎么释放这些空间呢?不是自己分配的就不用自己释放,系统在释放m_hdc这个内存设备上下文的时候就会释放了。
但是要记得在适当的地方用DeleteDC(m_hdc)释放掉m_hdc哦。如果m_hbitmap被selectobject,记住要先将其换出,DeleteDC(m_hdc)才能把那块数据区域收回哦!
*/


char* back_icon_file_data = 0;




HICON myCreateIcon(HWND hwnd, HICON hicon)
{
	//__debugbreak();


	int result = 0;

	HICON h_myicon = (HICON)LoadImageW(g_dllModule, (WCHAR*)IDI_ICON1, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);


	return 0;
	setOverLay(hwnd, h_myicon, (WCHAR*)L"Warning");

	// 	result = SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)h_myicon);
	// 	result = SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)h_myicon);

	return 0;

	DWORD dw = 0;

	HDC hdcScreen = GetDC(NULL);

	HICON hicon_app = 0;
	if (hicon == 0)
	{
		hicon_app = getAppIcon(hwnd);
	}
	else {
		hicon_app = hicon;
	}

	ICONINFO iconInfo_app;
	result = GetIconInfo(hicon_app, &iconInfo_app);
	BITMAP bmpIcon_app;
	result = GetObjectW(iconInfo_app.hbmColor, sizeof(BITMAP), &bmpIcon_app);
	if (bmpIcon_app.bmWidth % 8 || bmpIcon_app.bmHeight % 8 || bmpIcon_app.bmBitsPixel % 8)
	{
		return FALSE;
	}

#define ICON_FILE

#ifdef  ICON_FILE

	// 	HRSRC res = FindResourceW(g_dllModule, (WCHAR*)IDI_ICON1, RT_GROUP_ICON);
	// 	HGLOBAL resGlobal = LoadResource(g_dllModule, res);
	// 	DWORD size = SizeofResource(g_dllModule, res);
	// 	BYTE* ptr = (BYTE*)LockResource(resGlobal);

	HICON hicon_res = (HICON)LoadImageW(g_dllModule, (WCHAR*)IDI_ICON1, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	//HICON hicon_res = LoadIconW(g_dllModule, (WCHAR*)IDI_ICON1);
	ICONINFO iconInfo_res;
	result = GetIconInfo(hicon_res, &iconInfo_res);

	HDC hdcResIcon = CreateCompatibleDC(hdcScreen);
	SelectObject(hdcResIcon, iconInfo_res.hbmColor);

	BITMAP bmp_res_icon;
	GetObjectW(iconInfo_res.hbmColor, sizeof(BITMAP), &bmp_res_icon);

	BITMAPINFO stback_icon_bmpinfo = { 0 };
	BITMAPINFOHEADER* back_icon_bmpinfo = &stback_icon_bmpinfo.bmiHeader;
	stback_icon_bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	stback_icon_bmpinfo.bmiHeader.biBitCount = bmp_res_icon.bmBitsPixel;
	stback_icon_bmpinfo.bmiHeader.biPlanes = bmp_res_icon.bmPlanes;
	stback_icon_bmpinfo.bmiHeader.biXPelsPerMeter = 0;
	stback_icon_bmpinfo.bmiHeader.biYPelsPerMeter = 0;
	stback_icon_bmpinfo.bmiHeader.biHeight = bmp_res_icon.bmHeight;
	stback_icon_bmpinfo.bmiHeader.biWidth = bmp_res_icon.bmWidth;
	stback_icon_bmpinfo.bmiHeader.biSizeImage = bmp_res_icon.bmHeight * bmp_res_icon.bmWidth * bmp_res_icon.bmBitsPixel / 8;

	BYTE* back_icon_pixel = (BYTE*) new char[stback_icon_bmpinfo.bmiHeader.biSizeImage + 0x1000];
	result = GetDIBits(hdcResIcon, iconInfo_res.hbmColor, 0, bmp_res_icon.bmHeight, back_icon_pixel, &stback_icon_bmpinfo, 0);
#else
	if (back_icon_file_data == 0)
	{
		WCHAR filename[MAX_PATH];
		GetModuleFileNameW(g_dllModule, filename, MAX_PATH);
		WCHAR* ptr = wcsrchr(filename, L'\\');
		if (ptr)
		{
			ptr[1] = 0;
			lstrcatW(filename, L"boxed_icon16.ico");
		}
		HANDLE hf_icon_back = CreateFileW(filename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
		if (hf_icon_back == INVALID_HANDLE_VALUE)
		{
			log(L"CreateFileW:%ws error", filename);
			return FALSE;
		}

		int back_icon_filesize = GetFileSize(hf_icon_back, 0);

		back_icon_file_data = new char[back_icon_filesize + 0x1000];

		result = ReadFile(hf_icon_back, back_icon_file_data, back_icon_filesize, &dw, 0);
		CloseHandle(hf_icon_back);
	}

	LPICONDIR back_icondir = (LPICONDIR)back_icon_file_data;
	LPICONDIRENTRY back_icondir_entry = (LPICONDIRENTRY)back_icondir->idEntries;
	BITMAPINFOHEADER* back_icon_bmpinfo = (BITMAPINFOHEADER*)(back_icon_file_data + sizeof(ICONDIR));
	BYTE* back_icon_pixel = (BYTE*)back_icon_bmpinfo + sizeof(BITMAPINFOHEADER);
	if (back_icondir_entry->bWidth % 8 || back_icondir_entry->bHeight % 8 || back_icondir_entry->wBitCount % 8)
	{
		return FALSE;
	}
	back_icon_bmpinfo->biHeight = back_icon_bmpinfo->biHeight / 2;

	// 	compressBmp(back_icon_pixel, back_icon_bmpinfo->biWidth*back_icon_bmpinfo->biBitCount / 8,
	// 		back_icon_bmpinfo->biBitCount/8, back_icon_bmpinfo->biHeight);
	// 	back_icon_bmpinfo->biWidth = back_icon_bmpinfo->biWidth / 2;
	// 	back_icon_bmpinfo->biHeight = back_icon_bmpinfo->biHeight / 2;
	// 	back_icon_bmpinfo->biSizeImage = back_icon_bmpinfo->biSizeImage / 2;
	// 
	// 	back_icondir_entry->bWidth = back_icondir_entry->bWidth / 2;
	// 	back_icondir_entry->bHeight = back_icondir_entry->bHeight / 2;
	// 	back_icondir_entry->dwBytesInRes = back_icondir_entry->dwBytesInRes / 2;

	int icon_width = 0;
	int icon_height = 0;
	if (back_icon_bmpinfo->biWidth > bmpIcon_app.bmWidth)
	{
		icon_width = back_icon_bmpinfo->biWidth;
	}
	else {
		icon_width = bmpIcon_app.bmWidth;
	}

	if (back_icon_bmpinfo->biHeight > bmpIcon_app.bmHeight)
	{
		icon_height = back_icon_bmpinfo->biHeight;
	}
	else {
		icon_height = bmpIcon_app.bmHeight;
	}
#endif


	BITMAPINFO bmpinfo_addicon = { 0 };
	bmpinfo_addicon.bmiHeader.biSize = back_icon_bmpinfo->biSize;
	bmpinfo_addicon.bmiHeader.biBitCount = back_icon_bmpinfo->biBitCount;
	bmpinfo_addicon.bmiHeader.biPlanes = back_icon_bmpinfo->biPlanes;
	bmpinfo_addicon.bmiHeader.biXPelsPerMeter = back_icon_bmpinfo->biXPelsPerMeter;
	bmpinfo_addicon.bmiHeader.biYPelsPerMeter = back_icon_bmpinfo->biYPelsPerMeter;
	bmpinfo_addicon.bmiHeader.biHeight = back_icon_bmpinfo->biHeight;			// icon_height;
	bmpinfo_addicon.bmiHeader.biWidth = back_icon_bmpinfo->biWidth;				// icon_width;
	bmpinfo_addicon.bmiHeader.biSizeImage = back_icon_bmpinfo->biSizeImage;		// icon_height*icon_width*bmpinfo_addicon.bmiHeader.biBitCount / 8;

	if (0)
	{
		HDC hdcBackground = CreateCompatibleDC(hdcScreen);

		BYTE* background_pixel_buffer = 0;

		HBITMAP hbmp_Background = CreateDIBSection(hdcBackground, (BITMAPINFO*)&bmpinfo_addicon,
			DIB_RGB_COLORS, (void**)&background_pixel_buffer, NULL, 0);

		//memcpy(background_pixel_buffer, back_icon_pixel, back_icondir_entry->bHeight* back_icondir_entry->bWidth* back_icondir_entry->wBitCount / 8);
		memset(background_pixel_buffer, 0xff,
			bmpinfo_addicon.bmiHeader.biHeight * bmpinfo_addicon.bmiHeader.biWidth * bmpinfo_addicon.bmiHeader.biBitCount / 8);
		copyScanline(background_pixel_buffer, back_icon_pixel, back_icon_bmpinfo->biWidth * back_icon_bmpinfo->biBitCount / 8,
			bmpinfo_addicon.bmiHeader.biWidth * bmpinfo_addicon.bmiHeader.biBitCount / 8, back_icon_bmpinfo->biHeight);
		SelectObject(hdcBackground, hbmp_Background);
		//SetDIBits(hdcBackground,hbmp_Background, 0,back_icondir_entry->bHeight, &background_pixel_buffer, (BITMAPINFO*)back_icon_bmpinfo,0 );

		DeleteDC(hdcBackground);
		DeleteObject(hbmp_Background);
	}

	//DrawIconEx
	//CreateIcon
	//CreateBitmap

	HDC hdc_tmp = CreateCompatibleDC(hdcScreen);
	HBITMAP hbmp_tmp = iconInfo_app.hbmColor;
	SelectObject(hdc_tmp, hbmp_tmp);

	BITMAP bmp_tmp;
	result = GetObjectW(hbmp_tmp, sizeof(BITMAP), &bmp_tmp);

	BITMAPINFO bmpinfo_app = { 0 };
	bmpinfo_app.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpinfo_app.bmiHeader.biBitCount = bmpIcon_app.bmBitsPixel;
	bmpinfo_app.bmiHeader.biPlanes = bmpIcon_app.bmPlanes;
	bmpinfo_app.bmiHeader.biXPelsPerMeter = 0;
	bmpinfo_app.bmiHeader.biYPelsPerMeter = 0;
	bmpinfo_app.bmiHeader.biHeight = bmpIcon_app.bmHeight;
	bmpinfo_app.bmiHeader.biWidth = bmpIcon_app.bmWidth;
	bmpinfo_app.bmiHeader.biSizeImage = bmpinfo_app.bmiHeader.biHeight * bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8;

	BYTE* pixel_tmp_buf = (BYTE*)new char[bmpinfo_app.bmiHeader.biSizeImage + 0x1000];
	result = GetDIBits(hdc_tmp, hbmp_tmp, 0, bmpIcon_app.bmHeight, pixel_tmp_buf, &bmpinfo_app, 0);

	// 	HDC hdcApp_icon_mask = CreateCompatibleDC(hdcScreen);
	// 	SelectObject(hdcApp_icon_mask, iconInfo_app.hbmMask);
	// 	BYTE* pixel_icon_mask = (BYTE*)new char[bmpIcon_app.bmWidth * bmpIcon_app.bmHeight * bmpIcon_app.bmBitsPixel / 8 + 0x1000];
	// 	result = GetDIBits(hdcApp_icon_mask, iconInfo_app.hbmMask, 0, bmpIcon_app.bmHeight, pixel_icon_mask, &bmpinfo_app, 0);

	HDC hdcback_icon_mask = CreateCompatibleDC(hdcScreen);
	SelectObject(hdcback_icon_mask, iconInfo_res.hbmMask);
	BYTE* pixel_icon_res_mask = (BYTE*)new char[bmp_res_icon.bmWidth * bmp_res_icon.bmHeight * bmp_res_icon.bmBitsPixel / 8 + 0x1000];
	result = GetDIBits(hdcback_icon_mask, iconInfo_res.hbmMask, 0, bmp_res_icon.bmHeight, pixel_icon_res_mask, &stback_icon_bmpinfo, 0);

	if (bmpIcon_app.bmWidth < bmpinfo_addicon.bmiHeader.biWidth || bmpIcon_app.bmHeight < bmpinfo_addicon.bmiHeader.biHeight)
	{
		blendPixel(pixel_tmp_buf, pixel_icon_res_mask, back_icon_bmpinfo->biWidth * back_icon_bmpinfo->biBitCount / 8,
			bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8, back_icon_bmpinfo->biHeight, PIXERL_BLEND_APP_ICON_WEIGH);

		blendPixel(pixel_tmp_buf, back_icon_pixel, back_icon_bmpinfo->biWidth * back_icon_bmpinfo->biBitCount / 8,
			bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8, back_icon_bmpinfo->biHeight, PIXERL_BLEND_APP_ICON_WEIGH);
	}
	else
	{
		blendPixel(pixel_tmp_buf, pixel_icon_res_mask, back_icon_bmpinfo->biWidth * back_icon_bmpinfo->biBitCount / 8,
			bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8, back_icon_bmpinfo->biHeight, 0);

		blendPixel(pixel_tmp_buf, back_icon_pixel, back_icon_bmpinfo->biWidth * back_icon_bmpinfo->biBitCount / 8,
			bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8, back_icon_bmpinfo->biHeight, 0);
	}
	SetDIBits(hdc_tmp, hbmp_tmp, 0, bmpIcon_app.bmHeight, pixel_tmp_buf, &bmpinfo_app, 0);

	// 	BLENDFUNCTION bf = { 0 };
	// 	bf.BlendOp = AC_SRC_OVER;
	// 	bf.BlendFlags = 0;
	// 	bf.AlphaFormat = 0;
	// 	bf.SourceConstantAlpha = PIXERL_BLEND_APP_ICON_WEIGH;
	// 	result = AlphaBlend(hdc_tmp , 0, 0, back_icondir_entry->bWidth, back_icondir_entry->bHeight, hdcBackground,
	// 	 		0, 0, back_icondir_entry->bWidth, back_icondir_entry->bHeight, bf);


	HPEN hpen = CreatePen(PS_SOLID, 1, 0x00ff00);
	HPEN hpenOld = (HPEN)SelectObject(hdc_tmp, hpen);
	HBRUSH nullbrush = (HBRUSH)GetStockObject(NULL_BRUSH);
	SelectObject(hdc_tmp, nullbrush);

	//	POINT p = { 0 };
	// 	MoveToEx(hdc_tmp, 0, 0, &p);
	// 	LineTo(hdc_tmp, 0, bmpinfo_app.bmiHeader.biHeight);
	// 	LineTo(hdc_tmp, bmpinfo_app.bmiHeader.biWidth, bmpinfo_app.bmiHeader.biHeight);
	// 	LineTo(hdc_tmp, bmpinfo_app.bmiHeader.biWidth, 0);
	// 	LineTo(hdc_tmp, 0, 0);
	// 	HBRUSH hbrush = CreateSolidBrush(0x0);
	// 	HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc_tmp, hbrush);
	//Rectangle(hdc_tmp, 0, 0, bmpIcon_app.bmWidth, bmpIcon_app.bmHeight);
	//Rectangle(hdc_tmp, 0, bmpinfo_addicon.bmiHeader.biHeight, bmpinfo_addicon.bmiHeader.biWidth, bmpIcon_app.bmHeight);
	// 	Rectangle(hdc_tmp, 0, 0, bmpinfo_addicon.bmiHeader.biWidth, bmpinfo_addicon.bmiHeader.biHeight);
	// 	Rectangle(hdc_tmp, 0, 0, bmpinfo_addicon.bmiHeader.biWidth, bmpinfo_addicon.bmiHeader.biHeight);
	// 	SelectObject(hdc_tmp, hbrushOld);

	SelectObject(hdc_tmp, hpenOld);
	// 	DeleteObject(hbrush);
	DeleteObject(hpen);
	DeleteObject(nullbrush);

	// Icons require masks to indicate transparent and opaque areas. Since this
	// simple example has no transparent areas, we use a fully opaque mask.
// 	char* lpicon_mask_bits = 0;
// 	HBITMAP hbmp_icon_Mask = CreateDIBSection(hdcScreen, (BITMAPINFO*)&bmpinfo_app, DIB_RGB_COLORS, (void**)&lpicon_mask_bits, NULL, 0);
// 	memset(lpicon_mask_bits, 0xff, bmpinfo_app.bmiHeader.biHeight * bmpinfo_app.bmiHeader.biWidth * bmpinfo_app.bmiHeader.biBitCount / 8);

	ICONINFO iconinfo_new = { 0 };
	iconinfo_new.fIcon = TRUE;
	iconinfo_new.hbmMask = iconInfo_app.hbmMask;
	iconinfo_new.hbmColor = hbmp_tmp;
	HICON hIconNew = CreateIconIndirect(&iconinfo_new);

	result = SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconNew);
	result = SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIconNew);
	result = SendMessageW(hwnd, WM_SETICON, ICON_SMALL2, (LPARAM)hIconNew);

	// 	SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, (LPARAM)hIconNew);
	// 	SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, (LPARAM)hIconNew);

	ReleaseDC(NULL, hdcScreen);

	DeleteDC(hdc_tmp);
	DeleteObject(hbmp_tmp);
	//DeleteObject(hbmp_icon_Mask);
	DeleteObject(iconInfo_app.hbmColor);
	DeleteObject(iconInfo_app.hbmMask);

	//DeleteObject(bmpIcon_app);
	DeleteDC(hdcResIcon);

	DeleteDC(hdcback_icon_mask);
	DeleteObject(hicon_res);

	delete[]pixel_icon_res_mask;

	delete[] pixel_tmp_buf;

	delete[]back_icon_pixel;

	// 	NOTIFYICONDATA  nid = { 0 };
	// 	nid.cbSize = sizeof(nid);
	// 	nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	// 	lstrcpyW(nid.szTip, L"我的任务栏图标");
	// 	nid.hWnd = hwnd;
	// 	nid.uCallbackMessage = 0;
	// 	nid.hIcon = hIcon;
	// 	result = Shell_NotifyIcon(NIM_MODIFY, &nid);

	DeleteObject(hIconNew);

	//mylog(L"mycreateicon complete");

	return hIconNew;
}


#include <ShObjIdl.h>


VOID setOverLay(HWND hWnd, HICON hIcon, const WCHAR* description)
{


	ITaskbarList3* ptbl;

	HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
	if (SUCCEEDED(hr)) {

		// Attach the toolbar to the thumbnail.
		hr = ptbl->SetOverlayIcon(hWnd, hIcon, description);


	}
	else {

	}
	ptbl->Release();

	//mylog(L"setOverLay end");
}

VOID resetOverLay(HWND hWnd)
{
	//mylog(L"resetOverLay start");

	ITaskbarList3* ptbl;

	HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));

	if (SUCCEEDED(hr)) {
		// Attach the toolbar to the thumbnail.
		hr = ptbl->SetOverlayIcon(hWnd, NULL, (const WCHAR*)L"");

		//mylog(L"resetOverLay SetOverlayIcon result:%d error code:%d", hr, GetLastError());
	}
	else {
		//mylog(L"resetOverLay CoCreateInstance error code:%d", GetLastError());
	}
	ptbl->Release();

	//mylog(L"resetOverLay end");
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值