This question already has answers here:
How to change menu hover color
(4 answers)
Closed 9 years ago.
I add a MenuStrip in my app and is add on ManagerRenderMode at Render Mode. The problem is with the appearance, look offal. Look at those two photos, I want to change that white border of submenus in transparent, that blue rectangule that look offal on gray for the menu and for the submenu in dark gray (and his border that is a dark blue) and the border white of menu when is selected. How I can do this ?
BackColor is: 36; 36; 36 and ForeColor is LightGray.
I managed to change the blue rectangle, the white rectangle when the option is selected, the blue rectangle when I select an option of submenus, but I don't know how to change the white border, please help..
Here is the code so far...
Color culoare = Color.FromArgb(20, 20, 20);
Color culoare1 = Color.FromArgb(36, 36, 36);
public override Color MenuItemSelected
{
get { return culoare; }
}
public override Color MenuItemBorder
{
get { return culoare; }
}
public override Color MenuItemSelectedGradientBegin
{
get { return culoare; }
}
public override Color MenuItemSelectedGradientEnd
{
get { return culoare; }
}
public override Color MenuItemPressedGradientBegin
{
get { return culoare; }
}
public override Color MenuItemPressedGradientEnd
{
get { return culoare; }
}
public override Color MenuBorder
{
get { return culoare; }
}
You can do this by creating your own ColorTable, and overriding the properties you wish to change the colour of:
public class TestColorTable : ProfessionalColorTable
{
public override Color MenuItemSelected
{
get { return Color.Red; }
}
public override Color MenuBorder //added for changing the menu border
{
get { return Color.Green; }
}
}
You would use it like this:
private void Form1_Load(object sender, EventArgs e)
{
menuStrip1.Renderer = new ToolStripProfessionalRenderer(new TestColorTable());
}
Your approach is incorrect. You do not style menus and toolstrips using forecolor/backcolor.
Take a look at ToolStripProfessionalRenderer
Example on how to use this
public class MyToolStripRenderer : ToolStripProfessionalRenderer
{
/* override styling/drawing here */
}
MenuStrip strip = new MenuStrip();
strip.Renderer = new MyToolStripRenderer();
//this will set RenderMode to "Custom"
consider using this example on CodeProject as some research.
Better still, VBForums have loads of them, already implemented (in the usual Luna, Office, Windows, Visual Studio styles!)
http://www.vbforums.com/showthread.php?596563-100-Customizable-MenuStrip-ToolStrip-StatusStrip-including-common-presets
If you simply want to chaneg the colors...use Pondidum's answer! It involves less work!
Related
I have been working on an Iron Man hud. I am using WebEye to access the web cam... now i have to add label over the web cam control but the label is not transparent
I have tried every control but cant use the transparency function..
Here's my code
foreach (WebCameraId camera in webCameraControl1.GetVideoCaptureDevices())
{
comboBox1.Items.Add(new ComboBoxItem(camera));
}
if (comboBox1.Items.Count > 0)
{
comboBox1.SelectedItem = comboBox1.Items[0];
}
ComboBoxItem i = (ComboBoxItem)comboBox1.SelectedItem;
try
{
webCameraControl1.StartCapture(i.Id);
}
finally
{
//Do something if u want to
}
please help!!
Actually, creating a transparent label will not help over video. If your webCameraControl is not a sealed class, you can inherit it to add the text directly on it's surface, like I did with this picture box:
public partial class LabledPictureBox : PictureBox
{
public LabledPictureBox()
{
InitializeComponent();
}
#region properties
// I needed to override these properties to make them Browsable....
[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
[Browsable(true)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
[Browsable(true)]
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
#endregion properties
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
// This is actually the only code line that's needed to add the text to the picture box
TextRenderer.DrawText(pe.Graphics, this.Text, this.Font, pe.ClipRectangle, this.ForeColor);
}
}
The most important thing here is the line right after base.OnPaint - That line actually paints the text directly over the surface of the already painted control.
I want my ToolStrip Background to change when an Item is not saved.
To render the background of my toolstrip I use my own renderer:
class ToolStripRenderer : ToolStripProfessionalRenderer
{
private MenuBarForm parent;
public ToolStripRenderer(MenuBarForm Parent)
{
parent = Parent;
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
if (parent.controlItems.Last().Unsaved)
e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, Color.Red, 90, true), e.AffectedBounds);
else
e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, SystemColors.ControlDark, 90, true), e.AffectedBounds);
}
}
The first time the toolstrip renders it renders correctly with a grey to dark grey design:
But when the bar should become red, only the buttons which the mouse hovers over become red:
I would like the whole toolstrip the be red-colored at once.
I already tried changing e.AffectedBounds to e.ToolStrip.Bounds, to no avail.
You can create a custom color table inheriting ProfessionalColorTable and override relevant properties to change background color:
public class CustomColorTable : ProfessionalColorTable
{
public override Color ToolStripGradientBegin
{
get { return Color.Red; }
}
public override Color ToolStripGradientMiddle
{
get { return Color.Red; }
}
public override Color ToolStripGradientEnd
{
get { return SystemColors.ControlLightLight; }
}
}
To change your ToolStrip background, assign a new ToolStripProfessionalRenderer which uses your custom color table to ToolStripManager.Renderer:
ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new CustomColorTable());
To set the original professional renderer:
ToolStripManager.Renderer = new ToolStripProfessionalRenderer();
I Found this solution Thanks to FSDaniel comment:
By adding Invalidate() to the end of the OnRenderToolStripBackground the toolstrip did indeed become fully red but also caused the application to go into a infinite loop. I solved this by creating an event that was triggered by changing the UnSaved property. The form that has the toolstrip then subscribed a method to this event which called toolstrip.Invalidate(). This way Invalidate() is only used when necessary.
I have my ToolStripMenuItem and when I run the application, it looks like this:
As you can see, there is a little white space at the ToolStripMenuItem left.
How can I remove it? I tried to edit every property but it still remains...
Thank you all in advance!
To change appearance of menu item you should use a ToolStripProfessionalRenderer using a custom ProfessionalColorTable.
To change that color, you should override ImageMarginGradientBegin property of custom color table and return the color you want.
For example you can have :
public class CustomColorTable : ProfessionalColorTable
{
public override Color ImageMarginGradientBegin
{
get { return Color.Red; }
}
public override Color ImageMarginGradientMiddle
{
get { return Color.Green; }
}
public override Color ImageMarginGradientEnd
{
get { return Color.Blue; }
}
public override Color ToolStripDropDownBackground
{
get { return Color.Yellow; }
}
public override Color MenuItemSelected
{
get { return Color.Pink; }
}
//You should also override other properties if you need.
//This is just a sample code to show you the solution
}
And then in your form load:
private void Form_Load(object sender, EventArgs e)
{
ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new CustomColorTable());
}
I need to alternate the color of the items in my CheckedListBox but "alternatingColors" is not a property of CheckedListBox.
How do I go about making the item's colors alternate?
The OnDrawItem event is inaccessible by default, but if you derive a new control based on CheckedListBox, then you can override the base event.
public class MyCheckedListBox : CheckedListBox
{
private SolidBrush primaryColor = new SolidBrush(Color.White);
private SolidBrush alternateColor = new SolidBrush(Color.LightGreen);
[Browsable(true)]
public Color PrimaryColor
{
get { return primaryColor.Color; }
set { primaryColor.Color = value; }
}
[Browsable(true)]
public Color AlternateColor
{
get { return alternateColor.Color; }
set { alternateColor.Color = value; }
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (Items.Count <= 0)
return;
var contentRect = e.Bounds;
contentRect.X = 16;
e.Graphics.FillRectangle(e.Index%2 == 0 ? primaryColor : alternateColor, contentRect);
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), e.Font, Brushes.Black, contentRect);
}
}
It'll alternate between white and green by default. Make adjustments in the Properties panel at design time, or during runtime.
I don't think this is even possible. You may want to consider using a different control for what you need. A DataGridView might be able to work for you better.
How to change the Hover (mouse over) color of a Windows application menu?
Any method in C# ?
OR
Any way by using Windows API (DllImport) ?
See image :
You are using the MenuStrip class. You can override its renderer. Here's an example, pick your own colors please.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
menuStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer {
public MyRenderer() : base(new MyColors()) {}
}
private class MyColors : ProfessionalColorTable {
public override Color MenuItemSelected {
get { return Color.Yellow; }
}
public override Color MenuItemSelectedGradientBegin {
get { return Color.Orange; }
}
public override Color MenuItemSelectedGradientEnd {
get { return Color.Yellow; }
}
}
}
Other properties of ProfessionalColorTable control other color elements.
I had the similar question and I went through many articles, many forums, but have not found the perfect answer for my questions. I not only had the problem with the hover of the dropdown menu elements, but the background and overally the layout and how could I add sub-elements programmatically. Then I found how MenuStrip can be customized quiet easily in Stackoverflow forums, however I still got the issue with the dropdowns. Then I turend out by myself that ContextMenuStip has the properties to achieve the goals. It is easy to add any MenuStrip a ContextMenuStrip as a DropDown menu. Ohh, yes: The beauty in this is that you don't need to use any special components.
So, the steps are the following:
You need to have a color table.
You must use it on your MenuStrip.
ToolStripMenuItems on your MenuStrip must has a ContextMenuStrip as DropDown.
Through the ToolStripMenuItems.Items[?].DropDownItems function, you can easily manipulate the sub-elements that appears as drop-down elements.
1.- The color tables:
public class submenuColorTable : ProfessionalColorTable
{
public override Color MenuItemSelected
{
get { return ColorTranslator.FromHtml("#302E2D"); }
}
public override Color MenuItemBorder
{
get { return Color.Silver; }
}
public override Color ToolStripDropDownBackground
{
get { return ColorTranslator.FromHtml("#21201F"); }
}
public override Color ToolStripContentPanelGradientBegin
{
get { return ColorTranslator.FromHtml("#21201F"); }
}
}
public class LeftMenuColorTable : ProfessionalColorTable
{
public override Color MenuItemBorder
{
get { return ColorTranslator.FromHtml("#BAB9B9"); }
}
public override Color MenuBorder //added for changing the menu border
{
get { return Color.Silver; }
}
public override Color MenuItemPressedGradientBegin
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemPressedGradientEnd
{
get { return ColorTranslator.FromHtml("#5F5D5B"); }
}
public override Color ToolStripBorder
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemSelectedGradientBegin
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemSelectedGradientEnd
{
get { return ColorTranslator.FromHtml("#5F5D5B"); }
}
public override Color ToolStripDropDownBackground
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientBegin
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientEnd
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientMiddle
{
get { return ColorTranslator.FromHtml("#404040"); }
}
}
2.- Using it on MenuStrip:
menuStrip.Renderer = new ToolStripProfessionalRenderer(new LeftMenuColorTable());
3.- Adding ContextMenuStrip to the menu element programmatically
ContextMenuStrip CMS = new ContextMenuStrip()
{
Renderer = new ToolStripProfessionalRenderer(new submenuColorTable()),
ShowImageMargin = false
};
ToolStripMenuItem TSMI = new ToolStripMenuItem("Button name")
{
BackColor = sampleMenuItem.BackColor,
ForeColor = sampleMenuItem.ForeColor,
Font = sampleMenuItem.Font,
Margin = sampleMenuItem.Margin,
Padding = sampleMenuItem.Padding,
Size = sampleMenuItem.Size,
TextAlign = sampleMenuItem.TextAlign,
DropDown = CMS
};
menuStrip.Items.Add(TSMI);
4.- Manipulate the sub-elements
Here you can manipulate (for example: add) the elements of the drop-down menu. The color, size and other properties are just used this way for testing. You can use constant or different values. ("i" is the menu button index you want to add sub-entries)
ToolStripMenuItem newItem = new ToolStripMenuItem("Button Name", null, ToolStripMenuItem_Click)
{
Text = "Button Name",
BackColor = toolStripMenuItem01.BackColor,
ForeColor = toolStripMenuItem01.ForeColor,
Font = toolStripMenuItem01.Font,
Margin = toolStripMenuItem01.Margin,
Padding = toolStripMenuItem01.Padding,
Size = toolStripMenuItem01.Size
};
((ToolStripMenuItem)menuStrip.Items[i]).DropDownItems.Add(newItem);
The result is in my case the following:
This might be useful for others. Thanks for reading! Happy coding! :)
For changing the mouse-over border color (on items) use this:
public override Color MenuItemBorder
{
get { return Color.Green; }
}
You can also make it transparent (invisible):
get { return Color.Transparent; }