|
I edited the Function for Menu-Drawing to this:
public static void DrawMenuItem (System.Windows.Forms.DrawItemEventArgs e, MenuItem mi)
{
// Drawing rect holen
Rectangle rect = new Rectangle (e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height -1);
// Check the state of the menuItem
if ((e.State & DrawItemState.HotLight) == DrawItemState.HotLight) {
// Draw Hover rectangle
DrawHoverRect (e, mi);
}
else if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
// Draw selection rectangle
DrawSelectionRect (e, mi);
}
else {
// if no selection, just draw space
e.Graphics.FillRectangle (new SolidBrush (Globals.MainColor), rect);
e.Graphics.DrawRectangle (new Pen (Globals.MainColor), rect);
}
// Create stringFormat object with Line Alignment to Center and Stringalignment to Left
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Near;
// Draw the text
SolidBrush textBrush = new SolidBrush (Globals.TextColor);
string miText = mi.Text.Replace ("&", "");
int pos = mi.Text.IndexOf ('&');
if (pos == -1) {
e.Graphics.DrawString (miText, Globals.menuFont, textBrush, rect, sf);
}
else {
RectangleF rectF = new RectangleF (rect.X, rect.Y, rect.Width, rect.Height);
StringFormat sfmt = StringFormat.GenericTypographic;
sfmt.Alignment = sf.Alignment;
// underline the letter which has '&' in Front
string startText = mi.Text.Substring (0, pos);
if (startText.Length > 0) {
e.Graphics.DrawString (startText, Globals.menuFont, textBrush, rectF, sf);
SizeF size = e.Graphics.MeasureString (startText, Globals.menuFont, new PointF (rectF.X, rectF.Y), sfmt); // , new SizeF (rect.Width, rect.Height), sf
rectF = new RectangleF (rectF.X + size.Width, rectF.Y, rectF.Width - size.Width, rectF.Height);
}
Font f = new Font (Globals.menuFont, FontStyle.Underline);
string medString = mi.Text.Substring (pos + 1, 1);
e.Graphics.DrawString (medString, f, textBrush, rectF, sf);
SizeF size2 = e.Graphics.MeasureString (medString, Globals.menuFont, new PointF (rectF.X, rectF.Y), sfmt); // , new SizeF (rect.Width, rect.Height), sf
rectF = new RectangleF (rectF.X + size2.Width, rectF.Y, rectF.Width - size2.Width, rectF.Height);
e.Graphics.DrawString (mi.Text.Substring (pos + 2), Globals.menuFont, textBrush, rectF, sf);
}
}
|
|
|
|
|
Hi
Thanks for fast response. Your logic only works in Main menu item, but not in the menuitem under the main menu. same problem as the fixed provided by Picham (original fixes). I came back to the fixes provided by Picham and debug the DrawMenuText(System.Windows.Forms.DrawItemEventArgs e, MenuItem mi) function and fixed the problem
Commented out this line:
//string miText = mi.Text.Replace("&","");
and replaced this line:
//e.Graphics.DrawString(miText, Globals.menuFont, textBrush, rect, sf);
with the following line
e.Graphics.DrawString(mi.Text, Globals.menuFont, textBrush, rect, sf);
Bye
|
|
|
|
|
Hi,
in your reply the replaced line is the same as the former line. I am interested in your fix, as it is obviously easier then mine.
I changed the function DrawMenuText () in MenuItemDrawing.cs in the same manner as DrawMenuText () in MainMenuItemDrawing.cs. That way I get the fix in both MenuHeaders as Menuitems. Sorry if I only posted the function once, but I felt it was obious to make the change in both codefiles.
Greetings
|
|
|
|
|
Hi,
Summary:
I changed the function DrawMenuItem(...) in MainMenuItemDrawing.cs to Thomas-H.'s code.
I put the function DrawHoverRect() into MenuItemDrawing.cs and I changed the following line
<br />
e.Graphics.DrawString(miText, Globals.menuFont, textBrush, rect, sf);<br />
to this (Thomas-H.'s) code:
<br />
int pos = mi.Text.IndexOf ('&'); <br />
if (pos == -1) <br />
{<br />
e.Graphics.DrawString (miText, Globals.menuFont, textBrush, rect, sf);<br />
}<br />
else <br />
{<br />
RectangleF rectF = new RectangleF (rect.X, rect.Y, rect.Width, rect.Height);<br />
StringFormat sfmt = StringFormat.GenericTypographic;<br />
sfmt.Alignment = sf.Alignment;<br />
<br />
string startText = mi.Text.Substring (0, pos);<br />
if (startText.Length > 0) <br />
{<br />
e.Graphics.DrawString (startText, Globals.menuFont, textBrush, rectF, sf);<br />
SizeF size = e.Graphics.MeasureString (startText, Globals.menuFont, new PointF (rectF.X, rectF.Y), sfmt);<br />
rectF = new RectangleF (rectF.X + size.Width, rectF.Y, rectF.Width - size.Width, rectF.Height);<br />
}<br />
Font f = new Font (Globals.menuFont, FontStyle.Underline);<br />
string medString = " " + mi.Text.Substring (pos + 1, 1);<br />
e.Graphics.DrawString (medString, f, textBrush, rectF, sf);<br />
SizeF size2 = e.Graphics.MeasureString (medString, Globals.menuFont, new PointF (rectF.X, rectF.Y), sfmt);<br />
rectF = new RectangleF (rectF.X + size2.Width, rectF.Y, rectF.Width - size2.Width, rectF.Height);<br />
e.Graphics.DrawString (mi.Text.Substring (pos + 2), Globals.menuFont, textBrush, rectF, sf);<br />
}<br />
Bye
|
|
|
|
|
Hi,
There is a much easier way which will give correct results with all fonts. String drawing is prone to pixel format errors. Add this to the StringFormat object used to draw the string and don't replace the & char with ""
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
Charles
|
|
|
|
|
Nice, thanks!
Best regards,
Balder
|
|
|
|
|
Hey,
using the example project, I added pichamp's solution and this worked. No problem at all.
I am using Win XP SP2 and .NET 2003
Of course you have to change the text-property of the menu-items to contain the &(like &File or &Open), but that should be obvious.
Cheers,
Danny
|
|
|
|
|
To add the hotkey when you prefix a leffer by '&'
-------------------------------------------------
Search in the MenuItemDrawing.cs and MainMenuItemDrawing.cs files this line of code:
StringFormat sf = new StringFormat();
Be sure that the following list exists after this declaration (add it otherwise)
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
You need to make a last modification:
In the DrawMenuText method of the MenuItemDrawing.cs class you need to change this line:
string miText = mi.Text.Replace("&","");
into
string miText = mi.Text;
Now hot letters will appear correctly.
Thanks for the original tip to Gavray F.
Andy
|
|
|
|
|
But the good thing is much trueer!
|
|
|
|
|
C:\Documents and Settings\Stephen\My Documents\Visual Studio Projects\HTML workshop\Form1.cs(496): 'HTML_Workshop.Form1' denotes a 'class' where a 'variable' was expected
I have this under the load event of "Form1".
this.officeMenus1.Start( Form1 );
why won't this work? (the error is above)
|
|
|
|
|
nevermind, I also figured this one out.
I was wandering though. where did you find this componenet? is it possible you could look into doing the same with the office toolbars? or is there a way already to do this? also, is it legal to use the icons that come with office 2003? how would I get the icons? any ideas to any of this would be helpful.
|
|
|
|
|
I have office 2003 installed, but I don't know how you find this object. any help for a newbie, thanks.
|
|
|
|
|
nevermind, I found it in your demo project.
|
|
|
|
|
Check in the bin\Release folder...
Sreenivasulu Palavarapu
Lera Technologies,
Secunderabad.
|
|
|
|
|
This is very nice and useful piece of code but it has one bug. The bug is small but nevertheless serious. There’s no support for nested controls. For example if inside a Panel you have a ListBox with a ContextMenu this menu will not be owner drawn, i.e. Office 2003-like.
The bug is in the component’s Start() function. When all controls are scanned for their context menus there is no recursion. To put it plainly: only first level controls are scanned, not controls inside controls inside controls, etc. To avoid this just add little recursion function and even the overall code will be shorter.
public void Start(Form form)
{
...
//**************************************
// Context menus
//**************************************
// Parent Form context menu
ContextMenu cmenu = form.ContextMenu;
if ( cmenu != null ) {
InitMenuItem(cmenu);
}
// Now all context menus for all controls in the Form (recursive)
InitMenuItemRecursive( form.Controls );
}
catch {}
}
private void InitMenuItemRecursive(Control.ControlCollection cc)
{
foreach ( Control c in cc )
{
if ( c.Controls != null )
InitMenuItemRecursive( c.Controls );
if ( c.ContextMenu != null )
InitMenuItem( c.ContextMenu );
}
}
You can download the debugged component and test application from:
http://h-sw.com/free_code/Office2003Menus.zip
Sincerely,
Marko
|
|
|
|
|
You saved my life. Thank you very much. Nice spotted!!!
Thank you again.
|
|
|
|
|
Hey Guyz...
Am trying to use the modified code as shown by Marko(horvatma) but it still doesn't work properly please would u mind to elaborate how it works. Am usin the control on a VB.Net app all the main menus are aiight but when it comes to context menu in a control doesn't work..
Am i missing something.. I even downloaded the modified one u gave en its still not workin.. Help!!
desparate
Rastaman
Fire for all those that mislead Jah Children...Rastafariiii!!
|
|
|
|
|
i can not download office2003menus from your link. Could you send me a right link?
Thanks
Nemo
|
|
|
|
|
Sorry!My English is bad!
When I use notifyIcon control, Can't find Menu,Why?
|
|
|
|
|
Check the source code of method "start" in the file OfficeMenu.cs, you will get the answer.
|
|
|
|
|
Hi there
i'm not sure, i hope i'm only to stupid to make it.
I had downloaded your source code file. In this File there is a Demo application. I run it and everything was ok. Then i remove the MainMenu from the form an run again and no Office Style appears for contextMenu. I anulled the last step an everything is alright.
Is this a bug? Please clear me up!
thx
|
|
|
|
|
شكرا على السيطرة
هو مفيد
Thanks for your control; it's very usefull
|
|
|
|
|
Greate work! but there is bug. The seperators visible in original menu are not visible here. Please help me.
|
|
|
|
|
Hi. After searching I could find the bug for the separators not being visible.
The bug in is the "DrawMenuText" method in the MenuItemDrawig class:
Very simple:
I changed this line:
e.Graphics.DrawLine(new Pen(Globals.MenuLightColor), e.Bounds.X + Globals.PIC_AREA_SIZE + 3, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Y + 2);
to this:
e.Graphics.DrawLine(new Pen(Color.Silver), e.Bounds.X + Globals.PIC_AREA_SIZE + 3, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Y + 2);
Simple as you can see. It seems like the creator of the component, unwillingly, was putting the menu and separator colors the same.
Hope it helps.
Alexander German
|
|
|
|
|
Hi,
the control is great - but please add MDI support (menu-merging)!
regards, Tilli
|
|
|
|
|