-1

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 example :

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
Burak
  • 3
  • 2
  • Nothing can sit outside of the window. I have no idea what you actually want. Use the `Anchor` property to set the relation of controls like Buttons on the Form to the Form borders! – TaW Mar 12 '17 at 19:04
  • What you want to do is doable in WPF. In WinForms you need to override the window function. That requires knowledge about native Windows development which goes beyond the scope of SO. – Sefe Mar 12 '17 at 19:09
  • The buttons should be outside of the window. I've added better picture to explain what I want. – Burak Mar 12 '17 at 19:13
  • This is not possible but you can have smaller extra forms and keep them docked along the main form. Or you can try to make the left part except the tab transparent. – TaW Mar 12 '17 at 22:17
  • In the picture, the button is not outside of window. We can assume that the window background is transparent and it is a container with a button on upper-left side( both sitting on a transparent form). – Efe Mar 12 '17 at 22:23
  • Hence, by moving the container, window moves itself and all controls are moving together. This behavior can be achieved by mouse events. – Efe Mar 12 '17 at 22:30
  • Efe could you give me an example here or do I have to ask new question for this? – Burak Mar 13 '17 at 16:32

2 Answers2

1

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:

enter image description here

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Your solving works great but anti aliasing edges of images or texts have pink sections on their around. How can we fix this? Image: http://i.hizliresim.com/V0Yq3B.png – Burak Mar 19 '17 at 17:47
  • The button __must not have any__ anti-aliased pixels, __no way__ around this limitation. (Any controls within may have them, as long as they are not __semi-transparent__.) - If both buttons were of the same or similar colors you could make the effect lass visible by choosing a similar BackColor but green and black are way too different. – TaW Mar 19 '17 at 18:15
-1

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.

Community
  • 1
  • 1
ninja coder
  • 1,067
  • 9
  • 22
  • The buttons should be outside of the window. I've added better picture to explain what I want. – Burak Mar 12 '17 at 19:13