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
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:
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text= "";
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Fuchsia;
this.TransparencyKey = this.BackColor;
Now add to the Form
Panel
that fills the right, main portion of the Form
Button
you want attachedLabel
(label1
) inside the main Panel
, filling the top and holding the form's title textTab
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.
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.