如何定义任意形状的Windows控件? 我记得原来vb中有API函数可以这样的,C#中应该可以的,你到www.allapi.net上看看,有没有(英文)。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考:仿Windows Media Player的播放器http://www.dapha.net/down/show.asp?classid=2 GraphicsPathRegion两个类共同使用,可以搞定! 通过继承修改我做了一个修改的例子class;在使用的时候直接声明就行,主要是通过重载OnPaint事件修改(用GraphicDraw)using System;using System.Drawing;using System.Windows;using System.Windows.Forms; namespace Buttons{ /// <summary> /// Summary description for MyButton. /// </summary> public class MyButton:System.Windows.Forms.Button { private bool mouseDown=false; //设置鼠标位置状态按下 private bool mouseHover=false; //设置鼠标位置状态悬停 public MyButton() { // // TODO: Add constructor logic here //// BackColor=System.Drawing.Color.Blue; //设置背景色为蓝色// FlatStyle=System.Windows.Forms.FlatStyle.Flat;// Font=System.Windows.Forms.Control.DefaultFont;// ForeColor=System.Drawing.Color.White; MouseDown+=new MouseEventHandler(OnMouseDown); //重写鼠标按下事件 MouseUp+=new MouseEventHandler(OnMouseUp); //重写鼠标弹上事件 MouseEnter+=new EventHandler(OnMouseEnter); //重写鼠标焦点事件 MouseLeave+=new EventHandler(OnMouseLeave); //重写鼠标悬停事件 } protected override void OnPaint(PaintEventArgs pe) //重载OnPaint事件 { pe.Graphics.Clear(this.Parent.BackColor); pe.Graphics.FillEllipse(new SolidBrush(Parent.BackColor),pe.ClipRectangle); //填充一个矩形的形状来展示按钮 if (Enabled==false) //没有被灰掉 { DrawDisableButton(pe.Graphics); WriteText(pe.Graphics); //添加按钮文字 } else if (mouseDown) //当鼠标按下 { DrawMouseDownButton(pe.Graphics); WriteTextL(pe.Graphics); } else if (mouseHover) //当鼠标悬停 { DrawMouseHoverButton(pe.Graphics); WriteText(pe.Graphics); //添加按钮文字 } else if (Focused) //当按钮得到焦点 { DrawContainFocusButton(pe.Graphics); WriteText(pe.Graphics); //添加按钮文字 } else //当处在正常条件下 { DrawNomalButton(pe.Graphics); WriteText(pe.Graphics); //添加按钮文字 } } private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) //当鼠标按下 { mouseDown=true; } private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { mouseDown=false; PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮 OnPaint(pe); } private void OnMouseEnter(object sender, System.EventArgs e) { mouseHover=true; PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮 OnPaint(pe); } private void OnMouseLeave(object sender, System.EventArgs e) { mouseHover=false; PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮 OnPaint(pe); } private void DrawBorder(Graphics g,int state) { //int x=0,y=0; //填充矩形位置 if (state==1) {// Pen p=new Pen(SystemColors.ControlLightLight,2); } else if (state==2) //当鼠标悬停的时候 { } else if (state==3) { } else if (state==5) { } else if (state==6) //当按钮灰掉的时候,重画按钮 {// Pen p=new Pen(Color.FromArgb(161,161,161) ,1); } else //当按钮为普通状态的时候 {// Pen p=new Pen(Color.FromArgb(161,161,161) ,1); } } private void DrawNomalButton(Graphics g) { DrawBorder(g,1); PaintBack(g,SystemColors.ControlLightLight); } private void DrawMouseHoverButton(Graphics g) { DrawBorder(g,2); PaintBack(g,Color.SkyBlue); //当鼠标悬停的时候背景变篮 } private void DrawMouseDownButton(Graphics g) { DrawBorder(g,3); PaintBack(g,SystemColors.ControlLightLight); } private void DrawDisableButton(Graphics g) { DrawBorder(g,4); PaintBack(g,SystemColors.ControlLightLight); } private void DrawContainFocusButton(Graphics g) { DrawBorder(g,5); PaintBack(g,SystemColors.ControlLightLight); } private void PaintBack(Graphics g,Color c) //融合背景色 { g.FillEllipse(new SolidBrush(c),3,3,Width-6,Height-6); //用椭圆填充背景色3,3,Width-6,Height-6 } private void WriteText(Graphics g) { int x=0,y=0; Size s=g.MeasureString(Text,Font).ToSize(); x=(Width-s.Width)/2; y=(Height-s.Height)/2; if (Enabled) //画文字 { g.DrawString(Text,Font,Brushes.Black,x,y); } else { g.DrawString(Text,Font,Brushes.Gray,x,y); } } private void WriteTextL(Graphics g) { int x=0,y=0; Size s=g.MeasureString(Text,Font).ToSize(); x=(Width-s.Width)/2; y=(Height-s.Height)/2; if (Enabled) //画文字 { g.DrawString(Text,Font,Brushes.Black,x+1,y+1); } else { g.DrawString(Text,Font,Brushes.Gray,x+1,y+1); } } }} 请问谁有c#封装activex的方法 GridView中数据行上下移动 硬盘的C分区在每次格式化后序列号(有人叫分区ID)都不同,请问能通过代码修改成格式化前的那个序列号吗? txt文本文件抽取字符串问题 客户要求把所有报表导出为pdf,存放到服务器,这个reportviewer可以实现吗? 访问简单数据库,菜鸟求救 对控件的动态引用?????????? C#怎么模拟提交登陆信息后得到的返回页面? 在线等。急!! log4net 记录数据库问题? 如何用程序创建一个快捷方式,放到桌面上? 如何让一个线程重启动? 请问,如何在C#里面改变一个窗体标题的字体?
仿Windows Media Player的播放器
http://www.dapha.net/down/show.asp?classid=2
Region
两个类共同使用,可以搞定!
我做了一个修改的例子class;
在使用的时候直接声明就行,主要是通过重载OnPaint事件修改(用GraphicDraw)
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms; namespace Buttons
{
/// <summary>
/// Summary description for MyButton.
/// </summary>
public class MyButton:System.Windows.Forms.Button
{
private bool mouseDown=false; //设置鼠标位置状态按下
private bool mouseHover=false; //设置鼠标位置状态悬停
public MyButton()
{
//
// TODO: Add constructor logic here
//
// BackColor=System.Drawing.Color.Blue; //设置背景色为蓝色
// FlatStyle=System.Windows.Forms.FlatStyle.Flat;
// Font=System.Windows.Forms.Control.DefaultFont;
// ForeColor=System.Drawing.Color.White;
MouseDown+=new MouseEventHandler(OnMouseDown); //重写鼠标按下事件
MouseUp+=new MouseEventHandler(OnMouseUp); //重写鼠标弹上事件
MouseEnter+=new EventHandler(OnMouseEnter); //重写鼠标焦点事件
MouseLeave+=new EventHandler(OnMouseLeave); //重写鼠标悬停事件
}
protected override void OnPaint(PaintEventArgs pe) //重载OnPaint事件
{
pe.Graphics.Clear(this.Parent.BackColor);
pe.Graphics.FillEllipse(new SolidBrush(Parent.BackColor),pe.ClipRectangle); //填充一个矩形的形状来展示按钮
if (Enabled==false) //没有被灰掉
{
DrawDisableButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
else if (mouseDown) //当鼠标按下
{
DrawMouseDownButton(pe.Graphics);
WriteTextL(pe.Graphics);
}
else if (mouseHover) //当鼠标悬停
{
DrawMouseHoverButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
else if (Focused) //当按钮得到焦点
{
DrawContainFocusButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
else //当处在正常条件下
{
DrawNomalButton(pe.Graphics);
WriteText(pe.Graphics); //添加按钮文字
}
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) //当鼠标按下
{
mouseDown=true;
}
private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouseDown=false;
PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void OnMouseEnter(object sender, System.EventArgs e)
{
mouseHover=true;
PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void OnMouseLeave(object sender, System.EventArgs e)
{
mouseHover=false;
PaintEventArgs pe= new PaintEventArgs(CreateGraphics(),ClientRectangle); //重画矩形按钮
OnPaint(pe);
}
private void DrawBorder(Graphics g,int state)
{
//int x=0,y=0; //填充矩形位置
if (state==1)
{
// Pen p=new Pen(SystemColors.ControlLightLight,2); }
else if (state==2) //当鼠标悬停的时候
{ }
else if (state==3)
{ }
else if (state==5)
{ }
else if (state==6) //当按钮灰掉的时候,重画按钮
{
// Pen p=new Pen(Color.FromArgb(161,161,161) ,1); }
else //当按钮为普通状态的时候
{
// Pen p=new Pen(Color.FromArgb(161,161,161) ,1); }
}
private void DrawNomalButton(Graphics g)
{
DrawBorder(g,1);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g,2);
PaintBack(g,Color.SkyBlue); //当鼠标悬停的时候背景变篮
}
private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g,3);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g,4);
PaintBack(g,SystemColors.ControlLightLight);
} private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g,5);
PaintBack(g,SystemColors.ControlLightLight);
}
private void PaintBack(Graphics g,Color c) //融合背景色
{
g.FillEllipse(new SolidBrush(c),3,3,Width-6,Height-6); //用椭圆填充背景色3,3,Width-6,Height-6
}
private void WriteText(Graphics g)
{
int x=0,y=0;
Size s=g.MeasureString(Text,Font).ToSize();
x=(Width-s.Width)/2;
y=(Height-s.Height)/2;
if (Enabled) //画文字
{
g.DrawString(Text,Font,Brushes.Black,x,y);
}
else
{
g.DrawString(Text,Font,Brushes.Gray,x,y);
}
}
private void WriteTextL(Graphics g)
{
int x=0,y=0;
Size s=g.MeasureString(Text,Font).ToSize();
x=(Width-s.Width)/2;
y=(Height-s.Height)/2;
if (Enabled) //画文字
{
g.DrawString(Text,Font,Brushes.Black,x+1,y+1);
}
else
{
g.DrawString(Text,Font,Brushes.Gray,x+1,y+1);
}
}
}
}