Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

http://stuff.nazgulled.net/images/firenotesoptions.jpg

 

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);
		}
	}
}
}

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...