本文介绍了如何使用C#将文本和图像放入标准Winform按钮中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试为我的应用程序创建一个自定义的标准Winform Button,在其中可以放置带有一些文本的图像(例如Label Control),并禁用Standard按钮的默认行为.
任何帮助将不胜感激.
在Advance中致谢
Hi all,
I am trying to create a customize standard Winform Button for my application in which I can put an image with some text(like Label Control)and also to disable the default behavior of the Standard button.
Any help will be appreciated.
Thanks in Advance
推荐答案
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
class myButton : Panel
{
public myButton()
{
this.Size = new Size(75, 23); // this is just to set size to default button size change as needed
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; // makes a border around button
}
private Image buttonImage
{
get
{
string base64string = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA1RJREFUeNq0VetLVFEQn3P35aobahpEUtkXQSw2A+1BEPTVPgRiCqsSQn71TwmCyCAMM5IQIZLwSxBIpNGGhRBEYmUPRFvfu3v3nkcz99y9u3dd0Q0aGC7nnDm/md/MnLlMKQWMMSDp+6iCYMAtXHbg8gIeBWEfca5m0PYNKBiXAu4/irIMbdrYWQexuDoGDCZxP1oeBgiX4WU4gDhGqTTATpJQYU4JaH/cyn66DnrmIMjTKo7WzbWH0UaQd60HZADMB2CgrqziQqp5X4idG22BjJ8O0+tygDGjua4WgKcQWNqRgKa5P7jNAtUwAGoi6GSFNfO0HMCdOwadWztmTyiAX8yc4BgAMhCOSrm3ujakeI+TIgZhZRCTsG0GCvzRoF8bQZHUBDGMwUaAxkN63T+7BxuhmRAWMH+U9m0GkosAgUsneo9ipD6WAyd50LbbLss8iyM4D7gOOPIkqtyhKfJSRBc3sS69096Ihy8W2BXe5wJyDITcHXkRjb3yOhm5XNxOs5Hg1kDggjazPX29AaDzFBxIRq8AdL/07lENhePAYSA8ERwUPCtPrurW9jLJS5HgmgEpC8A/iVEGLobWXQyoDgoMnD4Ty6WB98azDJSjwmWga2AJ7RGbmZs+GF8EeLasnz+1nLWND2cT7UwFT68xCPpy4LF3+jy9JvJqIF0GtgMpla46Okgl8JsoHunzDm/+umewLXcI3PJUmBxIje+8ZMwLxwbGeZQ3YLzy4kbIs+58TSMGwFwzCzpIaidGIFcDI1jB8eVha3FMCXdzmFV/ZQgmlnIgHfjozA0c0atJj53gln2fWxY2S3kqlyJIf05nWFPIr5y54mVCdRj5hD2/gMHRa02Sg+2ikZOYmH9lmFjJCu0g+evLFDvR2hRSKZBW2naZP49TifW9W6hgMhqBMsiEwpD89nYKoFWnaOFe/11ubi5uGGEIVFUTPbfw+yrZodIduksYhEWYdozOL9OoOd/Vfvzm0G1f+FBDZSVAJFLaW9jaAtjGrInU5uL34YHBxMzYJGJLlvfTDwWqjradHBjtCtefvuSP1J0pxQHf/jOfWvow/XUoNmat/55teajMeF+OgZtC1HrUI6jVJU4LKhTNgB9nh7HiCPk+6+B/yl8BBgDnEVPWrXYOEgAAAABJRU5ErkJggg==";
byte[] base64byte = Convert.FromBase64String(base64string);
MemoryStream base64stream = new MemoryStream(base64byte);
return Image.FromStream(base64stream);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// do mousedown events
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
// do mouseup events
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawButtonImage(e);
DrawButtonText(e);
}
private void DrawButtonText(PaintEventArgs e)
{
e.Graphics.DrawString("button1",this.Font,Brushes.Black,new PointF(25,3));
}
private void DrawButtonImage(PaintEventArgs e)
{
e.Graphics.DrawImage(buttonImage, 0, 0, 20, 20);
}
}
这篇关于如何使用C#将文本和图像放入标准Winform按钮中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!