delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (8)


Give your menus a (customisable) new look with this component.Go to Vassilis Perantzakis's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Looks a lot like the XP menus. Only Better.
Product:
Delphi 5.x (or higher)
Category:
GUI
Skill Level:
Scoring:
Last Update:
11/02/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet Delphi-5,VCL,Custom-Component,Windows-95,Windows-98,Windows-2000,Windows-XP,Windows-NT,Menus
Times Scored:
11
Visits:
5985
Uploader: Vassilis Perantzakis
Company: B-logica soft
Reference: www.devconone.com
 
Question/Problem/Abstract:
Bored with the standard Delphi menus? try the XPMenu addition. Just place the component allongside your menus, enable Ownerdraw property on every menu you wish to take the look you want, and call the XPMenu Execute method.
Answer:



unit XpMenu;

interface

uses
  Windows, Classes, Graphics, Menus;

type
  TXpMenu = class(TComponent)
  private
    { Private declarations }
   FSelColor:TColor;
   FStripColor:TColor;
   FBackColor:TColor;
   FStripWidth:integer;
   FSelFontColor:TColor;
   FNotSelFontColor:TColor;
   FNotActiveColor:TColor;
  protected
    { Protected declarations }
  public
    { Public declarations }
    Procedure Execute;
    Procedure RemoveXPs;
    Constructor Create(AOwner:TComponent);override;
  published
    { Published declarations }
    procedure DefMenuDrawItem(Sender: TObject; ACanvas: TCanvas;
      ARect: TRect; Selected: Boolean);
    procedure DefMenuMeasureItem(Sender: TObject; ACanvas: TCanvas;
      var Width, Height: Integer);
    procedure DefMenuAdvancedDrawItem(Sender: TObject; ACanvas: TCanvas;
      ARect: TRect; State: TOwnerDrawState);
    property SelectionColor:TColor read FSelColor write FSelColor;
    property SelectionFontColor: TColor read FSelFontColor write FSelFontColor;
    property NotSelectedFontColor: TColor read FNotSelFontColor write FNotSelFontColor;
    property NotActiveColor: TColor read FNotActiveColor write FNotActiveColor;
    property StripColor:TColor read FStripColor write FStripColor;
    property BackColor:TColor read FBackColor write FBackColor;
    property StripWidth:integer read FStripWidth write FStripWidth;

  end;

procedure Register;

implementation

constructor TXpMenu.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  FSelFontColor:=clWhite;
  FSelColor:=$00FEC0D0;
  FStripColor:=clGray;
  FBackColor:=clWhite;
  FNotSelFontColor:=clBlack;
  FNotActiveColor:=clGray;  
  FStripWidth:=20;
end;


procedure TXpMenu.DefMenuDrawItem(Sender: TObject; ACanvas: TCanvas;
  ARect: TRect; Selected: Boolean);
