Solved

How to limit pop-up menu items?

Posted on 2003-11-24
4
408 Views
Last Modified: 2010-04-05
I like to know how to setup the number of lines of pop-up menu items so it will not go out of the screen size.

Thanks.

Jammy
0
Comment
Question by:jlislo
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
Comment Utility
Jammy,

This one is a little tricky, due to the fact that each menu item has an OnMeasureItem that could be coded to modifiy the menu item's default height/width. (thus making it almost impossible to calculate without hooking into the menu unit source code).

If this is not an issue, you call SystemParametersInfo to determine the default menu height that Delphi will use for the menu items.

var  ncMetrics:     TNonClientMetrics;
begin

  // Determine the default height for a menu item
  ncMetrics.cbSize:=Sizeof(TNonClientMetrics);
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, @ncMetrics, 0);
 
  // ncMetrics.iMenuHeight will be set with the default menu item height.

end;

You will also need to check if the menu item has a "-" for the caption, in which case Delphi will default to a 5 pixel height for the item.

So basically, you would walk your list of menu items, totalling the height and compare it against the Screen.Height.

An example which will wrap the menu items into new columns, where each column will fit within the height of the screen. (makes use of/modifes the Break property of the menu item)

procedure WrapPopupMenu(PopupMenu: TPopupMenu);
var  ncMetrics:     TNonClientMetrics;
     dwSysHeight:   Integer;
     dwItemHeight:  Integer;
     dwHeight:      Integer;
     dwCount:       Integer;
begin

  // Determine the default height for a menu item
  ncMetrics.cbSize:=Sizeof(TNonClientMetrics);
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, @ncMetrics, 0);
  dwSysHeight:=ncMetrics.iMenuHeight;

  // Iterate the menu items
  dwHeight:=0;
  for dwCount:=0 to Pred(PopupMenu.Items.Count) do
  begin
     // Calculate the estimated height of the menu item
     with PopupMenu.Items[dwCount] do
     begin
        // Clear any current break setting
        Break:=mbNone;
        if (Caption = '-') then
           dwItemHeight:=5
        else
           dwItemHeight:=dwSysHeight;
        // Check against the screen height
        if ((dwHeight+dwItemHeight) > Screen.Height) then
        begin
           // Break on this item
           dwHeight:=0;
           Break:=mbBarBreak;
        end;
        // Increment the calculated height
        Inc(dwHeight, dwItemHeight);
     end;
  end;

end;

Hope this helps,
Russell


0
 

Author Comment

by:jlislo
Comment Utility
Russell,

This is so great. I will add 100 points more for you if you can show me how to nerrow the width of the column. If not I will accept this answer. Thank you very much

Jammy
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
Jammy,
Sorry, missed the last posting by you (thanks for the points btw). To answer your second question, if you want to narrow/expand the menu width, you will need to do 2 things.

1. Make sure the menu/popupmenu has OwnerDraw set to true.
2. You have to handle the OnMeasureItem for the menu items, and change the Width Setting there.  The easiest way to do this is to create one method and assign all the menu items OnMeasureItem to it.

Note: You don't have to handle the painting at all, as long as you dont assign a method to OnDrawItem.

Hope this helps,
Russell



0
 

Author Comment

by:jlislo
Comment Utility
Russell,

Go to my next question "For Russell on OnMeasureItem answer". 100 points is waitting for you. Thanks you very much.

Jammy
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

11 Experts available now in Live!

Get 1:1 Help Now