?
Solved

How to limit pop-up menu items?

Posted on 2003-11-24
4
Medium Priority
?
418 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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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