正在开发中的游戏有个全屏功能--可以在window桌面背景上运行,就像一些视频播放器在桌面背景上播放一样的,花了个上午整了个Demo放出来留个纪念。
实现功能:显示图标,双击图标执行相应的程序,右击图标弹出该图标对应得菜单,点击非图标区则弹出桌面菜单。需要完整工程可以点此下载:DesktopWindow.rar。程序效果图如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/kesalin/Desktop_00.JPG)
在这个程序里,定义了一个XShellItem的数据结构,保持桌面图标的iten id(ITEMIDLiST),图标以及文字图标。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
struct XShellItem ...{
ITEMIDLIST* itemId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x;
int y;
int w;
int h;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nameX;
int nameY;
int nameW;
int nameH;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BOOL hit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStringW name;
Bitmap* icon;
Bitmap* nameIcon;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XShellItem()
:
itemId(NULL),
x(0),
y(0),
w(0),
h(0),
nameX(0),
nameY(0),
nameW(0),
nameH(0),
name(L""),
hit(FALSE),
icon(NULL),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
nameIcon(NULL) ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
~XShellItem() ...{
}
};
然后定义一个数组CAtlArray<XShellItem> itemArray;用来保存所有桌面图标对象,在InitShellFolder()中对它进行初始化:
// 获取桌面图标的相关数据
BOOL InitShellFolder()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HRESULT hRslt = SHGetDesktopFolder(&folder);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (FAILED(hRslt)) ...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CComPtr<IEnumIDList> ids;
hRslt = folder->EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ids);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (FAILED(hRslt)) ...{
return FALSE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CAtlList<XShellItem> items;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (;;) ...{
ITEMIDLIST* id = 0;
ULONG cIds = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hRslt = ids->Next(1, &id, &cIds);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (hRslt != S_OK) ...{
break;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStringW name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
STRRET str = ...{ 0};
hRslt = folder->GetDisplayNameOf(id, SHGDN_NORMAL | SHGDN_INFOLDER, &str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (SUCCEEDED(hRslt)) ...{
LPWSTR pname = 0;
StrRetToStrW(&str, id, &pname);
name = pname;
CoTaskMemFree(pname);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XShellItem item;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
item.itemId = id;
item.name = name;
items.AddTail(item);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SIZE_T iItem = 0;
SIZE_T cItems = items.GetCount();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemArray.SetCount(cItems);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
POSITION pos = items.GetHeadPosition();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while (pos != 0) ...{
XShellItem& si = items.GetNext(pos);
itemArray[iItem] = si;
iItem++;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC hDC = CreateCompatibleDC(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g(hDC);
g.Clear(Color(0, 0, 0, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ICONMETRICS im = ...{ 0};
im.cbSize = sizeof(im);
SystemParametersInfo(SPI_GETICONMETRICS, sizeof(im), &im, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SolidBrush br_t(Color(255, 255, 255));
Font font_i(hDC, &(im.lfFont));
float fcy = font_i.GetHeight(&g) * 2 + 2;
DeleteDC(hDC);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Gdiplus::StringFormat sf(Gdiplus::StringFormat::GenericTypographic());
sf.SetAlignment(Gdiplus::StringAlignmentCenter);
sf.SetTrimming(Gdiplus::StringTrimmingEllipsisWord);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iconSpacingWidth = im.iHorzSpacing + OFFSET_WIDTH;
iconSpacingHeight = im.iVertSpacing + OFFSET_HEIGHT;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int iconWidth = GetSystemMetrics(SM_CXICON);
int iconHeight = GetSystemMetrics(SM_CYICON);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (SIZE_T i = 0; i < cItems; i++) ...{
XShellItem& item = itemArray[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// SHGetFileInfo
HICON hIcon = 0;
HIMAGELIST hImgList;
SHFILEINFO stSHFileInfo;
CImageList cImgList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取图标
hImgList = (HIMAGELIST)::SHGetFileInfo(
(LPCWSTR) item.itemId,
0,
&stSHFileInfo,
sizeof(SHFILEINFO),
SHGFI_PIDL | SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SYSICONINDEX);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// DIBSection 8bit
BITMAPINFO bmi;
BITMAPINFOHEADER& bmih = bmi.bmiHeader;
bmih.biSize = sizeof(bmih);
bmih.biWidth = ICON_WIDTH;
bmih.biHeight = -ICON_HEIGHT; // BMP反转
bmih.biPlanes = 1;
bmih.biBitCount = 32;
bmih.biCompression = BI_RGB;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HDC memDC = CreateCompatibleDC(0);
void* pDib = 0;
HBITMAP hBmp = CreateDIBSection(memDC, &bmi, DIB_RGB_COLORS, &pDib, 0, 0);
GdiFlush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HGDIOBJ old = SelectObject(memDC, hBmp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ImageList_Draw WindowsXP
ImageList_SetBkColor(hImgList, 0x0);
ImageList_Draw(hImgList, stSHFileInfo.iIcon, memDC, 0, 0, ILD_NORMAL);
SelectObject(memDC, old);
DeleteDC(memDC);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cImgList.Attach(hImgList);
hIcon = cImgList.ExtractIcon(stSHFileInfo.iIcon);
cImgList.Detach();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (hIcon != 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Bitmap::FromHICON 0~255
item.icon = Bitmap::FromHICON(hIcon);
item.w = iconWidth;
item.h = iconHeight;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Gdiplus::RectF rc(float(2), float(2), float(iconSpacingWidth - 4), fcy);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Gdiplus::Bitmap * nameIcon = new Bitmap(NAME_WIDTH, NAME_HEIGHT, &g);
Gdiplus::Graphics * g2 = Gdiplus::Graphics::FromImage(nameIcon);
g2->Clear(Gdiplus::Color(Gdiplus::ARGB(0)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g2->DrawString(item.name, item.name.GetLength(), &font_i, rc, &sf, &br_t);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
item.nameIcon = nameIcon;
item.nameW = NAME_WIDTH;
item.nameH = NAME_HEIGHT;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete g2;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DestroyIcon(hIcon);
DeleteObject(hBmp);
DestroyIcon(stSHFileInfo.hIcon);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return TRUE;
}
注意这里面并没有设置图标对象的位置,因为当窗口改变大小的时候,相应地也要调整图标的描绘位置,所以图标位置是在SetShellItemPosition()中动态调整的.
// 根据窗口大小设置图标位置
void SetShellItemPosition()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
int iconWidth = GetSystemMetrics(SM_CXICON);
int iconHeight = GetSystemMetrics(SM_CYICON);
static const int OFFSET_Y = 20;
int x = 0;
int y = OFFSET_Y;
SIZE_T cItems = itemArray.GetCount();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (SIZE_T i = 0; i < cItems; i++) ...{
XShellItem& item = itemArray[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (item.icon) ...{
item.x = x + (iconSpacingWidth - iconWidth) / 2;
item.y = y;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (item.nameIcon) ...{
item.nameX = x;
item.nameY = y + iconHeight + 2;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WTL::CRect rect;
GetClientRect(&rect);
y += iconSpacingHeight;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (y + iconSpacingHeight >= rect.bottom) ...{
x += iconSpacingWidth;
y = OFFSET_Y;
}
}
}
描绘图标就很简单了,呵呵,不贴了,下面来说说弹出图标菜单,执行图标对应的程序以及弹出桌面菜单。
执行图标对应的程序,需要以先前保持的图标itemid作为参数,代码如下:
void RunShellItem(ITEMIDLIST* pIID)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
SHELLEXECUTEINFO info;
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_INVOKEIDLIST;
info.hwnd = m_hWnd;
info.lpVerb = NULL;
info.lpFile = NULL;
info.lpParameters = NULL;
info.lpDirectory = NULL;
info.nShow = SW_SHOWNORMAL;
info.hInstApp = NULL;
info.lpIDList = pIID;
ShellExecuteEx(&info);
}
弹出桌面菜单的代码如下:
// 桌面菜单
void DesktopMenu()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HWND program = FindWindowEx(0, 0, _T("Progman"), _T("Program Manager"));
HWND view = FindWindowEx(program, 0, _T("SHELLDLL_DefView"), 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//HWND list = FindWindowEx(view, 0, _T("SysListView32"), 0);
::SetForegroundWindow(view);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
POINT pt;
GetCursorPos(&pt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LPARAM lp = pt.y << 16 | (pt.x - 32);
::PostMessage(view, WM_LBUTTONDOWN, 0, lp);
::PostMessage(view, WM_RBUTTONUP, 0, lp);
}
弹出图标菜单的代码如下,这里定义了两个全局的IContextMenu对象:
static IContextMenu2* g_pIContext2 = NULL;
static IContextMenu3* g_pIContext3 = NULL;
以便在消息回调函数中使用。具体代码如下:
// 图标菜单
void RightMenu(ITEMIDLIST* pIID)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
HWND hwnd = m_hWnd;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LPCONTEXTMENU pContextMenu = NULL;
LPCONTEXTMENU pCtxMenuTemp = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g_pIContext2 = NULL;
g_pIContext3 = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int menuType = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HRESULT hRslt = folder->GetUIObjectOf(
hwnd,
1,
(LPCITEMIDLIST*) &(pIID),
IID_IContextMenu,
0,
(void**) &pCtxMenuTemp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (FAILED(hRslt)) ...{
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
POINT pt;
GetCursorPos(&pt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (pCtxMenuTemp->QueryInterface(IID_IContextMenu3, (void**) &pContextMenu) == NOERROR) ...{
menuType = 3;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (pCtxMenuTemp->QueryInterface(IID_IContextMenu2, (void**) &pContextMenu) == NOERROR) ...{
menuType = 2;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (pContextMenu) ...{
pCtxMenuTemp->Release();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else ...{
pContextMenu = pCtxMenuTemp;
menuType = 1;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (menuType == 0) ...{
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HMENU hMenu = CreatePopupMenu();
hRslt = pContextMenu->QueryContextMenu(hMenu, 0, 1, 0x7fff, CMF_NORMAL | CMF_EXPLORE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (FAILED(hRslt)) ...{
return;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifndef _WIN64
#pragma warning(disable : 4244 4311)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// subclass window
WNDPROC oldWndProc = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (menuType > 1) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// only subclass if it is IID_IContextMenu2 or IID_IContextMenu3
oldWndProc = (WNDPROC) SetWindowLongPtr(GWL_WNDPROC, (LONG) HookWndProc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (menuType == 2) ...{
g_pIContext2 = (LPCONTEXTMENU2) pContextMenu;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else ...{
g_pIContext3 = (LPCONTEXTMENU3) pContextMenu;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else ...{
oldWndProc = NULL;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int cmd = ::TrackPopupMenu(
hMenu,
TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
pt.x,
pt.y,
0,
hwnd,
0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// unsubclass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oldWndProc) ...{
SetWindowLongPtr(GWL_WNDPROC, (LONG) oldWndProc);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifndef _WIN64
#pragma warning(default : 4244 4311)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (cmd != 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
CMINVOKECOMMANDINFO ci = ...{ 0};
ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
ci.hwnd = hwnd;
ci.lpVerb = (LPCSTR) MAKEINTRESOURCE(cmd - 1);
ci.nShow = SW_SHOWNORMAL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pContextMenu->InvokeCommand(&ci);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pContextMenu->Release();
g_pIContext2 = NULL;
g_pIContext3 = NULL;
::DestroyMenu(hMenu);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
static LRESULT CALLBACK HookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
switch (message) ...{
case WM_MENUCHAR: // only supported by IContextMenu3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (g_pIContext3) ...{
LRESULT lResult = 0;
g_pIContext3->HandleMenuMsg2(message, wParam, lParam, &lResult);
return(lResult);
}
break;
case WM_DRAWITEM:
case WM_MEASUREITEM:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (wParam) ...{
break; // if wParam != 0 then the message is not menu-related
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case WM_INITMENUPOPUP:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (g_pIContext2) ...{
g_pIContext2->HandleMenuMsg(message, wParam, lParam);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else ...{
g_pIContext3->HandleMenuMsg(message, wParam, lParam);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(message == WM_INITMENUPOPUP ? 0 : TRUE);
break;
default:
break;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ::CallWindowProc((WNDPROC) GetProp(hWnd, TEXT("oldWndProc")), hWnd, message, wParam, lParam);
}