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
Solved

How to limit pop-up menu items?

Posted on 2003-11-24
4
412 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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