Board index » cppbuilder » TMenu and RadioItem display

TMenu and RadioItem display


2005-01-25 05:19:00 PM
cppbuilder75
can anyone tell me how to get around this prblem:
Borland C++ 6
I have a mainMenu. On a toplevel menu are 3 sub menus. For each of the 3
submenus I have set AutoCheck=true, RadioItem = true and GroupIndex=1. When
I run the application the 3 submenus run as radiobutton, as I expect, with
the selected menuItem showing a black cicle to the left of the item.
The problem arises when I link an ImageList to the mainMenu (Iwant some of
my menu items to show images.) When I run my application the radioItem menus
work as before but the black circle is bounded by a lightly shaded square.
It looks like a placeholder for an image. No image has been specified for
these menuItems.
Is there an easy way around this.
Many thanks,
Neville.
 
 

Re:TMenu and RadioItem display

Hi Neville,
Unfortunately, the code that draws the shaded square is occurs deep
within a subprocedure of the TMenuItem class and is not easily bypassed
without drawing the entire menu item yourself. A somewhat easier
workaround is to set the AutoCheck property of the TMenuItems to false,
and then use your own custom "checked" image which you'd add to the
menu's image-list.
For example, here's a function which will add the default "black circle"
image to the end of an image-list...
#include <memory>
void AddCheckImage(
TCustomImageList& ImageList
)
{
std::auto_ptr<Graphics::TBitmap>Bitmap(new Graphics::TBitmap());
Bitmap->Width = ImageList.Width;
Bitmap->Height = ImageList.Height;
HDC const hDC = Bitmap->Canvas->Handle;
RECT RCheck = {0, 0, ImageList.Width, ImageList.Height};
DrawFrameControl(hDC, &RCheck, DFC_MENU, DFCS_MENUBULLET);
// add it to the image list
ImageList.Add(Bitmap.get(), NULL);
}
You could call this function, e.g., in your Form's constructor, like so...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
AddCheckImage(*MainMenu1->Images);
}
Then, within the OnClick event handler for your grouped menu items,
you'd toggle the "checked" state by changing the TMenuItem::ImageIndex
property (rather than using the Checked property)...
#include <cassert>
// shared OnClick event handler
void __fastcall TForm1::MenuItemsClick(TObject *Sender)
{
assert(dynamic_cast<TMenuItem*>(Sender) != NULL);
TMenuItem& MenuItem = static_cast<TMenuItem&>(*Sender);
ToggleState(MenuItem, MainMenu1->Images->Count - 1);
// insert your other code here...
// NOTE: to determine if the item is checked,
// you'd query its ImageIndex property
}
Here's the definition of the ToggleState() function...
void ToggleState(
TMenuItem& Item,
int check_img_index
)
{
// check or uncheck the current item
Item.ImageIndex = (Item.ImageIndex == -1) ? check_img_index : -1;
// uncheck the other items
if ((Item.ImageIndex == check_img_index) && (Item.Parent != NULL))
{
int const max_index = Item.Parent->Count;
for (int index = 0; index < max_index; ++index)
{
TMenuItem& CurrentItem = *Item.Parent->Items[index];
if ((&CurrentItem != &Item) &&
(CurrentItem.GroupIndex == Item.GroupIndex))
{
CurrentItem.ImageIndex = -1;
}
}
}
}
HTH,
--
Damon (TeamB)
C++Builder Developer's Journal
bcbjournal.org
N Bentley wrote:
Quote
I have a mainMenu. [...]

The problem arises when I link an ImageList to the mainMenu (Iwant some of
my menu items to show images.) When I run my application the radioItem menus
work as before but the black circle is bounded by a lightly shaded square.
 

Re:TMenu and RadioItem display

Many thanks, I''l give that a try.
Neville.
 

{smallsort}