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();
}
展开
 我来答
隔岸观火的鱼
2012-01-15 · TA获得超过651个赞
知道小有建树答主
回答量:255
采纳率:0%
帮助的人:347万
展开全部
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。 然后自己调一下位置与宽度吧。
lqqiang123
2012-01-15
知道答主
回答量:7
采纳率:0%
帮助的人:1.1万
展开全部
加上这段
private void txt2_Click(object sender, EventArgs e)
{
this.txt2_Paint(sender, new PaintEventArgs(this.txt2.CreateGraphics(), txt2.ClientRectangle));
}
不过这样当鼠标点击txt2的时候 边框会闪动一下、、
追问
嗯,是会闪动一下,有更好的解决方案吗? 谢谢
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式