📄 toolmenu.cpp
字号:
#include "stdafx.h"
#include "ToolMenu.h"
#include "afxpriv.h"
#include "MenuData.h"
//
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//##############################################################
#define MENU_SPACETEXT 1//10
#define MENU_TOPFACTORS 0//2
#define MENU_LEFTFACTORS 0//4
#define MENU_RIGHTFACTORS 0//4
#define MENU_BOTTOMFACTORS 0//2
#define GAP 1
//##############################################################
int m_selectcheck = -1;
int m_iconX=16;
int m_iconY=16;
CImageList *checkmaps=NULL;
int m_unselectcheck = -1;
int disable_old_style=FALSE;
int original_select_disabled=TRUE;
int m_nMenuColorMode=1;
//##############################################################
struct CMenuItemInfo : public
#ifndef UNICODE //SK: this fixes warning C4097: typedef-name 'MENUITEMINFO' used as synonym for class-name 'tagMENUITEMINFOA'
tagMENUITEMINFOA
#else
tagMENUITEMINFOW
#endif
{
CMenuItemInfo()
{
memset(this, 0, sizeof(MENUITEMINFO));
cbSize = sizeof(MENUITEMINFO);
}
};
CToolMenu::CToolMenu()
{
}
CToolMenu::~CToolMenu()
{
try{
DestroyMenuData(m_hMenu,0);
}catch(...){}
}
enum Win32Type{
Win32s,
Windoze95,
WinNT3,
WinNT4orHigher
};
Win32Type IsShellType()
{
Win32Type ShellType;
DWORD winVer;
OSVERSIONINFO *osvi;
winVer=GetVersion();
if(winVer<0x80000000){/*NT */
ShellType=WinNT3;
osvi= (OSVERSIONINFO *)malloc(sizeof(OSVERSIONINFO));
if (osvi!=NULL){
memset(osvi,0,sizeof(OSVERSIONINFO));
osvi->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(osvi);
if (osvi->dwMajorVersion>=4L)
ShellType=WinNT4orHigher;//yup, it is NT 4 or higher!
free(osvi);
}
}
else if (LOBYTE(LOWORD(winVer))<4)
ShellType=Win32s;/*Win32s*/
else
ShellType=Windoze95;/*Windoze95*/
return ShellType;
}
static Win32Type g_Shell=IsShellType();
BOOL IsNewShell ()
{
return (Windoze95==g_Shell || WinNT4orHigher==g_Shell);
}
COLORREF LightenColor(COLORREF col,double factor)
{
if(factor>0.0&&factor<=1.0){
BYTE red,green,blue,lightred,lightgreen,lightblue;
red = GetRValue(col);
green = GetGValue(col);
blue = GetBValue(col);
lightred = (BYTE)((factor*(255-red)) + red);
lightgreen = (BYTE)((factor*(255-green)) + green);
lightblue = (BYTE)((factor*(255-blue)) + blue);
col = RGB(lightred,lightgreen,lightblue);
}
return(col);
}
void DitherBlt (HDC hdcDest, int nXDest, int nYDest, int nWidth,
int nHeight, HBITMAP hbm, int nXSrc, int nYSrc,
COLORREF bgcolor)
{
ASSERT(hdcDest && hbm);
ASSERT(nWidth > 0 && nHeight > 0);
// Create a generic DC for all BitBlts
HDC hDC = CreateCompatibleDC(hdcDest);
ASSERT(hDC);
if (hDC)
{
// Create a DC for the monochrome DIB section
HDC bwDC = CreateCompatibleDC(hDC);
ASSERT(bwDC);
if (bwDC)
{
// Create the monochrome DIB section with a black and white palette
struct {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[2];
} RGBBWBITMAPINFO = {
{ // a BITMAPINFOHEADER
sizeof(BITMAPINFOHEADER), // biSize
nWidth, // biWidth;
nHeight, // biHeight;
1, // biPlanes;
1, // biBitCount
BI_RGB, // biCompression;
0, // biSizeImage;
0, // biXPelsPerMeter;
0, // biYPelsPerMeter;
0, // biClrUsed;
0 // biClrImportant;
},
{
{ 0x00, 0x00, 0x00, 0x00 }, { 0xFF, 0xFF, 0xFF, 0x00 }
}
};
VOID *pbitsBW;
HBITMAP hbmBW = CreateDIBSection(bwDC,
(LPBITMAPINFO)&RGBBWBITMAPINFO, DIB_RGB_COLORS, &pbitsBW, NULL, 0);
ASSERT(hbmBW);
if (hbmBW)
{
// Attach the monochrome DIB section and the bitmap to the DCs
HBITMAP olddib = (HBITMAP)SelectObject(bwDC, hbmBW);
HBITMAP hdcolddib = (HBITMAP)SelectObject(hDC, hbm);
// BitBlt the bitmap into the monochrome DIB section
BitBlt(bwDC, 0, 0, nWidth, nHeight, hDC, nXSrc, nYSrc, SRCCOPY);
// Paint the destination rectangle in gray
FillRect(hdcDest, CRect(nXDest, nYDest, nXDest + nWidth, nYDest +
nHeight), GetSysColorBrush(bgcolor));
//SK: looks better on the old shell
// BitBlt the black bits in the monochrome bitmap into COLOR_3DHILIGHT
// bits in the destination DC
// The magic ROP comes from the Charles Petzold's book
HBRUSH hb = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
HBRUSH oldBrush = (HBRUSH)SelectObject(hdcDest, hb);
BitBlt(hdcDest,nXDest+1,nYDest+1,nWidth,nHeight,bwDC,0,0,0xB8074A);
// BitBlt the black bits in the monochrome bitmap into COLOR_3DSHADOW
// bits in the destination DC
hb = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
DeleteObject(SelectObject(hdcDest, hb));
BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight,bwDC,0,0,0xB8074A);
DeleteObject(SelectObject(hdcDest, oldBrush));
VERIFY(DeleteObject(SelectObject(bwDC, olddib)));
SelectObject(hDC, hdcolddib);
}
VERIFY(DeleteDC(bwDC));
}
VERIFY(DeleteDC(hDC));
}
}
void DitherBlt2(CDC *drawdc, int nXDest, int nYDest, int nWidth,
int nHeight, CBitmap &bmp, int nXSrc, int nYSrc,
COLORREF bgcolor)
{
// create a monochrome memory DC
CDC ddc;
ddc.CreateCompatibleDC(0);
CBitmap bwbmp;
bwbmp.CreateCompatibleBitmap(&ddc, nWidth, nHeight);
CBitmap * pddcOldBmp = ddc.SelectObject(&bwbmp);
CDC dc;
dc.CreateCompatibleDC(0);
CBitmap * pdcOldBmp = dc.SelectObject(&bmp);
// build a mask
ddc.PatBlt(0, 0, nWidth, nHeight, WHITENESS);
dc.SetBkColor(GetSysColor(COLOR_BTNFACE));
ddc.BitBlt(0, 0, nWidth, nHeight, &dc, nXSrc,nYSrc, SRCCOPY);
dc.SetBkColor(GetSysColor(COLOR_BTNHILIGHT));
ddc.BitBlt(0, 0, nWidth, nHeight, &dc, nXSrc,nYSrc, SRCPAINT);
// Copy the image from the toolbar into the memory DC
// and draw it (grayed) back into the toolbar.
dc.FillSolidRect(0,0, nWidth, nHeight, bgcolor);
//SK: Looks better on the old shell
dc.SetBkColor(RGB(0, 0, 0));
dc.SetTextColor(RGB(255, 255, 255));
CBrush brShadow, brHilight;
brHilight.CreateSolidBrush(GetSysColor(COLOR_BTNHILIGHT));
brShadow.CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW));
CBrush * pOldBrush = dc.SelectObject(&brHilight);
dc.BitBlt(0,0, nWidth, nHeight, &ddc, 0, 0, 0x00E20746L);
drawdc->BitBlt(nXDest+1,nYDest+1,nWidth, nHeight, &dc,0,0,SRCCOPY);
dc.BitBlt(1,1, nWidth, nHeight, &ddc, 0, 0, 0x00E20746L);
dc.SelectObject(&brShadow);
dc.BitBlt(0,0, nWidth, nHeight, &ddc, 0, 0, 0x00E20746L);
drawdc->BitBlt(nXDest,nYDest,nWidth, nHeight, &dc,0,0,SRCCOPY);
// reset DCs
ddc.SelectObject(pddcOldBmp);
ddc.DeleteDC();
dc.SelectObject(pOldBrush);
dc.SelectObject(pdcOldBmp);
dc.DeleteDC();
brShadow.DeleteObject();
brHilight.DeleteObject();
bwbmp.DeleteObject();
}
BOOL GetBitmapFromImageList(CDC* pDC,CImageList *imglist,int nIndex,CBitmap &bmp)
{
HICON hIcon = imglist->ExtractIcon(nIndex);
CDC dc;
dc.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC,m_iconX,m_iconY);
CBitmap* pOldBmp = dc.SelectObject(&bmp);
CBrush brush ;
brush.CreateSolidBrush(GetSysColor(COLOR_MENU));
::DrawIconEx(
dc.GetSafeHdc(),
0,
0,
hIcon,
m_iconX,
m_iconY,
0,
(HBRUSH)brush,
DI_NORMAL
);
dc.SelectObject( pOldBmp );
dc.DeleteDC();
// the icon is not longer needed
DestroyIcon(hIcon);
return(TRUE);
}
void DrawCheckMark(CDC* pDC,int x,int y,COLORREF color)
{
CPen m_penBack;
m_penBack.CreatePen (PS_SOLID,0,color);
CPen *pOldPen = pDC->SelectObject (&m_penBack);
pDC->MoveTo(x,y+2);
pDC->LineTo(x,y+5);
pDC->MoveTo(x+1,y+3);
pDC->LineTo(x+1,y+6);
pDC->MoveTo(x+2,y+4);
pDC->LineTo(x+2,y+7);
pDC->MoveTo(x+3,y+3);
pDC->LineTo(x+3,y+6);
pDC->MoveTo(x+4,y+2);
pDC->LineTo(x+4,y+5);
pDC->MoveTo(x+5,y+1);
pDC->LineTo(x+5,y+4);
pDC->MoveTo(x+6,y);
pDC->LineTo(x+6,y+3);
pDC->SelectObject (pOldPen);
m_penBack.DeleteObject();
}
void DrawRadioDot(CDC *pDC,int x,int y,COLORREF color)
{
CRect rcDot(x,y,x+6,y+6);
CBrush brush;
CPen pen;
brush.CreateSolidBrush(color);
pen.CreatePen(PS_SOLID,0,color);
CBrush *pOldBrush=pDC->SelectObject(&brush);
CPen *pOldPen=pDC->SelectObject(&pen);
pDC->Ellipse(&rcDot);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
pen.DeleteObject();
brush.DeleteObject();
}
BOOL Draw3DCheckmark(CDC *dc, const CRect& rc,
BOOL bSelected, HBITMAP hbmCheck)
{
CRect rcDest = rc;
CBrush brush;
//COLORREF col=GetSysColor((bSelected||!IsNewShell())?COLOR_MENU:COLOR_3DLIGHT);//SK: Looks better on the old shell
COLORREF col;//
if (bSelected||!IsNewShell())
col=GetSysColor(COLOR_MENU);
else
col=RGB(233,233,233);
brush.CreateSolidBrush(col);
dc->FillRect(rcDest,&brush);
brush.DeleteObject();
if (IsNewShell()) //SK: looks better on the old shell
dc->DrawEdge(&rcDest, BDR_SUNKENOUTER, BF_RECT);
if (!hbmCheck)DrawCheckMark(dc,rc.left+7,rc.top+7,GetSysColor(COLOR_MENUTEXT));
else DrawRadioDot(dc,rc.left+7,rc.top+7,GetSysColor(COLOR_MENUTEXT));
return TRUE;
}
BOOL DrawXPCheckmark(CDC *dc, const CRect& rc, HBITMAP hbmCheck,COLORREF &colorout)
{
CBrush brushin;
brushin.CreateSolidBrush(LightenColor(colorout,0.85));
dc->FillRect(rc,&brushin);
brushin.DeleteObject();
dc->Draw3dRect (rc,colorout,colorout);
if (!hbmCheck)DrawCheckMark(dc,rc.left+5,rc.top+4,GetSysColor(COLOR_MENUTEXT));
else DrawRadioDot(dc,rc.left+5,rc.top+4,GetSysColor(COLOR_MENUTEXT));
return TRUE;
}
void DrawItem_Win9xNT2000 (LPDRAWITEMSTRUCT lpDIS)
{
ASSERT(lpDIS != NULL);
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
//?
CMenuData* pmd = (CMenuData*)(lpDIS->itemData);
if(pmd->m_pImgList != NULL)
::ImageList_GetIconSize(*(pmd->m_pImgList), &m_iconX, &m_iconY);
else if (lpDIS->itemID!=0)//for fav bar not have image
{
m_iconX=16;
m_iconY=16;
}
//
if (m_iconX==11)//for tab icon 11*16
{
m_iconX=16;
m_iconY=16;
}
//special,when SEPARATOR,itemState is 6
if (lpDIS->itemID==0)//if (pmd->m_pImgList==NULL)
lpDIS->itemState = MF_SEPARATOR;
CRect rect;
UINT state = lpDIS->itemState;//?(((BCMenuData*)(lpDIS->itemData))->nFlags);
CBrush m_brBackground;
m_brBackground.CreateSolidBrush(GetSysColor((IsNewShell())?COLOR_3DFACE:COLOR_MENU));
// remove the selected bit if it's grayed out
if(lpDIS->itemState & ODS_GRAYED&&!original_select_disabled)
{
if(lpDIS->itemState & ODS_SELECTED)
lpDIS->itemState=lpDIS->itemState & ~ODS_SELECTED;
}
if(state & MF_SEPARATOR)
{
rect.CopyRect(&lpDIS->rcItem);
pDC->FillRect (rect,&m_brBackground);
rect.top += (rect.Height()>>1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -