📄 xpmenu.pas
字号:
{
XPMenu for Delphi
Author: Khaled Shagrouni
URL: http://www.shagrouni.com/english/software/xpmenu.html
e-mail: khaled@shagrouni.com
Version 3.1 - 22.02.2004
XPMenu is a Delphi component to mimic Office XP menu and toolbar style.
Copyright (C) 2001, 2003 Khaled Shagrouni.
This component is FREEWARE with source code. I still hold the copyright, but
you can use it for whatever you like: freeware, shareware or commercial software.
If you have any ideas for improvement or bug reports, don't hesitate to e-mail
me <khaled@shagrouni.com> (Please state the XPMenu version and OS information).
}
{$IFDEF VER130}
{$DEFINE VER5U}
{$ENDIF}
{$IFDEF VER140}
{$DEFINE VER5U}
{$DEFINE VER6U}
{$ENDIF}
{$IFDEF VER150}
{$DEFINE VER5U}
{$DEFINE VER6U}
{$DEFINE VER7U}
{$ENDIF}
unit XPMenu;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, ComCtrls, Forms,
Menus, Commctrl, ExtCtrls, StdCtrls, Buttons;
type
TXPContainer = (xccForm, xccFrame, xccToolbar, xccCoolbar, xccControlbar, xccPanel,
xccScrollBox, xccGroupBox, xccTabSheet, xccPageScroller);
TXPContainers = set of TXPContainer;
TXPControl = (xcMainMenu, xcPopupMenu, xcToolbar, xcControlbar, xcCombo, xcListBox,
xcEdit, xcMaskEdit, xcMemo, xcRichEdit, xcMiscEdit, xcCheckBox,
xcRadioButton, xcButton, xcBitBtn, xcSpeedButton, xcUpDown, xcPanel,
xcGroupBox, xcTreeView, xcListView, xcProgressBar, xcHotKey);
{xcStringGrid, xcDrawGrid, xcDBGrid);}
TXPControls = set of TXPControl;
TXPMenu = class;
TControlSubClass = class(TComponent) //: "Fabian Jakubowski" <fj@sambreville.com>
private
Control: TControl;
FBuilding: boolean;
FMouseInControl: boolean;
FLButtonBressed: boolean;
FBressed: boolean;
FIsKeyDown: boolean;
FIsFocused: boolean;
orgWindowProc: TWndMethod;
XPMenu: TXPMenu;
FCtl3D: boolean;
FBorderStyle: TBorderStyle;
{FOnDrawCell: TDrawCellEvent;}
FDefaultDrawing: boolean;
FSelCol, FSelRow: integer;
FMsg: Cardinal;
procedure ControlSubClass(var Message: TMessage);
procedure PaintControlXP;
procedure PaintCombo;
procedure PaintDBLookupCombo;
procedure PaintEdit;
procedure PaintRichEdit;
procedure PaintCheckBox;
procedure PaintRadio;
procedure PaintButton;
procedure PaintBitButn;
procedure PaintUpDownButton;
procedure PaintSpeedButton;
procedure PaintPanel;
procedure PaintGroupBox;
procedure PaintNCWinControl;
procedure PaintProgressBar;
procedure PaintHotKey;
end;
TXPMenu = class(TComponent)
private
FActive: boolean;
{Changes MMK FForm to TScrollingWinControl}
FForm: TScrollingWinControl;
FFont: TFont;
FColor: TColor;
FIconBackColor: TColor;
FMenuBarColor: TColor;
FCheckedColor: TColor;
FSeparatorColor: TColor;
FSelectBorderColor: TColor;
FSelectColor: TColor;
FDisabledColor: TColor;
FSelectFontColor: TColor;
FIconWidth: integer;
FDrawSelect: boolean;
FUseSystemColors: boolean;
FColorsChanged: boolean; // +jt
FFColor, FFIconBackColor, FFSelectColor, FFSelectBorderColor,
FFSelectFontColor, FCheckedAreaColor, FCheckedAreaSelectColor,
FFCheckedColor, FFMenuBarColor, FFDisabledColor, FFSeparatorColor,
FMenuBorderColor, FMenuShadowColor: TColor;
Is16Bit: boolean;
FOverrideOwnerDraw: boolean;
FGradient: boolean;
FFlatMenu: boolean;
FAutoDetect: boolean;
FXPContainers: TXPContainers;
FXPControls: TXPControls;
FGrayLevel: byte;
FDimLevel: byte;
FDrawMenuBar: boolean;
FUseDimColor: boolean;
FDimParentColor, FDimParentColorSelect: integer;
// FUseParentClor: boolean;
// +jt
FSettingWindowRng: boolean;
FIsW2k: boolean;
FIsWXP: boolean;
FIsWNT: boolean;
// FTransparentColor: TColor;
// +jt
// Do not allow the component to be used for subclassing
FDisableSubclassing: boolean;
procedure SetDisableSubclassing(const Value: boolean);
procedure SetActive(const Value: boolean);
procedure SetAutoDetect(const Value: boolean);
procedure SetForm(const Value: TScrollingWinControl);
procedure SetFont(const Value: TFont);
procedure SetColor(const Value: TColor);
procedure SetIconBackColor(const Value: TColor);
procedure SetMenuBarColor(const Value: TColor);
procedure SetCheckedColor(const Value: TColor);
procedure SetDisabledColor(const Value: TColor);
procedure SetSelectColor(const Value: TColor);
procedure SetSelectBorderColor(const Value: TColor);
procedure SetSeparatorColor(const Value: TColor);
procedure SetSelectFontColor(const Value: TColor);
procedure SetIconWidth(const Value: integer);
procedure SetDrawSelect(const Value: boolean);
procedure SetUseSystemColors(const Value: boolean);
procedure SetOverrideOwnerDraw(const Value: boolean);
procedure SetGradient(const Value: boolean);
procedure SetFlatMenu(const Value: boolean);
procedure SetXPContainers(const Value: TXPContainers);
procedure SetXPControls(const Value: TXPControls);
procedure SetDrawMenuBar(const Value: boolean);
procedure SetUseDimColor(const Value: boolean);
protected
procedure Loaded; override; //add by Cunha, liyang.
procedure InitItems(wForm: TWinControl; Enable, Update: boolean);
procedure InitItem(Comp: TComponent; Enable, Update: boolean); // Tom: "Thomas Knoblauch" <thomas@tom-the-bomb.de> 27.08
procedure DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
Selected: Boolean);
procedure MenueDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
Selected: Boolean);
{$IFDEF VER5U}
procedure ToolBarDrawButton(Sender: TToolBar;
Button: TToolButton; State: TCustomDrawState; var DefaultDraw: Boolean);
{$ENDIF}
procedure ControlBarPaint(Sender: TObject; Control: TControl;
Canvas: TCanvas; var ARect: TRect; var Options: TBandPaintOptions);
procedure SetGlobalColor(ACanvas: TCanvas);
procedure DrawTopMenuItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
BckColor:Tcolor; IsRightToLeft: boolean);
procedure DrawCheckedItem(FMenuItem: TMenuItem; Selected, Enabled,
HasImgLstBitmap: boolean; ACanvas: TCanvas; CheckedRect: TRect);
procedure DrawTheText(Sender: TObject; txt, ShortCuttext: string;
ACanvas: TCanvas; TextRect: TRect;
Selected, Enabled, Default, TopMenu, IsRightToLeft: boolean;
var TxtFont: TFont; TextFormat: integer);
procedure DrawIcon(Sender: TObject; ACanvas: TCanvas; B: TBitmap;
IconRect: Trect; Hot, Selected, Enabled, Checked, FTopMenu,
IsRightToLeft: boolean);
procedure MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
//function GetImageExtent(MenuItem: TMenuItem): TPoint;
function GetImageExtent(MenuItem: TMenuItem; FTopMenu: TMenu): TPoint; // +jt
function TopMenuFontColor(ACanvas: TCanvas; Color: TColor): TColor;
procedure DrawGradient(ACanvas: TCanvas; ARect: TRect;
IsRightToLeft: boolean);
procedure DrawWindowBorder(hWnd: HWND; IsRightToLeft: boolean);
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure InitComponent(Comp: TComponent); // Tom: Added for usage by the main program ."Thomas Knoblauch" <thomas@tom-the-bomb.de> 27.08
procedure ActivateMenuItem(MenuItem: TMenuItem; SubMenus: boolean); // +jt
property Form: TScrollingWinControl read FForm write SetForm;
// +jt
property IsWXP: boolean read FIsWXP;
property IsW2k: boolean read FIsW2k;
property IsWNT: boolean read FIsWNT;
// property TransparentColor: TColor read FTransparentColor write FTransparentColor;
// +jt
published
property DimLevel: Byte read FDimLevel write FDimLevel default 30;
property GrayLevel: Byte read FGrayLevel write FGrayLevel default 10;
property Font: TFont read FFont write SetFont;
property Color: TColor read FColor write SetColor default clBtnFace;
property DrawMenuBar: boolean read FDrawMenuBar write SetDrawMenuBar default False;
property IconBackColor: TColor read FIconBackColor write SetIconBackColor default clBtnFace;
property MenuBarColor: TColor read FMenuBarColor write SetMenuBarColor default clBtnFace;
property SelectColor: TColor read FSelectColor write SetSelectColor default clHighlight;
property SelectBorderColor: TColor read FSelectBorderColor
write SetSelectBorderColor default clHighlight;
property SelectFontColor: TColor read FSelectFontColor
write SetSelectFontColor default clMenuText;
property DisabledColor: TColor read FDisabledColor write SetDisabledColor default clInactiveCaption;
property SeparatorColor: TColor read FSeparatorColor
write SetSeparatorColor default clBtnFace;
property CheckedColor: TColor read FCheckedColor write SetCheckedColor default clHighlight;
property IconWidth: integer read FIconWidth write SetIconWidth default 24;
property DrawSelect: boolean read FDrawSelect write SetDrawSelect default True;
property UseSystemColors: boolean read FUseSystemColors
write SetUseSystemColors default True;
property UseDimColor: boolean read FUseDimColor write SetUseDimColor default False;
property OverrideOwnerDraw: boolean read FOverrideOwnerDraw
write SetOverrideOwnerDraw default False;
property Gradient: boolean read FGradient write SetGradient default False;
property FlatMenu: boolean read FFlatMenu write SetFlatMenu default False;
property AutoDetect: boolean read FAutoDetect write SetAutoDetect default False;
property XPContainers: TXPContainers read FXPContainers write SetXPContainers
default [xccForm, xccFrame, xccToolbar, xccCoolbar, xccControlbar, xccPanel,
xccScrollBox, xccGroupBox, xccTabSheet, xccPageScroller];
property XPControls :TXPControls read FXPControls write SetXPControls
default [xcMainMenu, xcPopupMenu, xcToolbar, xcControlbar, xcCombo, xcListBox,
xcEdit, xcMaskEdit, xcMemo, xcRichEdit, xcMiscEdit, xcCheckBox,
xcRadioButton, xcButton, xcBitBtn, xcSpeedButton, xcUpDown, xcPanel,
xcGroupBox, xcTreeView, xcListView, xcProgressBar, xcHotKey];
{xcStringGrid, xcDrawGrid, xcDBGrid];}
property Active: boolean read FActive write SetActive default False;
property DisableSubclassing: boolean read FDisableSubclassing write SetDisablesubclassing
default False;
end;
TXPMenuManager = class(TPersistent)
private
FXPMenuList: TList;
FPendingFormsList: TList;
FFormList: TList;
FActiveXPMenu: TXPMenu;
FDisableSubclassing: boolean;
function MainWindowHook(var Message: TMessage): boolean;
procedure CollectForms;
procedure RemoveChildSubclassing(AForm: TCustomForm);
procedure SetDisableSubclassing(AValue: boolean);
function FindSubclassingXPMenu(Exclude: TXPMenu): TXPMenu;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);
public
constructor Create;
destructor Destroy; override;
procedure Add(AXPMenu: TXPMenu);
procedure Delete(AXPMenu: TXPMenu);
procedure UpdateActiveXPMenu(AXPMenu: TXPMenu);
procedure AddForm(AForm: TCustomForm);
procedure RemoveForm(AForm: TCustomForm);
function IsFormSubclassed(AForm: TCustomForm): boolean;
function IsComponentSubclassed(AComponent: TComponent): boolean;
property ActiveXPMenu: TXPMenu read FActiveXPMenu;
property DisableSubclassing: boolean read FDisableSubclassing write SetDisableSubclassing
default False;
end;
function GetShadeColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;
function MergColor(Colors: Array of TColor): TColor;
function NewColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;
procedure DimBitmap(ABitmap: TBitmap; Value: integer);
procedure DrawArrow(ACanvas: TCanvas; X, Y: integer); overload;
procedure DrawArrow(ACanvas: TCanvas; X, Y, Orientation: integer); overload;
function GrayColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;
function GetInverseColor(AColor: TColor): TColor;
procedure GrayBitmap(ABitmap: TBitmap; Value: integer);
procedure DrawBitmapShadow(B: TBitmap; ACanvas: TCanvas; X, Y: integer;
ShadowColor: TColor);
procedure DrawCheckMark(ACanvas: TCanvas; X, Y: integer);
procedure GetSystemMenuFont(Font: TFont);
procedure Register;
const
WM_DRAWMENUBORDER = CN_NOTIFY + 101; // +jt
WM_DRAWMENUBORDER2 = CN_NOTIFY + 102; // +jt
// Gloabal access to the XPMenuManager
var
XPMenuManager: TXPMenuManager;
implementation
procedure Register;
begin
RegisterComponents('System', [TXPMenu]);
end;
// Set up the global variable that represents the XPMenuManager
procedure InitControls;
begin
if XPMenuManager = nil then
XPMenuManager := TXPMenuManager.Create;
end;
// Delete the global variable that represents the XPMenuManager
procedure DoneControls;
begin
if (XPMenuManager <> nil) then
begin
XPMenuManager.Free;
XPMenuManager := nil;
end;
end;
// Test if mouse cursor is in the given rect of the application's main form
function IsMouseInRect(TheForm: TScrollingWinControl; DestRect: TRect): boolean;
var
p: TPoint;
begin
if Assigned(TheForm) then
begin
p := Mouse.CursorPos;
p.x := p.x - TheForm.Left;
p.y := p.y - TheForm.Top;
Dec(DestRect.Right);
Dec(DestRect.Bottom, 2);
Result := (p.x >= DestRect.Left) and (p.x <= DestRect.Right) and
(p.y >= DestRect.Top) and (p.y <= DestRect.Bottom);
end
else Result := False;
end;
{ TXPMenue }
constructor TXPMenu.Create(AOwner: TComponent);
var
OSVersionInfo: TOSVersionInfo; // +jt
begin
inherited Create(AOwner);
FFont := TFont.Create;
FDisableSubclassing := false; // enable XPMenu to be used for global subclassing
{$IFDEF VER5U}
FFont.Assign(Screen.MenuFont);
{$ELSE}
GetSystemMenuFont(FFont);
{$ENDIF}
FForm := Owner as TScrollingWinControl;
FUseSystemColors := True;
FColor := clBtnFace;
FIconBackColor := clBtnFace;
FSelectColor := clHighlight;
FSelectBorderColor := clHighlight;
FMenuBarColor := clBtnFace;
FDisabledColor := clInactiveCaption;
FSeparatorColor := clBtnFace;
FCheckedColor := clHighlight;
FSelectFontColor := FFont.Color;
FGrayLevel := 10;
FDimLevel := 30;
FIconWidth := 24;
FDrawSelect := True;
XPContainers := [xccForm, xccFrame, xccToolbar, xccCoolbar, xccControlbar, xccPanel,
xccScrollBox, xccGroupBox, xccTabSheet, xccPageScroller];
XPControls := [xcMainMenu, xcPopupMenu, xcToolbar, xcControlbar, xcCombo, xcListBox,
xcEdit, xcMaskEdit, xcMemo, xcRichEdit, xcMiscEdit, xcCheckBox,
xcRadioButton, xcButton, xcBitBtn, xcSpeedButton, xcUpDown, xcPanel,
xcGroupBox, xcTreeView, xcListView, xcProgressBar, xcHotKey];
{xcStringGrid, xcDrawGrid, xcDBGrid];}
if Assigned(FForm) then
SetGlobalColor(TForm(FForm).Canvas);
// +jt
// FTransparentColor := clFuchsia;
FColorsChanged := false;
OSVersionInfo.dwOSVersionInfoSize := sizeof(OSVersionInfo);
GetVersionEx(OSVersionInfo);
FIsWXP:=false;
FIsW2k:=false;
FIsWNT:=false;
if OSVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
FIsWNT:=true;
if (OSVersionInfo.dwMajorVersion = 5) and (OSVersionInfo.dwMinorVersion = 0) then FIsW2k:=true;
if (OSVersionInfo.dwMajorVersion = 5) and (OSVersionInfo.dwMinorVersion = 1) then FIsWXP:=true;
end;
// +jt
if not(csDesigning in ComponentState) then
InitControls;
end;
destructor TXPMenu.Destroy;
begin
if Assigned(FForm) then //oleg oleg@vdv-s.ru Mon Oct 7
InitItems(FForm, false, false);
// Remove XPMenu from XPMenuManager
if Assigned(XPMenuManager) and not(csDesigning in ComponentState) then
begin
XPMenuManager.Delete(Self);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -