I want to create buttons with two texts: one left (text label itself) and one right (hotkey) aligned, and possibly an image. The screenshot below is from a similar application (not mine!)
It would be nice to support multiple lines of text, just like buttons F1-F6 in the image, but I can live without that. I don't need the "always-pressed state" of F8 button.
I'm using C# and windows forms.
EDIT: Apparently this is very simple in WPF. Never did anything in WPF, but will surely take a look.
Here is the solution you may be satisfied with:
public class XButton : Button
{
public XButton()
{
UseVisualStyleBackColor = false;
TextImageRelation = TextImageRelation.ImageAboveText;
}
public override string Text
{
get { return ""; }
set { base.Text = value;}
}
public string LeftText { get; set; }
public string RightText { get; set; }
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Rectangle rect = ClientRectangle;
rect.Inflate(-5, -5);
using (StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far })
{
using (Brush brush = new SolidBrush(ForeColor))
{
pevent.Graphics.DrawString(LeftText, Font, brush, rect, sf);
sf.Alignment = StringAlignment.Far;
pevent.Graphics.DrawString(RightText, Font, brush, rect, sf);
}
}
}
}
//Use it
xButton1.Image = yourImage;
xButton1.LeftText = "How interesting winforms is";
xButton2.RightText = "F12";
//You can add more properties to this XButton class to control how it looks
Screen shot