C#winform中,我想点击button1就将txt2的边框颜色改变,下面的代码可以实现:
但如果我将鼠标点一下txt2文本时,边框颜色就会恢复默认,请教一下应如何保持更改的边框颜色?谢谢!privatevoidbutton1_Click(objectsende...
但如果我将鼠标点一下txt2文本时,边框颜色就会恢复默认,请教一下应如何保持更改的边框颜色? 谢谢!
private void button1_Click(object sender, EventArgs e)
{
this.txt2_Paint(sender, new PaintEventArgs(this.txt2.CreateGraphics(), txt2.ClientRectangle));
}
private void txt2_Paint(object sender,PaintEventArgs e)
{
Pen pen = new Pen(Color.HotPink, 4.5f);
e.Graphics.DrawRectangle(pen,txt2.ClientRectangle );
pen.Dispose();
} 展开
private void button1_Click(object sender, EventArgs e)
{
this.txt2_Paint(sender, new PaintEventArgs(this.txt2.CreateGraphics(), txt2.ClientRectangle));
}
private void txt2_Paint(object sender,PaintEventArgs e)
{
Pen pen = new Pen(Color.HotPink, 4.5f);
e.Graphics.DrawRectangle(pen,txt2.ClientRectangle );
pen.Dispose();
} 展开
展开全部
private static int WM_NCPAINT = 0x0085;
private static int WM_ERASEBKGND = 0x0014;
private static int WM_PAINT = 0x000F;
[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT || m.Msg == WM_ERASEBKGND || m.Msg == WM_PAINT)
{
IntPtr hdc = GetDCEx(m.HWnd, (IntPtr)1, 1 | 0x0020);
if (hdc != IntPtr.Zero)
{
Graphics graphics = Graphics.FromHdc(hdc);
Color borderColor = Color.HotPink;
Rectangle rectangle = new Rectangle(textBox1.Location.X, textBox1.Location.Y + (25), textBox1.Width, textBox1.Height);
ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
m.Result = (IntPtr)1;
ReleaseDC(m.HWnd, hdc);
}
}
}
------------------------------------------
用这种,不会闪,需要把textbook的边框去掉,Border设置为None。 然后自己调一下位置与宽度吧。
private static int WM_ERASEBKGND = 0x0014;
private static int WM_PAINT = 0x000F;
[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT || m.Msg == WM_ERASEBKGND || m.Msg == WM_PAINT)
{
IntPtr hdc = GetDCEx(m.HWnd, (IntPtr)1, 1 | 0x0020);
if (hdc != IntPtr.Zero)
{
Graphics graphics = Graphics.FromHdc(hdc);
Color borderColor = Color.HotPink;
Rectangle rectangle = new Rectangle(textBox1.Location.X, textBox1.Location.Y + (25), textBox1.Width, textBox1.Height);
ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
m.Result = (IntPtr)1;
ReleaseDC(m.HWnd, hdc);
}
}
}
------------------------------------------
用这种,不会闪,需要把textbook的边框去掉,Border设置为None。 然后自己调一下位置与宽度吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询