我前几天有做,也碰到点问题,一起探讨下:
边框我有了
但是我的问题是背景图,每次输入文字就会重绘,以致看不到输入的文字(实际.text已经得到)
是不是还要override onTextChange?我的代码如下:public class TextBoxBlue:TextBox
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
int WM_PAINT=15;
if (m.Msg==WM_PAINT)//好象这个判断不用也可以
{
Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawRectangle(Pens.Blue,this.ClientRectangle.Left,this.ClientRectangle.Top,this.ClientRectangle.Width - 1,this.ClientRectangle.Height - 1);//画边框
g.DrawImage((Bitmap)Bitmap.FromFile(@"c:\00.bmp"),this.ClientRectangle.Left,this.ClientRectangle.Top,this.ClientRectangle.Width - 1,this.ClientRectangle.Height - 1);//画背景图
g.Dispose();
}
}
}
边框我有了
但是我的问题是背景图,每次输入文字就会重绘,以致看不到输入的文字(实际.text已经得到)
是不是还要override onTextChange?我的代码如下:public class TextBoxBlue:TextBox
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
int WM_PAINT=15;
if (m.Msg==WM_PAINT)//好象这个判断不用也可以
{
Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawRectangle(Pens.Blue,this.ClientRectangle.Left,this.ClientRectangle.Top,this.ClientRectangle.Width - 1,this.ClientRectangle.Height - 1);//画边框
g.DrawImage((Bitmap)Bitmap.FromFile(@"c:\00.bmp"),this.ClientRectangle.Left,this.ClientRectangle.Top,this.ClientRectangle.Width - 1,this.ClientRectangle.Height - 1);//画背景图
g.Dispose();
}
}
}
你不觉得边框还是没有改变吗,只是在编辑区的外围加了一个兰色框
你的文字也要重绘啊,用g.DrawString 不过我试了好象又把背景图覆盖了
而且this.ClientRectangle得到的是客户区的大小,要使用this.Size.
在WndProc中处理WM_NCPAINT消息即可。
如果有尺寸的变化,还需要处理WM_NCCALCSIZE消息。
protected override void WndProc(ref Message m)
{
switch( m.Msg )
{
case Platform.NativeMethods.WM_NCPAINT:
Graphics g = Graphics.FromHdc(...GetWindowDC(m.HWnd));//API
g.Draw...(...);
base.WndProc(ref m);
...
}
}
{
int msg = m.Msg;
base.WndProc (ref m);
switch( msg )
{
case WM_NCPAINT:
{
Graphics borderG = this.Parent.CreateGraphics();
borderG.DrawRectangle( new Pen(this.BackColor), this.Left+1, this.Top+1, this.Width-2, this.Height-2 );
break;
}
}
}
ControlPaint.DrawBorder
ControlPaint.DrawBorder
ControlPaint.DrawBorder
ControlPaint.DrawBorder
ControlPaint.DrawBorder
ControlPaint.DrawBorder
似乎应该是:borderG.DrawRectangle( new Pen(this.BackColor), this.Left-1, this.Top-1, this.Width-2, this.Height-2 );
我都试过了,就是没用,现在关键是Graphic对象,我试了一下:
用this.CreateGraphics(); 只能在TextBox的编辑区内绘图。
用this.Parent.CreateGraphics(); 就只能在它的Parent上绘图。
始终不能影响边框。
我尝试过上面两个Graphic填充一个区域,但最后边框还是没有改变
Graphics g = Graphics.FromHdc(...GetWindowDC(m.HWnd));//API
需调用GetWindowDC(API函数)得到windowDC
以搞定:
switch( msg )
{
case WM_NCPAINT:
{
Pen pen = new Pen(this.BackColor);
Graphics borderG = Graphics.FromHdc( GetWindowDC(m.HWnd) ); borderG.DrawRectangle( pen, 1, 1, this.Size.Width-2, this.Size.Height- 2 ); borderG.Dispose();
pen.Dispose();
break;
}
}
.net Framework中要强制发一个WM_NCPAINT消息,该用什么方法