const
  cHotkeyPrefix='&';
  Alignments: array[TPopupAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
  myBaseR:TRect;
  myrect:TRect;
  myBRect:TRect;
  mySrect:TRect;
  vCaption:String;
  vColumn:integer;
  ParentMenu:TMenu;
  Flags:integer;
  NotEnabled:boolean;
  Alignment: TPopupAlignment;
  BitMp:TBitmap;
begin
  BitMp:=TBitmap.Create;
  ParentMenu := TMenuItem(Sender).GetParentMenu;
  NotEnabled:= TMenuItem(Sender).Enabled = false;
  //outputdebugstring(pchar(string(ParentMenu.ClassName)));
  if ParentMenu is TMenu then
    Alignment := paLeft
  else if ParentMenu is TPopupMenu then
    Alignment := TPopupMenu(ParentMenu).Alignment
  else
    Alignment := paLeft;
  myBaseR.Left:=ARect.Left;
  myBaseR.Right:=myBaseR.Left+FStripWidth;
  myBaseR.Top:=ARect.Top;
  myBaseR.Bottom:=ARect.Bottom;
  Myrect.Left:=ARect.Left+FStripWidth;
  Myrect.Right:=ARect.Right;
  Myrect.Top:=ARect.Top;
  Myrect.Bottom:=ARect.Bottom;
  mySrect.Left:=ARect.Left;
  mySrect.Right:=ARect.Right;
  mySrect.Top:=ARect.Top;
  mySrect.Bottom:=ARect.Bottom;
  if not ((Sender as TMenuItem).Parent.Name='') then
   begin
      ACanvas.Brush.Color:=FStripColor;
      ACanvas.FillRect(myBaseR);
      if not ((Sender as TMenuItem).Caption='-') then
        begin
          if Selected then
            begin
              ACanvas.Brush.Color:=FSelColor;//clSilver;
              ACanvas.FillRect(Myrect);
              ACanvas.Rectangle(mySrect);
              if (Sender as TMenuItem).Bitmap<>nil then
                begin
                  MyBrect.Left:=ARect.Left+2;
                  MyBrect.Right:=MyBrect.Left+(Sender as TMenuItem).Bitmap.Width;
                  MyBrect.Top:=ARect.Top+2;
                  MyBrect.Bottom:=ARect.Top+(Sender as TMenuItem).Bitmap.Height;
                  ACanvas.Brush.Color:=FStripColor;
                  ACanvas.FillRect(MyBrect);
                  //If (Sender as TMenuItem).ImageIndex>-1 then
                  //  begin
                  //    (ParentMenu as TMainMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,(Sender as TMenuItem).Bitmap);
                  //  end;

                  If (Sender as TMenuItem).ImageIndex>-1 then
                    begin
                      if (ParentMenu is TMainMenu) then
                        begin
                          if not ((ParentMenu as TMainMenu).Images=nil) then
                            (ParentMenu as TMainMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,BitMp);
                        end
                      else
                        begin
                          if not ((ParentMenu as TPopupMenu).Images=nil) then
                            (ParentMenu as TPopupMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,BitMp);
                        end;
                      ACanvas.Draw(mySrect.left+2,arect.top+2,BitMp);
                    end
                  else
                    ACanvas.Draw(mySrect.left,mySrect.top,(Sender as TMenuItem).Bitmap);
                  ACanvas.Brush.Color:=FSelColor;//clSilver;
                end;
            end
          else
            begin
              ACanvas.Brush.Color:=FBackColor;
              ACanvas.FillRect(Myrect);
              If (Sender as TMenuItem).ImageIndex>-1 then
                begin
                  if (ParentMenu is TMainMenu) then
                    begin
                      if not ((ParentMenu as TMainMenu).Images=nil) then
                        (ParentMenu as TMainMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,BitMp);
//                        (ParentMenu as TMainMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,(Sender as TMenuItem).Bitmap);
                    end
                  else
                    begin
                      if not ((ParentMenu as TPopupMenu).Images=nil) then
                        (ParentMenu as TPopupMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,BitMp);
//                        (ParentMenu as TPopupMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,(Sender as TMenuItem).Bitmap);
                    end;
                  ACanvas.Draw(mySrect.left+2,arect.top+2,BitMp);
                end
              else
                ACanvas.Draw(mySrect.left+2,arect.top+2,(Sender as TMenuItem).Bitmap);
            end;
          Myrect.left:=Myrect.left+4;
          myrect.top:=myrect.top+1;
          flags:=DT_EXPANDTABS {or DT_SINGLELINE or DT_CALCRECT }or DT_NOCLIP or Alignments[Alignment];
          if Selected then
            begin
              if NotEnabled then
                ACanvas.Font.Color:=FNotActiveColor
              else
                ACanvas.Font.Color:=FSelFontColor;
              DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),Myrect,Flags);
            end
          else
            begin
              if NotEnabled then
                ACanvas.Font.Color:=FNotActiveColor
              else
                ACanvas.Font.Color:=FNotSelFontColor;
              DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),Myrect,Flags);
            end;
          if not (TMenuItem(Sender).GetParentComponent is TMainMenu)  then
            begin
          //outputDebugstring(pchar(      ));
              Myrect.left:=MyRect.right-ACanvas.TextWidth(shortcuttotext((Sender as TMenuItem).shortcut))-1;
              if Selected then
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FSelFontColor;
                  DrawText(ACanvas.Handle,pchar(shortcuttotext((Sender as TMenuItem).shortcut)),length(shortcuttotext((Sender as TMenuItem).shortcut)),Myrect,Flags);
                end
              else
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FNotSelFontColor;
                  DrawText(ACanvas.Handle,pchar(shortcuttotext((Sender as TMenuItem).shortcut)),length(shortcuttotext((Sender as TMenuItem).shortcut)),Myrect,Flags);
                end;
            end;
          //ACanvas.TextOut(Myrect.Left+4,arect.top+1,(Sender as TMenuItem).Caption);//+shortcuttotext((Sender as TMenuItem).shortcut));
        end
      else
        begin
          ACanvas.Brush.Color:=FBackColor;
          ACanvas.FillRect(Myrect);
          myrect.top:=myrect.top+1;
          myrect.bottom:=myrect.top+1;
          myrect.Left:=myrect.Left+12;
          ACanvas.Brush.Color:=FStripColor;
          ACanvas.FillRect(Myrect);
        end;
    end;
  BitMp.free;    
end;

procedure TXpMenu.DefMenuMeasureItem(Sender: TObject; ACanvas: TCanvas;
  var Width, Height: Integer);
