重写TextBox的OnPaint方法,为什么不行?

gxg2004csdn 2004-10-09 09:31:10
public class MyTextBox : TextBox
{
private Color _BorderColor = Color.Black;

public MyTextBox()
{


base.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;


}
public Color BorderColor
{
get{return _BorderColor;}
set{_BorderColor = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(new Pen(_BorderColor),0,0,base.Width,base.Height);
MessageBox.Show("ok");




}

}
...全文
357 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSTerry 2004-10-09
  • 打赏
  • 举报
回复
留QQ?

……不用了吧,讨论问题还是论坛方便
gxg2004csdn 2004-10-09
  • 打赏
  • 举报
回复
CSTerry能否留下你的QQ,我的QQ是359149453
CSTerry 2004-10-09
  • 打赏
  • 举报
回复
看看这个例子

需要注意的地方是从ToolBar继承的自定义控件不能直接override它的OnPaint函数,必须在它的construct函数设置它的外观由用户绘制,定义代码如下:
this.SetStyle(ControlStyles.UserPaint,true);

/// <summary>
/// 实现XP风格的工具栏按钮
/// </summary>
public class ToolBarXP : System.Windows.Forms.ToolBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Color fillColorHover = Color.FromArgb(182,189,210);
private Color borderColorHover = Color.Navy;
private Color fillDownColor = Color.RosyBrown;
private Color borderDownColor = Color.SeaGreen;


public ToolBarXP(System.ComponentModel.IContainer container)
{
///
/// Required for Windows.Forms Class Composition Designer support
///
container.Add(this);
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.SetStyle(ControlStyles.UserPaint,true);
}

public ToolBarXP()
{
///
/// Required for Windows.Forms Class Composition Designer support
///
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.SetStyle(ControlStyles.UserPaint,true);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// 鼠标在按钮之上时,按钮呈现的背景色
/// </summary>
[Description("鼠标在按钮之上时,按钮呈现的背景色"),
Category("Natrpan")]
public Color FillColorHover
{
get { return fillColorHover;}
set { fillColorHover = value;}
}
/// <summary>
/// 鼠标在按钮之上时,按钮呈现的边框色
/// </summary>
[Description("鼠标在按钮之上时,按钮呈现的边框色"),
Category("Natrpan")]
public Color BorderColorHover
{
get {return borderColorHover;}
set { borderColorHover = value;}
}

/// <summary>
/// 按钮被按下时的填充背景色
/// </summary>
[Description("按钮被按下时的背景色"),
Category("Natrpan")]
public Color FillDownColor
{
get {return fillDownColor;}
set { fillDownColor = value;}
}

/// <summary>
/// 按钮被按下时的边框色
/// </summary>
[Description("按钮被按下时的边框色"),
Category("Natrpan")]
public Color BorderDownColor
{
get {return borderDownColor;}
set { borderDownColor = value;}
}


protected override void OnPaint(PaintEventArgs e)
{

Graphics g = e.Graphics;

for(int i=0;i<this.Buttons.Count;i++)
{
if(this.Buttons[i].Rectangle.Contains(PointToClient(MousePosition)))
{
g.FillRectangle(new SolidBrush(this.fillColorHover),this.Buttons[i].Rectangle);
g.DrawImage(this.ImageList.Images[i],this.Buttons[i].Rectangle.X+1,this.Buttons[i].Rectangle.Y+1);
g.DrawRectangle(new Pen(this.borderColorHover),this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y,this.Buttons[i].Rectangle.Width-2,this.Buttons[i].Rectangle.Height-2);

continue;


}

// g.DrawRectangle(Pens.Red,this.Buttons[i].Rectangle);
g.DrawImage(this.ImageList.Images[i],this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y);

}
// g.FillRectangle(new SolidBrush(Color.Purple),this.ClientRectangle);
// base.OnPaint(e);
}

protected override void OnMouseHover(EventArgs e)
{
// Graphics g = this.CreateGraphics();
// g.DrawRectangle(Pens.SaddleBrown,this.Buttons[0].Rectangle);
// g.Dispose();
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
TrustNone 2004-10-09
  • 打赏
  • 举报
回复
需要捕获Windows传递的消息,别人的代码可以参考一下
class MyTextBox : System.Windows.Forms.TextBox

{

[DllImport("user32.dll")]

static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]

static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

const int WM_NCPAINT = 0x0085;
protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

//Debug.WriteLine("WM_NCPAINT: hWnd = "+Handle.ToString()+"WParam = "+m.WParam.ToString());

IntPtr hDC = GetWindowDC(Handle);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

return;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);

g.DrawRectangle(p,r);


ReleaseDC(Handle,hDC);

}

protected override void WndProc(ref Message m)

{

switch(m.Msg)

{

case WM_NCPAINT:

{

base.WndProc(ref m);

Debug.WriteLine("WM_NCPAINT: hWnd = "+m.HWnd.ToString()+"WParam ="+m.WParam.ToString());

//Region rgn = Region.FromHrgn(m.WParam);


IntPtr hDC = GetWindowDC(m.HWnd);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

break;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);

g.DrawRectangle(p,r);

m.Result = IntPtr.Zero;

ReleaseDC(m.HWnd,hDC);

return;

}

}

base.WndProc(ref m);

}

}
CSTerry 2004-10-09
  • 打赏
  • 举报
回复
类似例子……

需要注意的地方是从ToolBar继承的自定义控件不能直接override它的OnPaint函数,必须在它的construct函数设置它的外观由用户绘制,定义代码如下:

this.SetStyle(ControlStyles.UserPaint,true);


微启子 2004-10-09
  • 打赏
  • 举报
回复
系统不让啊!需要知道微软的接口!

110,533

社区成员

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

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

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