[C#] Subclassing ToolStrip + Overriding OnPaint Event

Nazgulled

Centurion
Joined
Jun 1, 2004
Messages
119
I have subclassed the ToolStrip and ToolStripProfessionalRenderer to make a toolbar work much like of Thunderbird's (version 2) options dialog. I have that goal accomplished but I have a little issue...

I also tried to override the OnPaint event of ToolStrip so I could 2 little lines just below the ToolStrip, but for some reason, it's producing unexpected results. The first 2 buttons in the ToolStrip also have parts of those lines painted above them and I can't see why that's happening. The strange thing is, it only happens on the first 2 buttons (I tried to delete them and re add them, but it didn't work).

Below, there's a screenshot and the code:

firenotesoptions.jpg


Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace FireNotes
{
	internal class MyToolStrip : ToolStrip
	{
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			Pen cDarkDark = new Pen(SystemColors.ControlDarkDark);
			Pen cLightLight = new Pen(SystemColors.ControlLightLight);

			e.Graphics.DrawLine(cDarkDark, 0, e.ClipRectangle.Height - 2, e.ClipRectangle.Width + 1, e.ClipRectangle.Height - 2);
			e.Graphics.DrawLine(cLightLight, 0, e.ClipRectangle.Height - 1, e.ClipRectangle.Width + 1, e.ClipRectangle.Height - 1);
		}

		protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
		{
			SwapSelectedButton((ToolStripButton)e.ClickedItem);
			base.OnItemClicked(e);
		}
		
		private void SwapSelectedButton(ToolStripButton tsbSelected) {
			ToolStripButton tsbThis;

			foreach (ToolStripItem tsItem in base.Items) {
				tsbThis = tsItem as ToolStripButton;

				if (tsbThis != tsbSelected) {
					tsbThis.Checked = false;
				} else {
					tsbThis.Checked = true;
				}
			}
		}
	}

	internal class MyToolStripProfessionalRenderer : ToolStripProfessionalRenderer
	{
		protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
		{
			ToolStripButton tsbItem = e.Item as ToolStripButton;
			Rectangle rect = new Rectangle(0, 0, 70, 43);

			if (tsbItem.Checked || (tsbItem.Selected && tsbItem.Checked)) {
				tsbItem.ForeColor = Color.White;
				e.Graphics.DrawImage(Properties.Resources.TSButton_State2, rect);
			} else if (tsbItem.Selected) {
				tsbItem.ForeColor = Color.White;
				e.Graphics.DrawImage(Properties.Resources.TSButton_State1, rect);
			}
		}
	}
}
 
Nevermind!

The problem was on e.ClipRectangle.Height/e.ClipRectangle.Width and I just replaced it by base.Height/base.Width and I also changed to the OnBackgroundPaint as it makes more sense.
 
Back
Top