如何为WinForm制作一个框架,C#?[英] How to make a frame for WinForm, C#?

本文是小编为大家收集整理的关于如何为WinForm制作一个框架,C#?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在考虑更改窗户形式的边框颜色,并发现它是由Windows决定的,好的,所以我看到以前问过这个问题的人被告知要去这里 http://customerborder.codeplex.com/目前看起来该站点目前不可用.那我必须做自己的框架吗?如果是这样,我要去哪里弄清楚这一点?我正在使用Visual Studio 2012.

推荐答案

这是一个绘制自己边框的表格的示例,可以调整并移动.

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);
    }

}

文档向您展示如何模拟击中控件并在需要的情况下调整框.当然,您也应该以某种方式将它们涂漆!

其他推荐答案

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

您可以使用 formBorderStyle 使它消失.

其他推荐答案

它确实涉及,但是如果您想创建自己的Windows表单库,那根本不使用Windows Form Project.使用Windows GDI文档和PinVoke.net来调用GDI API函数以创建Windows ETC并设计您自己的表单系统.

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

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

本文地址:https://www.itbaoku.cn/post/2091165.html

问题描述

I've been looking into changing the color of the border for the windows form and found out it's decided by windows, ok that makes sense, so then I see that people who have asked this question before are told to go here http://customerborderform.codeplex.com/ Looks like the site isn't usable at the moment. So would I have to make my own frame? If so where would I go to figure this, out? I'm using visual studio 2012.

推荐答案

Here is an example of a form that draws its own border, can be resized and moved..:

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);
    }

}

The WM_NCHITTEST docs show you how to simulate hitting the control and resize boxes if you need those. You should paint them somehow as well, of course!

其他推荐答案

I suppose you wants to make custom winform. In this case, you may wanna not render the default windows. Draw your desired winform in photoshop and use it as a background in your application. The issue with this approach is that you will need to design your own minimize, maximize and close buttons.

You may use the FormBorderStyle to make it disappear.

其他推荐答案

It's REALLY involved, but if you want you can create your own Windows Forms library, that is don't use the Windows Form project at all. Use the Windows GDI Documentation and PInvoke.Net to call into the GDI API functions for creating windows etc and design your own form system.

The specific section you want to look at is Window Functions:

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