Solved

How to limit pop-up menu items?

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DBGrid or StringGrid ? 6 120
Tidtcpserver listening on multiports? 1 46
DBCtrlGrid, Delphi, Scroll 8 60
Firemonkey Listview item popup menu ? 1 38
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

730 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