MFC ListCtrl 如何实现像任务管理器一样选中高亮的风格。 mfc 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个需要自绘CListCtrl控件,codeproject上很多实例 可以自绘实现,派生CListCtrl类,添加处理虚函数DrawItem SDK://////////////////////////////////////////////////////////////LRESULT ListViewCustomDraw(HWND hwnd, LPARAM lParam){ LPNMHDR pnmh = (LPNMHDR) lParam; if (pnmh->code != NM_CUSTOMDRAW) return 0; LPNMLVCUSTOMDRAW lpNMCustomDraw = (LPNMLVCUSTOMDRAW) lParam; int nResult = CDRF_DODEFAULT; if (CDDS_PREPAINT == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_NOTIFYITEMDRAW; } else if (CDDS_ITEMPREPAINT == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT; } else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_SKIPDEFAULT; const DWORD dwStyle = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS; HDC hdc = lpNMCustomDraw->nmcd.hdc; SetBkMode(hdc,TRANSPARENT); int nItem = (int)lpNMCustomDraw->nmcd.dwItemSpec; int nSubItem = lpNMCustomDraw->iSubItem; BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED); RECT subItemRect; ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect);// HBRUSH brsh=0; if (bItemSelected) { //OutputDebugString("bItemSelected\n"); brsh=CreateSolidBrush(RGB(255, 128, 128)); FillRect(hdc, &subItemRect,brsh); } else {// not Selected brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30)); FillRect(hdc, &subItemRect,brsh); } if(brsh) DeleteObject(brsh);// if(nSubItem==0) {//OutputDebugString("bmp\n"); RECT iconRect; ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_ICON, &iconRect); OffsetRect(&iconRect, -1, 0); HBITMAP oldbmp=(HBITMAP)SelectObject(g_hMemDC,g_hbmNormal); BitBlt(hdc,iconRect.left, iconRect.top, 16, 16,g_hMemDC,0,0,SRCCOPY); SelectObject(hdc,oldbmp); }// TCHAR szText[260]; ListView_GetItemText(hwnd, nItem, nSubItem, szText, 260); OffsetRect(&subItemRect, 18, 0); DrawText(hdc, szText, strlen(szText), &subItemRect, dwStyle); return nResult; } else if (CDDS_ITEMPOSTPAINT == lpNMCustomDraw->nmcd.dwDrawStage) {// draw (horizantal line) HPEN hpen=CreatePen(PS_SOLID,1,RGB(0,255,0)); HPEN holdpen=(HPEN)SelectObject(lpNMCustomDraw->nmcd.hdc,hpen); RECT crc; GetClientRect(hwnd,&crc); RECT irc; ListView_GetItemRect(hwnd, lpNMCustomDraw->nmcd.dwItemSpec,&irc,LVIR_BOUNDS); HDC hdc = lpNMCustomDraw->nmcd.hdc; MoveToEx(hdc,0,irc.top,0); LineTo(hdc,crc.right-crc.left,irc.top); SelectObject(hdc,holdpen); // return CDRF_DODEFAULT; } return nResult;} ListViewCustomDraw 这个问题已经回答很多次了,可惜没一个人对这个有兴趣。有一次还被向立天以无满意答案而结贴,我实在无语了。typedef HRESULT (WINAPI *SETWINDOWTHEME)(HWND, LPCWSTR, LPCWSTR);// 纯 C 风格HMODULE hinstDLL = LoadLibrary(TEXT("uxtheme.dll"));if (hinstDLL != NULL) { SETWINDOWTHEME pfnSWT = (SETWINDOWTHEME)GetProcAddress(hinstDLL, "SetWindowTheme"); if (pfnSWT != NULL) { (*pfnSWT)(hwndList, L"explorer", NULL); } FreeLibrary(hinstDLL);}// 在 MFC 中使用HMODULE hinstDLL = ::LoadLibrary(TEXT("uxtheme.dll"));if (hinstDLL != NULL) { SETWINDOWTHEME pfnSWT = (SETWINDOWTHEME)::GetProcAddress(hinstDLL, "SetWindowTheme"); if (pfnSWT != NULL) { (*pfnSWT)(m_List.GetSafeHwnd(), L"explorer", NULL); } ::FreeLibrary(hinstDLL);} 这本功能本来就是系统提供的,并且 MSDN 上也说过:当然,前提是你系统开启了主题并且界面也启用了主题效果(右)。如果还是 Windows 2000 的那种窗口(左),你用这个代码没效果: 可不可在内存中生成一个虚拟文件,能够进写读写操作? 大家好,第一次来,请教一个问题 VC中有没有功能和TC中的outport()一样的函数?? 播放视频抖动的问题,帮忙啊。。。 有模拟MSN好友列表的源码嘛?! 求教以下代码有没有错误 在线请教,急呀!(问题很简单) 大家来看看,这是什么问题? List Control里面删除一行后出现的问题。。。 在VC中有什么方法可以读取光盘的某磁道某扇区的内容???? 求大神帮忙啊,vs2010出现问题了 VS2012 + Win7 写的ATL DLL,在XP下无法注册,急急急~
LRESULT ListViewCustomDraw(HWND hwnd, LPARAM lParam)
{
LPNMHDR pnmh = (LPNMHDR) lParam;
if (pnmh->code != NM_CUSTOMDRAW) return 0;
LPNMLVCUSTOMDRAW lpNMCustomDraw = (LPNMLVCUSTOMDRAW) lParam; int nResult = CDRF_DODEFAULT;
if (CDDS_PREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
{
nResult = CDRF_NOTIFYITEMDRAW;
}
else if (CDDS_ITEMPREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
{
nResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT;
}
else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == lpNMCustomDraw->nmcd.dwDrawStage)
{
nResult = CDRF_SKIPDEFAULT;
const DWORD dwStyle = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS;
HDC hdc = lpNMCustomDraw->nmcd.hdc;
SetBkMode(hdc,TRANSPARENT);
int nItem = (int)lpNMCustomDraw->nmcd.dwItemSpec;
int nSubItem = lpNMCustomDraw->iSubItem;
BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED);
RECT subItemRect;
ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect);
//
HBRUSH brsh=0;
if (bItemSelected)
{ //OutputDebugString("bItemSelected\n");
brsh=CreateSolidBrush(RGB(255, 128, 128));
FillRect(hdc, &subItemRect,brsh);
}
else
{// not Selected
brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30));
FillRect(hdc, &subItemRect,brsh);
}
if(brsh) DeleteObject(brsh);
//
if(nSubItem==0)
{//OutputDebugString("bmp\n");
RECT iconRect;
ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_ICON, &iconRect);
OffsetRect(&iconRect, -1, 0);
HBITMAP oldbmp=(HBITMAP)SelectObject(g_hMemDC,g_hbmNormal);
BitBlt(hdc,iconRect.left, iconRect.top, 16, 16,g_hMemDC,0,0,SRCCOPY);
SelectObject(hdc,oldbmp);
}
//
TCHAR szText[260];
ListView_GetItemText(hwnd, nItem, nSubItem, szText, 260);
OffsetRect(&subItemRect, 18, 0);
DrawText(hdc, szText, strlen(szText), &subItemRect, dwStyle);
return nResult;
}
else if (CDDS_ITEMPOSTPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
{// draw (horizantal line)
HPEN hpen=CreatePen(PS_SOLID,1,RGB(0,255,0));
HPEN holdpen=(HPEN)SelectObject(lpNMCustomDraw->nmcd.hdc,hpen);
RECT crc;
GetClientRect(hwnd,&crc);
RECT irc;
ListView_GetItemRect(hwnd, lpNMCustomDraw->nmcd.dwItemSpec,&irc,LVIR_BOUNDS);
HDC hdc = lpNMCustomDraw->nmcd.hdc;
MoveToEx(hdc,0,irc.top,0);
LineTo(hdc,crc.right-crc.left,irc.top);
SelectObject(hdc,holdpen);
//
return CDRF_DODEFAULT;
}
return nResult;
}
ListViewCustomDraw
typedef HRESULT (WINAPI *SETWINDOWTHEME)(HWND, LPCWSTR, LPCWSTR);
// 纯 C 风格
HMODULE hinstDLL = LoadLibrary(TEXT("uxtheme.dll"));
if (hinstDLL != NULL) {
SETWINDOWTHEME pfnSWT = (SETWINDOWTHEME)GetProcAddress(hinstDLL, "SetWindowTheme");
if (pfnSWT != NULL) {
(*pfnSWT)(hwndList, L"explorer", NULL);
}
FreeLibrary(hinstDLL);
}
// 在 MFC 中使用
HMODULE hinstDLL = ::LoadLibrary(TEXT("uxtheme.dll"));
if (hinstDLL != NULL) {
SETWINDOWTHEME pfnSWT = (SETWINDOWTHEME)::GetProcAddress(hinstDLL, "SetWindowTheme");
if (pfnSWT != NULL) {
(*pfnSWT)(m_List.GetSafeHwnd(), L"explorer", NULL);
}
::FreeLibrary(hinstDLL);
}
这本功能本来就是系统提供的,并且 MSDN 上也说过:当然,前提是你系统开启了主题并且界面也启用了主题效果(右)。
如果还是 Windows 2000 的那种窗口(左),你用这个代码没效果: