VS2015 C# 自定义"MyButton"类,为什么“MyButton”类里面的Image属性无法添加图片?

NieC2018 2018-06-28 01:26:14
我直接附上代码:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyButton
{
//自定义枚举
public enum MouseState
{
MouseDown = 1,
MouseUp = 2,
Focused = 3,
Enable = 4,
False = 5
}
public class MyButton:Button
{

//私有变量
MouseState M;

//重绘事件
protected override void OnPaint(PaintEventArgs pevent)
{
//绘制原生控件
base.OnPaint(pevent);
//重刷一下背景清掉原生绘制
base.OnPaintBackground(pevent);

//重绘边框
Graphics g = pevent.Graphics;
g.DrawLine(new Pen(Color.White, 0.5f), 0, 0, this.Width - 1, 0);
g.DrawLine(new Pen(Color.White, 0.5f), 0, 0, 0, this.Height - 1);
g.DrawLine(new Pen(Color.Gray, 0.5f), this.Width - 1, 0, this.Width - 1, this.Height - 1);
g.DrawLine(new Pen(Color.Gray, 0.5f), 0, this.Height - 1, this.Width - 1, this.Height - 1);

g.DrawLine(new Pen(Color.White, 0.5f), 1, 1, this.Width - 2, 1);
g.DrawLine(new Pen(Color.White, 0.5f), 1, 1, 1, this.Height - 2);
g.DrawLine(new Pen(Color.DarkGray, 0.5f), this.Width - 2, 1, this.Width - 2, this.Height - 2);
g.DrawLine(new Pen(Color.DarkGray, 0.5f), 1, this.Height - 2, this.Width - 2, this.Height - 2);

//绘制文本内容
//RectangleF RF = new Rectangle(new Point(0,0),new Size(this.Width,this.Height));
SizeF TextSize = g.MeasureString(this.Text, this.Font);
int Width_OffSet = (int)TextSize.Width;
int Height_OffSet = (int)TextSize.Height;
g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), (this.Width - Width_OffSet) / 2, (this.Height - Height_OffSet) / 2);

//根据按钮状态显示
switch (M)
{
case MouseState.MouseDown:
g.DrawLine(new Pen(Color.Black, 0.5f), 0, 0, this.Width - 1, 0);
g.DrawLine(new Pen(Color.Black, 0.5f), 0, 0, 0, this.Height - 1);
g.DrawLine(new Pen(Color.Black, 0.5f), this.Width - 1, 0, this.Width - 1, this.Height - 1);
g.DrawLine(new Pen(Color.Black, 0.5f), 0, this.Height - 1, this.Width - 1, this.Height - 1);

g.DrawLine(new Pen(Color.Gray, 0.5f), 1, 1, this.Width - 2, 1);
g.DrawLine(new Pen(Color.Gray, 0.5f), 1, 1, 1, this.Height - 2);
g.DrawLine(new Pen(Color.White, 0.5f), this.Width - 2, 1, this.Width - 2, this.Height - 2);
g.DrawLine(new Pen(Color.White, 0.5f), 1, this.Height - 2, this.Width - 2, this.Height - 2);

Pen P = new Pen(Color.Black, 0.3f);
P.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
P.DashPattern = new float[] { 0.3f, 0.3f };

g.DrawLine(P, 3, 3, this.Width - 4, 3);
g.DrawLine(P, 3, 3, 3, this.Height - 4);
g.DrawLine(P, this.Width - 4, 3, this.Width - 4, this.Height - 4);
g.DrawLine(P, 3, this.Height - 4, this.Width - 4, this.Height - 4);
break;
case MouseState.MouseUp:
//...
break;
case MouseState.Focused:
//...
break;
case MouseState.Enable:
//...
break;
case MouseState.False:
//...
break;
default:
break;
}
}

以上是全部重绘代码,有几个问题请教各位大神!
1、“MyButton”类编译通过,能正常放到其他项目使用,但是“MyButton”里面的Image属性添加的图片不能显示!通过ImageList也不行!(是不是Image也要重绘啊?如果真是这样,那么该怎么重绘Image?)

2、我现在的做法是往BlackGroudImage添加图片,但是后面就有一个问题:因为我这个“MyButton”有可能是False(不启用状态)。那么就需要图片变成灰色的(就是VS自带Button有的那种功能)。如果是BlackGroudImage添加图片,想要实现这个功能就比较难,当然也有替代办法,但是我不想将就,本身效果也不是很理想。

3、本人菜鸟一个,由于项目需要,现学现卖,希望各位不要见笑。我的分数也不多,只有100分,全部奉上。希望各位帮我解决这个问题,谢谢大家!
...全文
168 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
NieC2018 2018-06-30
  • 打赏
  • 举报
回复
引用 3 楼 just59277 的回复:
请问你现在是想要达到一个什么样的效果? 你代码里面 base.OnPaintBackground(pevent); 这句不就把原有的背景(包含image属性在内)给“替换”掉了吗?


谢谢,您说的对!把这条代码“base.OnPaintBackground(pevent);”删除就可以了,本质上:还是自己没有完全理解继承的含义。
NieC2018 2018-06-30
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
Button 有 Image 属性吗?
如果没用,你并没有给出定义
如果有,请确认其默认行为符合你的要求


谢谢,版主言简意赅,但是我明白你的意思。确实是我把父类的原有的背景给清除了,包括:Image属性。
just59277 2018-06-28
  • 打赏
  • 举报
回复
请问你现在是想要达到一个什么样的效果? 你代码里面 base.OnPaintBackground(pevent); 这句不就把原有的背景(包含image属性在内)给“替换”掉了吗?
xuzuning 2018-06-28
  • 打赏
  • 举报
回复
Button 有 Image 属性吗?
如果没用,你并没有给出定义
如果有,请确认其默认行为符合你的要求
NieC2018 2018-06-28
  • 打赏
  • 举报
回复
自己顶一下!

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