日期:2014-05-18  浏览次数:20929 次

c#如何重绘窗口
c#如何重绘窗口

------解决方案--------------------
参考如下代码
C# code
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

private const int WM_NCPAINT = 0x0085;
private const int WM_NCACTIVATE = 0x0086;
private const int WM_NCLBUTTONDOWN = 0x00A1;
protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    Rectangle vRectangle = new Rectangle((Width - 75) / 2, 3, 75, 25);
    switch (m.Msg)
    {
        case WM_NCPAINT:
        case WM_NCACTIVATE:
            IntPtr vHandle = GetWindowDC(m.HWnd);
            Graphics vGraphics = Graphics.FromHdc(vHandle);
            vGraphics.FillRectangle(new LinearGradientBrush(vRectangle, 
                Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal),
                vRectangle);
            
            StringFormat vStringFormat = new StringFormat();
            vStringFormat.Alignment = StringAlignment.Center;
            vStringFormat.LineAlignment = StringAlignment.Center;
            vGraphics.DrawString("About", Font, Brushes.BlanchedAlmond, 
                vRectangle, vStringFormat);

            vGraphics.Dispose();
            ReleaseDC(m.HWnd, vHandle);
            break;
        case WM_NCLBUTTONDOWN:
            Point vPoint = new Point((int)m.LParam);
            vPoint.Offset(-Left, -Top);
            if (vRectangle.Contains(vPoint))
                MessageBox.Show(vPoint.ToString());
            break;
    }
}