如何为WinForm、C#制作框架?

How to make a frame for WinForm, C#?

我一直在考虑更改 windows 表单的边框颜色,发现它是由 windows 决定的,好的,所以我看到那些有之前问过这个问题被告知去这里 http://customerborderform.codeplex.com/ 看起来该网站目前无法使用。那么我必须自己制作框架吗?如果是这样,我会去哪里解决这个问题?我正在使用 visual studio 2012.

我想你想制作自定义 winform。在这种情况下,您可能不想渲染默认值 windows。在 photoshop 中绘制所需的 winform,并将其用作应用程序中的背景。这种方法的问题是您需要设计自己的最小化、最大化和关闭按钮。

您可以使用 FormBorderStyle 使其消失。

它确实涉及,但如果您愿意,可以创建自己的 Windows 表单库,即根本不使用 Windows 表单项目。使用 Windows GDI 文档和 PInvoke.Net 调用 GDI API 函数来创建 windows 等并设计您自己的表单系统。

您要查看的特定部分是 Window 函数:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx

这里是一个表单的例子,它自己绘制边框,可以调整大小和移动..:[=​​12=]

public partial class BorderForm : Form
{
    public BorderForm()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        BorderColor = Color.DarkSlateGray;
    }


    private const int hWidth = 12;        // resize handle width
    private const int bWidth = 28;        // border width
    public Color BorderColor { get; set;  }

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    protected override void OnPaint(PaintEventArgs e)
    {
        // draw the border..
        using (Pen pen = new Pen(BorderColor, bWidth)
             { Alignment = System.Drawing.Drawing2D.PenAlignment.Inset})
            e.Graphics.DrawRectangle(pen, ClientRectangle);
        // now maybe draw a title text..

        base.OnPaint(e);
    }


    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }


    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84) // Trap WM_NCHITTEST
        {  
            Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
            pos = PointToClient(pos);

            bool isTop = pos.Y <= hWidth;
            bool isBottom = pos.Y >= ClientSize.Height - hWidth;
            bool isRight = pos.X >= ClientSize.Width - hWidth;
            bool isLeft = pos.X <= hWidth;

            m.Result = (IntPtr)1;

            if (isTop) m.Result = 
                       isLeft ? (IntPtr)13 : isRight ? (IntPtr)14 : (IntPtr)12;
            else if (isBottom) m.Result = 
                 isLeft ? (IntPtr)16 : isRight ? (IntPtr)17 : (IntPtr)15;
            else if (isLeft) m.Result = (IntPtr)10;  
            else if (isRight) m.Result = (IntPtr)11;

            if ( m.Result != (IntPtr)1) return;
        }
        base.WndProc(ref m);
    }

}

WM_NCHITTEST 文档向您展示了如何模拟点击控件并在需要时调整框的大小。当然,你也应该以某种方式画它们!