Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created November 5, 2020 14:02
Show Gist options
  • Save t3knoid/03de78bfd466cf21bb955b3ddc6e7178 to your computer and use it in GitHub Desktop.
Save t3knoid/03de78bfd466cf21bb955b3ddc6e7178 to your computer and use it in GitHub Desktop.
This is an example on how to use a toolstrip render. This example shows by making a ToolStripButton object non-clickable by changing the background colors to match the system colors in every case.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomRenders
{
/// <summary>
/// Provides an alternate way of painting toolstrip objects
///
/// https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripsystemrenderer?view=netframework-4.8
/// </summary>
class Renderers : ToolStripProfessionalRenderer
{
/// <summary>
/// Overrides OnRendererButtonBackground
///
/// https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripsystemrenderer.onrenderbuttonbackground?view=netframework-4.8
/// </summary>
/// <param name="e"></param>
protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
{
if (!e.Item.Selected)
{
base.OnRenderButtonBackground(e);
}
else
{
switch (e.Item.Name)
{
case "toolStripButton1": // This makes the button non-clickable
Rectangle rectangle = new Rectangle(0, 0, e.Item.Size.Width - 1, e.Item.Size.Height - 1);
e.Graphics.FillRectangle(SystemBrushes.Control, rectangle);
e.Graphics.DrawRectangle(SystemPens.Control, rectangle);
break;
default:
base.OnRenderButtonBackground(e);
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment