日期:2014-05-18  浏览次数:20978 次

button按扭初始问题??????????????????????????????????????????
button按扭初始问题

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsApplication6 

public partial class Form1 : Form 

public Form1() 

InitializeComponent(); 

private void Button1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 

//初始化一个GraphicsPath类的对象 
System.Drawing.Drawing2D.GraphicsPath myGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); 
//确定一个字符串,该字符串就是控件的形状 
string stringText = "Arial"; 
//确定字符串的字体 
FontFamily family = new FontFamily(stringText); 
//确定字符串的风格 
int fontStyle = (int)FontStyle.Bold; 
//确定字符串的高度 
int emSize = 35; 
//确定字符串的起始位置,它是从控件开始计算而非窗体 
PointF origin = new PointF(0, 0); 
//一个StringFormat对象来确定字符串的字间距以及对齐方式 
StringFormat format = new StringFormat(StringFormat.GenericDefault); 
//用AddString方法创建字符串 
myGraphicsPath.AddString(stringText, family, fontStyle, emSize, origin, format); 
//将控件的Region属性设置为上面创建的GraphicsPath对象 
CustomButton.Region = new Region(myGraphicsPath); 

private void Button1_Click(object sender, EventArgs e) 





上面的代码是初始按扭的造型 为什么运行时候显示不出 如果把Button1_Paint代码放在Button1_Click下,然后运行点击可以看到效果 但这不是我想要的结果 我想一开始能看到彩色无筐字体的按扭,到底哪不对????

------解决方案--------------------
把代码写到一个函数里,在load事件里调用一下
------解决方案--------------------
开始的时候不显示的话,只能说明Button1_Paint没有被执行,如果Button1_Paint事件写得正确的话,在load的时候执行一次Button1.Refresh()可能就可以了
------解决方案--------------------
设置按钮的形状不要在Paint事件中,自己从Button派生一个类,在构造函数中设置Button形状。