Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how do i get the caption of the currently highlighted menuitem in a popupmenu?

Posted on 2007-04-02
8
Medium Priority
?
323 Views
Last Modified: 2010-04-05
how do i get the caption of the currently highlighted menuitem in a popupmenu?

I have a popupmenu and in it is menuitems added dynamically, i want to know how to get the caption of the menuitem while on mouseover or on the menuitem currently highlighted.
i dont want to know of the caption when you click on the item, i want to know the caption when i am on mouse over on the item.

and put the caption in the form1.caption.
any clues?

Ben
0
Comment
Question by:Alpha_AI
[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
  • 5
  • 3
8 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1200 total points
ID: 18840310
I only know how to do it with Owner Draw

Set the PopupMenu's "OwnerDraw" to true
then on the first menuitem, set the 2 events "OnDrawItem" and "OnMeasureItem"


procedure TForm2.SomeMenuItem_MeasureItem(Sender: TObject; ACanvas: TCanvas;
  var Width, Height: Integer);
begin
  Width := 140;
end;

procedure TForm2.SomeMenuItem_1DrawItem(Sender: TObject; ACanvas: TCanvas;
  ARect: TRect; Selected: Boolean);
var
 ImgID: integer;
begin
 if Selected then
 begin
   ACanvas.Brush.Color := clHighlight;
   Form2.Caption := StringReplace(TMenuItem(sender).Caption, '&', '', [rfReplaceAll]); // or just use hint
 end
 else
 begin
   ACanvas.Brush.Color := clMenu;
   Form2.Caption := '';
 end;

 ARect.Left := 4;
 ACanvas.FillRect(ARect);
 DrawText(ACanvas.Handle, PChar( TMenuItem(sender).Caption), -1, ARect, DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOCLIP);
end;

Now... for all the menuitems, choose the same 2 events (so that all the menuitems are calling the same 2 events)

This is just a partial example I wrote for you. It does not draw the images, but you can actually customize it a lot. Take a look at this site for more info
http://delphi.about.com/od/vclusing/a/owner_drawing.htm
0
 

Author Comment

by:Alpha_AI
ID: 18842061
The code works but the highlighted bar is now gone.
Do you know how make it appear?

Ben
0
 

Author Comment

by:Alpha_AI
ID: 18842136
Do you know how to get the width of the popupmenu.

You see my menuitems are dynamic so the length of the caption in the menuitem determines the width of the popupmenu
I saw the function

procedure TForm2.SomeMenuItem_MeasureItem(Sender: TObject; ACanvas: TCanvas;
  var Width, Height: Integer);
begin
  Width := 140;
end;

and i realise that width contains the length of the last str in the menu item but if the length of that last item
is the shortest then i cant use width .

i need to know the longest menuitem caption and i guess there must be a way to just get the width.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18856500
I know how to get the width of teh text in teh menu item...
canvas.textwidth (it uses the canvas font to determine the dimensions)
eg.
width := ACanvas.TextWidth( TMenuItem(sender).Caption ); // + any buffer for the checkmark/image of course

height := ACanvas.TextHeight(TMenuItem(sender).Caption);
0
 

Author Comment

by:Alpha_AI
ID: 18858251
ok you dont know how to make the blue highlighter while you hover reappear do you?

Ben
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18920387
it does highlight on mine.
see this portion
if Selected then
 begin
   ACanvas.Brush.Color := clHighlight;
0
 

Author Comment

by:Alpha_AI
ID: 18922032
it doesnt on mine, what am i doing wrong?

procedure TForm1.SomeMenuItem_1DrawItem(Sender: TObject; ACanvas: TCanvas;
  ARect: TRect; Selected: Boolean);
var
  ImgID: integer;
  HoverText : string;
  position : integer;
  GetTheNumberString : string;
  GetTheNumber : integer;
  Node : PVirtualNode;
  Data : PNodeData;
begin
 if Selected then
 begin
   ACanvas.Brush.Color := clHighlight;
   Form1.Caption := StringReplace(TMenuItem(sender).Caption, '&', '', [rfReplaceAll]); // or just use hint
   hovertext := Form1.Caption;
   Position := Pos(')',hovertext);  // if copy of real version has a '(' in it then run this small function
   if Position = 3 then
   begin
    GettheNumberString := AnsiMidStr(hovertext,2,1); //Extract the number out cos we need it for duplicate custom names, this way each custom name is unique by a number.
    GetTheNumber := strtoint(GetTheNumberString);
    Delete(hovertext,1,4);
  end;

if Position = 4 then
  begin
  GettheNumberString := AnsiMidStr(hovertext,2,2); //Extract the number out cos we need it for duplicate custom names, this way each custom name is unique by a number.
  GetTheNumber := strtoint(GetTheNumberString);
  Delete(hovertext,1,5);
  end;
  Node := FindNodeByID1(vtClips,hovertext);
  if Node <> nil then
    begin
    Data := vtClips.GetNodeData(Node);
    Form7.Caption := Data^.DictionaryWord;
    Form7.PageControl1.Pages[0].Caption := Data^.DictionaryWord;
    Form7.RichEdit1.Text := Data^.Type1;

    end;
 end
 else
 begin
   ACanvas.Brush.Color := clMenu;
   Form1.Caption := '';
  ARect.Left := 4;
 ACanvas.FillRect(ARect);
 DrawText(ACanvas.Handle, PChar( TMenuItem(sender).Caption), -1, ARect, DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOCLIP);
end;
end;
0
 

Author Comment

by:Alpha_AI
ID: 18922078
yeah i removed the end; at the bottom and put it underneath Form1.Caption := ''; and then it worked :-)

THanks heaps

Ben
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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