Popup menu border

Posted on 2004-12-01
Last Modified: 2010-04-05

Simply put, I want to be able to show popup menus with solid borders, with my choice of thickness and colour.


Question by:MoonCalf
    LVL 17

    Expert Comment

    by:Wim ten Brink
    AFAIK, the popup class doesn't have a customdraw method and doesn't even provide you any way to draw it yourself with simple events. I think it cannot be done, unless you want to write your own code for it. (E.g. by displaying a panel, drawing the menuoptions on this panel any way you like them and use OnClick events to respond to menu-options. But then this isn't a real popup anymore.)
    LVL 1

    Author Comment

    Erm, thanks alex, but I've already done a lot of drawing on the menus.  If you google you'll find lots of examples of custom-drawing on menus, both standard and popup.
    LVL 17

    Accepted Solution

    Ah, I see. I was looking at the wrong location... :-)
    Well, in that case you already know the answer... Use the CustomDraw event of the menu item... The simplest way to do this is by assigning one single OnDraw event to all the menu-items in your messagelist. The sender will be the menu item that wants to be drawn so use it to get the caption. If you want to draw a box around the whole popup menu then you have to look if the menu-item happens to be the first or last in the list. If so, you should draw the sides and either the top or bottom border. If it's not, draw only the sides.
    Set the Popup OwnerDraw to True and on e.g. the FormCreate, call this code:

      for I := 0 to pred( PopupMenu1.Items.Count ) do
        PopupMenu1.Items[ I ].OnAdvancedDrawItem := GlobalAdvancedDrawItem;

    And of course, add the GlobalAdvancedDrawItem function:

    procedure TFormMain.GlobalAdvancedDrawItem( Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState );
      MenuItem: TMenuItem;
      if Sender.InheritsFrom( TMenuItem ) then begin
        MenuItem := TMenuItem( Sender );
        if ( MenuItem.MenuIndex = 0 ) then begin
          DrawEdge( ACanvas.Handle, ARect, EDGE_ETCHED, BF_LEFT or BF_RIGHT or BF_TOP );
        else if ( MenuItem.MenuIndex = Pred( MenuItem.Parent.Count ) ) then begin
          DrawEdge( ACanvas.Handle, ARect, EDGE_ETCHED, BF_LEFT or BF_RIGHT or BF_BOTTOM );
        else begin
          DrawEdge( ACanvas.Handle, ARect, EDGE_ETCHED, BF_LEFT or BF_RIGHT );
        ARect.Left := ARect.Left + 2;
        ARect.Top := ARect.Top + 2;
        ARect.Right := ARect.Right - 2;
        ARect.Bottom := ARect.Bottom - 2;
        ACanvas.TextRect( ARect, ARect.Left, ARect.Top, MenuItem.Caption );

    Of course, you could use your own drawing technique. Also, above code doesn't check if items are hidden of if there's only one menu-item in the list. It needs more work... Use the canvas methods to draw whatever you like but also make sure to set the OnMeasureItem event if you want to make it a bit bigger. You have to make sure that whatever you draw will fit.
    LVL 1

    Author Comment

    Thanks alex - with a bit of jigging around I can get that working with what I've already done.

    Cheers mate,



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now