begin
  width:=width+FStripWidth;
  if (not (shortcuttotext((Sender as TMenuItem).ShortCut)='')) or (TMenuItem(Sender).GetParentComponent is TPopupMenu) then
    width:=width+ACanvas.TextWidth(shortcuttotext((Sender as TMenuItem).shortcut));
end;

procedure TXpMenu.DefMenuAdvancedDrawItem(Sender: TObject; ACanvas: TCanvas;
  ARect: TRect; State: TOwnerDrawState);
const
  cHotkeyPrefix='&';
  Alignments: array[TPopupAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
  myBaseR:TRect;
  myrect:TRect;
  myBRect:TRect;
  mySrect:TRect;
  selected:boolean;
  NotEnabled:boolean;
  pl:Array [0..3] of tpoint;
  ParentMenu:TMenu;
  Flags:integer;
  Alignment: TPopupAlignment;
begin
  ParentMenu := TMenuItem(Sender).GetParentMenu;
  if ParentMenu is TMenu then
    Alignment := paLeft
  else if ParentMenu is TPopupMenu then
    Alignment := TPopupMenu(ParentMenu).Alignment
  else
    Alignment := paLeft;
  Selected := odSelected in State;
  NotEnabled:= odDisabled in State;
  myBaseR.Left:=ARect.Left;
  myBaseR.Right:=myBaseR.Left+FStripWidth;
  myBaseR.Top:=ARect.Top;
  myBaseR.Bottom:=ARect.Bottom;
  Myrect.Left:=ARect.Left+FStripWidth;
  Myrect.Right:=ARect.Right;
  Myrect.Top:=ARect.Top;
  Myrect.Bottom:=ARect.Bottom;
  mySrect.Left:=ARect.Left;
  mySrect.Right:=ARect.Right;
  mySrect.Top:=ARect.Top;
  mySrect.Bottom:=ARect.Bottom;
  pl[0].x:=mySRect.Left;
  pl[0].y:=mySRect.Bottom;
  pl[1]:=mySRect.TopLeft;
  pl[2].x:=mySRect.Right;
  pl[2].y:=mySRect.Top;
  pl[3]:=mySRect.BottomRight;
  ACanvas.Brush.Color:=FStripColor;

  if (TMenuItem(Sender).GetParentComponent is TPopupMenu)  then
     ACanvas.FillRect(myBaseR);

  if not ((Sender as TMenuItem).Caption='-') then
    begin
      if ((Sender as TMenuItem).Parent.Name='') then
        begin
          if Selected then
            begin
              if (TMenuItem(Sender).GetParentComponent is TPopupMenu)  then
                begin
                  ACanvas.Brush.Color:=FSelColor;//clSilver;
                  ACanvas.FillRect(mySrect);
                  ACanvas.Rectangle(mySrect);
                  if (Sender as TMenuItem).Bitmap<>nil then
                    begin
                      MyBrect.Left:=ARect.Left+2;
                      MyBrect.Right:=MyBrect.Left+(Sender as TMenuItem).Bitmap.Width;
                      MyBrect.Top:=ARect.Top+2;
                      MyBrect.Bottom:=ARect.Top+(Sender as TMenuItem).Bitmap.Height;
                      ACanvas.Brush.Color:=FStripColor;
                      ACanvas.FillRect(MyBrect);
                      ACanvas.Draw(mySrect.left,mySrect.top,(Sender as TMenuItem).Bitmap);
                      ACanvas.Brush.Color:=FSelColor;//clSilver;
                    end;
                end
              else
                begin
                  ACanvas.Brush.Color:=FBackColor;
                  ACanvas.FillRect(mySrect);
                  ACanvas.Rectangle(mySrect);
                end;

            end
          else
            begin
              if (TMenuItem(Sender).GetParentComponent is TPopupMenu)  then
                begin
                  ACanvas.Brush.Color:=FBackColor;
                  ACanvas.FillRect(Myrect);
                  If (Sender as TMenuItem).ImageIndex>-1 then
                    begin
                      if not ((ParentMenu as TPopupMenu).Images=nil) then
                        (ParentMenu as TPopupMenu).Images.GetBitmap((Sender as TMenuItem).ImageIndex,(Sender as TMenuItem).Bitmap);
                    end;
                  ACanvas.Draw(mySrect.left+2,arect.top+2,(Sender as TMenuItem).Bitmap);
                end
              else
                begin
                  ACanvas.Brush.Color:=clBtnFace;
                  ACanvas.FillRect(mySrect);
                  if odHotLight  in State then
                    begin
                      ACanvas.Brush.Color:=FSelColor;//clSilver;
                      ACanvas.FillRect(mySrect);
                      ACanvas.Rectangle(mySrect);
                    end;
                end;
            end;
          mySrect.left:=mySrect.left+4;
          mySrect.top:=mySrect.top+1;
          flags:=DT_EXPANDTABS {or DT_SINGLELINE or DT_CALCRECT }or DT_NOCLIP or Alignments[Alignment];
          if not (TMenuItem(Sender).GetParentComponent is TPopupMenu)  then
            begin
              if Selected then
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FSelFontColor;
                  DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),mySrect,Flags)
                end
              else
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FNotSelFontColor;
                  DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),mySrect,Flags)
                end;
            end
          else
            begin
              myrect.left:=myrect.left+4;
              myrect.top:=myrect.top+1;
              if Selected then
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FSelFontColor;
                  DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),myrect,Flags);
                end
              else
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FNotSelFontColor;
                  DrawText(ACanvas.Handle,pchar((Sender as TMenuItem).Caption),length((Sender as TMenuItem).Caption),myrect,Flags);
                end;
              Myrect.left:=MyRect.right-ACanvas.TextWidth(shortcuttotext((Sender as TMenuItem).shortcut))-1;
              if Selected then
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FSelFontColor;
                  DrawText(ACanvas.Handle,pchar(shortcuttotext((Sender as TMenuItem).shortcut)),length(shortcuttotext((Sender as TMenuItem).shortcut)),Myrect,Flags);
                end
              else
                begin
                  if NotEnabled then
                    ACanvas.Font.Color:=FNotActiveColor
                  else
                    ACanvas.Font.Color:=FNotSelFontColor;
                  DrawText(ACanvas.Handle,pchar(shortcuttotext((Sender as TMenuItem).shortcut)),length(shortcuttotext((Sender as TMenuItem).shortcut)),Myrect,Flags);
                end;
            end;
          //ACanvas.TextOut(mySrect.Left+4,mySrect.top+1,(Sender as TMenuItem).Caption);
        end;
      end
    else
      begin
        ACanvas.Brush.Color:=FBackColor;
        ACanvas.FillRect(Myrect);
        myrect.top:=myrect.top+1;
        myrect.bottom:=myrect.top+1;
        myrect.Left:=myrect.Left+12;
        ACanvas.Brush.Color:=FStripColor;
        ACanvas.FillRect(Myrect);
      end;
end;

Procedure TXpMenu.RemoveXPs;
var
  i:Integer;
begin
  with owner do
    begin
      for i:=0 to ComponentCount-1 do
        begin
          if Components[i] is TMenuItem then
            begin
               (Components[i] as TMenuItem).OnDrawItem:=nil;
               (Components[i] as TMenuItem).OnMeasureItem:=nil;
               (Components[i] as TMenuItem).OnAdvancedDrawItem:=nil;
            end;
        end;
    end;
end;

Procedure TXpMenu.Execute;
var
  i:Integer;
begin
  with owner do
    begin
      for i:=0 to ComponentCount-1 do
        begin
          if Components[i] is TMenuItem then
            begin
               (Components[i] as TMenuItem).OnDrawItem:=Self.DefMenuDrawItem;
               (Components[i] as TMenuItem).OnMeasureItem:=Self.DefMenuMeasureItem;
               (Components[i] as TMenuItem).OnAdvancedDrawItem:=Self.DefMenuAdvancedDrawItem;
            end;
        end;
    end;
end;

procedure Register;
begin
  RegisterComponents('VNPVcls', [TXpMenu]);
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Checked property?
    Ruud van Steenis (Mar 9 2010 3:52PM)

Is doesn't correctly handle the 'checked' property?
Respond

XPMenu
    Steve Gossett (Jul 14 2004 1:10AM)

Can you give a quick example of calling the WinXP Execute method after dropping the component on a form?
Respond

RE: XPMenu
Vassilis Perantzakis (Jul 14 2004 10:21AM)

procedure TForm1.FormCreate(Sender:TObject);
begin
  XpMenu1.Execute;
{now all the menus get the new look. If a menu is changed dynamicaly then just call the execute method again where you change it.}
end;
Respond

RE: RE: XPMenu
SteveG (Jul 15 2004 4:49PM)

I see my mistake thanks alot. It is a great component!
Respond

GUI Addition
    Adam Lanzafame (Nov 6 2001 3:26AM)

what a fantastic addition to the GUI of any delphi app.
The component is very well done..
Respond

RE: GUI Addition
Amir Nabi (Nov 6 2002 11:47AM)

asaa asda as sa f ewr woi iweu iqw qiw irgvfhfrhvmx kkdkj
dlkf  kdfj fkdf dfkjf
Respond

RE: RE: GUI Addition
Anniel Santos (Nov 15 2005 5:01AM)

I wonder if i can put a bitmap like windows put in his menu.
Respond

RE: RE: RE: GUI Addition
Vassilis Perantzakis (Nov 15 2005 9:22AM)

Yes, I am working on a new version that will include images in the side bar
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
I. Siticov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)