Solved

How to limit pop-up menu items?

Posted on 2003-11-24
4
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 9813829
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
ID: 9839584
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
ID: 9858607
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
ID: 9862186
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

726 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