📄 newmenu.cpp
字号:
//------------------------------------------------------------------------------
// File : NewMenu.cpp
// Version : 1.18
// Date : 30. November 2003
// Author : Bruno Podetti
// Email : Podetti@gmx.net
// Web : www.podetti.com/NewMenu
// Systems : VC6.0/7.0 and VC7.1 (Run under (Window 98/ME), Windows Nt 2000/XP)
//
// For bugreport please add following informations
// - The CNewMenu version number (Example CNewMenu 1.16)
// - Operating system Win95 / WinXP and language (English / Japanese / German etc.)
// - Intalled service packs
// - Short description how to reproduce the bug
// - Pictures/Sample are wellcome too
// - You can write in English or German to the above email-address.
// - Have my compiled examples the same effect?
//
// Special
// Compiled version with visual studio V7.0/7.1 doesn't run correctly on
// WinNt 4.0 nor Windows 95. For those platforms, you have to compile it with
// visual studio V6.0.
// (It may run with 7.0/7.1, but you must be sure to have all required and newest
// dll from the MFC installed => otherwise menu painting problems)
// Keep in mind you should have installed latest service-packs to the operating-
// system too! (And a new IE because some fixes of common components were made)
//
// ToDo's
// checking GDI-Leaks, are there some more?
// Shade-Drawing, when underground is changing
// Border repainting after menu closing!!!
// Shade painting problems? On Chinese or Japanese systems?
// ALT-Key-support in menubar
//
// 30. November 2003 (Version 1.18)
// minor changes for CNewToolBar
//
// 11. September 2003 Bug Fixes or nice hints from 1.16
// Better handling of menubar brushes (never destroy) Style_XP and STYLE_XP_2003
// Drawing of menuicons on WinNt 4.0 fixed
// spelling & initializing & unused brushes fixed, thanks to John Zegers
//
// 12. July 2003 Bug Fixes or nice hints from 1.15
// Added gradient-Dockbar (CNewDockBar) for office 2003 style
// (you need to define USE_NEW_DOCK_BAR)
// fixed menu checkmarks with bitmaps (Selection-with is not correct on all styles)
// Drawing to a memory-DC minimizing flickering
// with of menu-bar-item without & now better
//
// 22. June 2003 Bug Fixes or nice hints from 1.14
// Shortkey-handling for ModifyODMenu and InsertODMenu
// Change os identifer for OS>5.0 to WinXP
// Color scheme for Office menu changed and updating menubarcolor too
// Gradientcolor for Office 2003 bar
// XP-changing settings Border Style => fixed border painting without restarting
//
// 23. April 2003 Bug Fixes or nice hints from 1.13
// Fixed the window menu with extreme width, thanks to Voy
// Fix bug about shortkey for added menu item with AppendMenu, thanks to Mehdy Bohlool(Zy)
// Measureitem for default item corrected, thanks to LordLiverpool
// New menu style office 2003, but color schema is not tested, changing of menubar color
// on win95 and WinNt is not possible
// Menubardrawing on deactivated app corrected for win95 & WinNt4.0
//
// 23. January 2003 Bug Fixes or nice hints from 1.12
// Fixed SetMenuText, thanks to Eugene Plokhov
// Easier for having an other menu font, but on your own risk ;)
// Added new function SetNewMenuBorderAllMenu for borderhandling off non CNewMenu
//
// 22. December 2002 Bug Fixes or nice hints from 1.11
// Added some new tracing stuffs
// fixed SetMenuText
// XP-Style menu more office style and better handling with less 256 color, thanks to John Zegers
// Close window button disabling under XP with theme fixed
// Menubar color with theme fixed, thanks to Luther Weeks
//
// (9. November) Bug Fixes or nice hints from 1.11b
// Some new function for bleaching bitmaps, thanks to John Zegers
// GetSubMenu(LPCTSTR) , thanks to George Menhorn
// Menu animation under Win 98 fixed
// Borderdrawing a little better also under win 98
// Added new menustyle ICY
//
// (21. August 2002) Bug Fixes or nice hints from 1.10
// Bugfix for Windows NT 4.0 System Bitmaps, thanks to Christoph Weber
//
// (30. July 2002) Bug Fixes or nice hints from 1.03
// Color of checked icons / selection marks more XP-look
// Updating images 16 color by changing systems colors
// Icon drawing with more highlighting and half transparency
// Scroller for large menu correcting (but the overlapping border is not corrected)
// Resource-problem on windows 95/98/ME?, when using too much bitmaps in menus
// changing the kind of storing bitmaps (images list)
// Calculating the size for a menuitem (specially submenuitem) corrected.
// Recentfilelist corrected (Seperator gots a wrong ID number)
//
// (25. June 2002) Bug Fixes or nice hints from 1.02
// Adding removing menu corrected for drawing the shade in menubar
// Draw the menuborder corrected under win 98 at the bottom side
// Highlighting under 98 for menu corrected (Assert), Neville Franks
// Works also on Windows 95
// Changing styles on XP menucolor of the bar now corrected.
//
// (18. June 2002) Bug Fixes or nice hints from 1.01
// Popup menu which has more items than will fit, better
// LoadMenu changed for better languagessuport, SnowCat
// Bordercolor & Drawing of XP-Style-Menu changed to more XP-Look
// Added some functions for setting/reading MenuItemData and MenuData
// Menubar highlighting now supported under 98/ME (NT 4.0 and 95?)
// Font for menutitle can be changed
// Bugs for popupmenu by adding submenu corrected, no autodestroy
//
// (6. June 2002) Bug Fixes or nice hints from 1.00
// Loading Icons from a resource dll expanded (LoadToolBar) Jonathan de Halleux, Belgium.
// Minimized resource leak of User and Gdi objects
// Problem of disabled windows button on 98/Me solved
// Gradient-drawing without Msimg32.dll now supported especially for win 95
// Using solid colors for drawing menu items
// GetSubMenu changed to const
// Added helper function for setting popup-flag for popup menu (centered text)
// Redraw menu bar corrected after canceling popup menu in old style menu
//
// (23. Mai 2002) Bug Fixes and portions of code from previous version supplied by:
// Brent Corkum, Ben Ashley, Girish Bharadwaj, Jean-Edouard Lachand-Robert,
// Robert Edward Caldecott, Kenny Goers, Leonardo Zide, Stefan Kuhr,
// Reiner Jung, Martin Vladic, Kim Yoo Chul, Oz Solomonovich, Tongzhe Cui,
// Stephane Clog, Warren Stevens, Damir Valiulin
//
// You are free to use/modify this code but leave this header intact.
// This class is public domain so you are free to use it any of your
// applications (Freeware, Shareware, Commercial).
// All I ask is that you let me know so that if you have a real winner I can
// brag to my buddies that some of my code is in your app. I also wouldn't
// mind if you sent me a copy of your application since I like to play with
// new stuff.
//------------------------------------------------------------------------------
#include "stdafx.h" // Standard windows header file
#include "NewMenu.h" // CNewMenu class declaration
// Those two following constants should be only defined for tracing.
// Normally they are commented out.
//#define _TRACE_MENU_
//#define _TRACE_MENU_LOGFILE
//#define _NEW_MENU_USER_FONT
// For using an userfont you can set the define. It does not work right in the
// systemmenu nor in the menubar. But when you like to use the new menu only in
// popups then it will work, but it is newer a good idea to set or change the
// menufont.
// I do not support this mode, you use it on your own risk. :)
#ifdef _NEW_MENU_USER_FONT
static LOGFONT MENU_USER_FONT = {20, 0, 0, 0, FW_NORMAL,
0, 0, 0, DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH,_T("Comic Sans MS")};
#endif
#ifdef _TRACE_MENU_
#include "MyTrace.h"
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE(hh) (HIWORD(hh)==NULL)
#endif
#define GAP 2
#ifndef COLOR_MENUBAR
#define COLOR_MENUBAR 30
#endif
#ifndef SPI_GETDROPSHADOW
#define SPI_GETDROPSHADOW 0x1024
#endif
#ifndef SPI_GETFLATMENU
#define SPI_GETFLATMENU 0x1022
#endif
// Count of menu icons normal gloomed and grayed
#define MENU_ICONS 3
BOOL bHighContrast = FALSE;
typedef BOOL (WINAPI* FktGradientFill)( IN HDC, IN PTRIVERTEX, IN ULONG, IN PVOID, IN ULONG, IN ULONG);
typedef BOOL (WINAPI* FktIsThemeActive)();
typedef HRESULT (WINAPI* FktSetWindowTheme)(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList);
FktGradientFill pGradientFill = NULL;
FktIsThemeActive pIsThemeActive = NULL;
FktSetWindowTheme pSetWindowTheme = NULL;
/////////////////////////////////////////////////////////////////////////////
// Forwarddeclaration
void DrawGradient(CDC* pDC,CRect& Rect,
COLORREF StartColor,COLORREF EndColor,
BOOL bHorizontal,BOOL bUseSolid=FALSE);
COLORREF DarkenColorXP(COLORREF color);
COLORREF DarkenColor( long lScale, COLORREF lColor);
COLORREF LightenColor( long lScale, COLORREF lColor);
/////////////////////////////////////////////////////////////////////////////
// Helper datatypes
class CToolBarData
{
public:
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
//WORD aItems[wItemCount]
WORD* items()
{ return (WORD*)(this+1); }
};
class CNewMenuIconInfo
{
public:
WORD wBitmapID;
WORD wWidth;
WORD wHeight;
WORD* ids(){ return (WORD*)(this+1); }
};
// Helpers for casting
__inline HMENU UIntToHMenu(const unsigned int ui )
{
return( (HMENU)(UINT_PTR)ui );
}
__inline HMENU HWndToHMenu(const HWND hWnd )
{
return( (HMENU)hWnd );
}
__inline HWND HMenuToHWnd(const HMENU hMenu)
{
return( (HWND)hMenu );
}
__inline UINT HWndToUInt(const HWND hWnd )
{
return( (UINT)(UINT_PTR) hWnd);
}
__inline HWND UIntToHWnd(const UINT hWnd )
{
return( (HWND)(UINT_PTR) hWnd);
}
__inline UINT HMenuToUInt(const HMENU hMenu )
{
return( (UINT)(UINT_PTR) hMenu);
}
static void ShowLastError()
{
DWORD error = GetLastError();
if(error)
{
LPVOID lpMsgBuf=NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
if(lpMsgBuf)
{
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("Error"), MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
else
{
CString temp;
temp.Format(_T("Error message 0x%lx not found"),error);
// Display the string.
MessageBox( NULL,temp, _T("Error"), MB_OK | MB_ICONINFORMATION );
}
}
}
UINT GetSafeTimerID(HWND hWnd, const UINT uiElapse)
{
UINT nNewTimerId = NULL;
if(IsWindow(hWnd))
{ // Try to start with TimerID 31, normaly is used 1, 10, or 100
for(UINT nTimerId=31; nTimerId<100 && nNewTimerId==NULL; nTimerId++)
{ // Try to set a timer each uiElapse ms
nNewTimerId = (UINT)::SetTimer(hWnd,nTimerId,uiElapse,NULL);
}
// Wow you have more than 69 timers!!!
// Try to increase nTimerId<100 to nTimerId<1000;
ASSERT(nNewTimerId);
}
return nNewTimerId;
}
WORD NumBitmapColors(LPBITMAPINFOHEADER lpBitmap)
{
if ( lpBitmap->biClrUsed != 0)
return (WORD)lpBitmap->biClrUsed;
switch (lpBitmap->biBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
}
return 0;
}
static int NumScreenColors()
{
static int nColors = 0;
if (!nColors)
{
// DC of the desktop
CClientDC myDC(NULL);
nColors = myDC.GetDeviceCaps(NUMCOLORS);
if (nColors == -1)
{
nColors = 64000;
}
}
return nColors;
}
HBITMAP LoadColorBitmap(LPCTSTR lpszResourceName, HMODULE hInst, int* pNumcol)
{
if(hInst==0)
{
hInst = AfxFindResourceHandle(lpszResourceName, RT_BITMAP);
}
HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(lpszResourceName),RT_BITMAP);
if (hRsrc == NULL)
return NULL;
// determine how many colors in the bitmap
HGLOBAL hglb;
if ((hglb = LoadResource(hInst, hRsrc)) == NULL)
return NULL;
LPBITMAPINFOHEADER lpBitmap = (LPBITMAPINFOHEADER)LockResource(hglb);
if (lpBitmap == NULL)
return NULL;
WORD numcol = NumBitmapColors(lpBitmap);
if(pNumcol)
{
*pNumcol = numcol;
}
UnlockResource(hglb);
FreeResource(hglb);
return LoadBitmap(hInst,lpszResourceName);
//TODO: check it bpo
//if(numcol!=16)
//{
// return LoadBitmap(hInst,lpszResourceName);
// // (HBITMAP)LoadImage(hInst,lpszResourceName,IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR|LR_CREATEDIBSECTION|LR_SHARED);
//}
//else
//{
// // mapping from color in DIB to system color
// // { RGB_TO_RGBQUAD(0x00, 0x00, 0x00), COLOR_BTNTEXT }, // black
// // { RGB_TO_RGBQUAD(0x80, 0x80, 0x80), COLOR_BTNSHADOW }, // dark gray
// // { RGB_TO_RGBQUAD(0xC0, 0xC0, 0xC0), COLOR_BTNFACE }, // bright gray
// // { RGB_TO_RGBQUAD(0xFF, 0xFF, 0xFF), COLOR_BTNHIGHLIGHT } // white
// return (HBITMAP)AfxLoadSysColorBitmap(hInst,hRsrc,FALSE);
//}
}
#if(WINVER < 0x0500)
class CNewLoadLib
{
public:
HMODULE m_hModule;
void* m_pProg;
CNewLoadLib(LPCTSTR pName,LPCSTR pProgName)
{
m_hModule = LoadLibrary(pName);
m_pProg = m_hModule ? (void*)GetProcAddress (m_hModule,pProgName) : NULL;
}
~CNewLoadLib()
{
if(m_hModule)
{
FreeLibrary(m_hModule);
m_hModule = NULL;
}
}
};
BOOL GetMenuInfo( HMENU hMenu, LPMENUINFO pInfo)
{
static CNewLoadLib menuInfo(_T("user32.dll"),"GetMenuInfo");
if(menuInfo.m_pProg)
{
typedef BOOL (WINAPI* FktGetMenuInfo)(HMENU, LPMENUINFO);
return ((FktGetMenuInfo)menuInfo.m_pProg)(hMenu,pInfo);
}
return FALSE;
}
BOOL SetMenuInfo( HMENU hMenu, LPCMENUINFO pInfo)
{
static CNewLoadLib menuInfo(_T("user32.dll"),"SetMenuInfo");
if(menuInfo.m_pProg)
{
typedef BOOL (WINAPI* FktSetMenuInfo)(HMENU, LPCMENUINFO);
return ((FktSetMenuInfo)menuInfo.m_pProg)(hMenu,pInfo);
}
return FALSE;
}
#endif
class CNewBrushList : public CObList
{
public:
CNewBrushList(){}
~CNewBrushList()
{
while(!IsEmpty())
{
delete RemoveTail();
}
}
};
class CNewBrush : public CBrush
{
public:
UINT m_nMenuDrawMode;
COLORREF m_BarColor;
CNewBrush(UINT menuDrawMode, COLORREF barColor):m_nMenuDrawMode(menuDrawMode),m_BarColor(barColor)
{
if(menuDrawMode==CNewMenu::STYLE_XP_2003)
{
// Get the desktop hDC...
HDC hDcDsk = GetWindowDC(0) ;
CDC* pDcDsk = CDC::FromHandle(hDcDsk);
CDC clientDC;
clientDC.CreateCompatibleDC(pDcDsk);
CRect rect(0,0,(GetSystemMetrics(SM_CXFULLSCREEN)+16)&~7,20);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDcDsk,rect.Width(),rect.Height());
CBitmap* pOldBitmap = clientDC.SelectObject(&bitmap);
DrawGradient(&clientDC,rect,barColor,LightenColor(110,barColor),TRUE,TRUE);
clientDC.SelectObject(pOldBitmap);
// Release the desktopdc
ReleaseDC(0,hDcDsk);
CreatePatternBrush(&bitmap);
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -