Override the drawing of some .Net Framework controls to change its border color?

前端 未结 2 457
野的像风
野的像风 2021-01-21 22:53

SCENARIO

I\'m using a 3rd party windows visual theme.

When I see my application, it looks like this:

2条回答
  •  抹茶落季
    2021-01-21 23:30

    You can catch the WM_PAINT or WM_ERASEBKGND message and draw the border manually:

    [DllImport("user32.dll")]
    static extern IntPtr GetWindowDC(IntPtr hWnd);
    
    [DllImport("user32.dll")]
    static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
    
    protected override void WndProc(ref Message m)
    {
        IntPtr hdc;
        if (m.Msg == 0x14) //WM_ERASEBKGND
        {
            hdc = GetWindowDC(m.HWnd);
    
            if (hdc != IntPtr.Zero)
            {
                using (Graphics g = Graphics.FromHdc(hdc))
                {
                    g.DrawRectangle(Pens.Red, 0, 0, this.Width-1, this.Height-1);
                }
                ReleaseDC(m.HWnd, hdc);
            }
    
        base.WndProc(ref m);
    }
    

    It does have however a problem when the textbox looses it's focus.

提交回复
热议问题