Adding Buttons To Side Of Window On Windows Form

前端 未结 2 948
一生所求
一生所求 2021-01-29 11:07

I would like to add buttons to side of my windows form on C# (in outside). The buttons should move together as soon as when the window was moved.

For e

相关标签:
2条回答
  • 2021-01-29 11:32

    I see two options:

    • Either put the buttons in a separate form and make both forms stick together by coding the Move and maybe Resize events.

    • Or, simpler, make the Form transparent and remove Border and Title area. I would go for this option.

    Here you go:

    First you style the Form by:

    • Setting this.ControlBox = false;
    • Setting this.MaximizeBox = false;
    • Setting this.MinimizeBox = false;
    • Setting this.Text= "";
    • Setting this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    • Setting this.BackColor = System.Drawing.Color.Fuchsia;
    • Setting this.TransparencyKey = this.BackColor;

    Now add to the Form

    • a Panel that fills the right, main portion of the Form
    • the Button you want attached
    • a Label (label1) inside the main Panel, filling the top and holding the form's title text
    • a Tab control etc..

    Finally we want to add code to make the form moveable:

    using System.Runtime.InteropServices;
    ..
    
    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();
    
    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }
    

    You can look up code to make the window sizeable, too..

    You can look up code to give the button a non-rectangular shape using a region. Note that you need to avoid anti-aliased pixels here or else the Fuchsia will shine through.

    0 讨论(0)
  • 2021-01-29 11:40

    I believe the anchor property on the button is what you are looking for. Anchor behaves on a control by making the control follow the edge it is anchored to. For example, if you anchor to the bottom and you make your window bigger by dragging it from the bottom, the control will move down your form. You are able to anchor to multiple edges as well. Dock could also be used, which would cause your buttons to expand in size but not necessarily move around.

    See this post for anchor vs dock.

    0 讨论(0)
提交回复
热议问题