Adding Buttons To Side Of Window On Windows Form

前端 未结 2 947
一生所求
一生所求 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.

提交回复
热议问题