1

Is there in c# any kind of window, form or anything that doesn't look like a usual form or dialog box and which can contain components like labels? Something like the black rectangle on this picture:

enter image description here

I also need that this "window" can be movable like a form when you click in the ControlBox but by holding down the mouse in any place of it. However, it (or at least its background) should have a property like transparency in some levels (high, medium, low transparency).

What should I use?

Thanks.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Drumnbass
  • 867
  • 1
  • 14
  • 35

2 Answers2

4

You can achieve this in a WinForms Form by setting the BackgroundImage property to an appropriate bitmap. E.g.:

enter image description here

Then, set the form’s FormBorderStyle to None and set the TransparencyKey to the color which has to be transparent in the bitmap (Color.Red in this example). That's it!

You will not be able to have partially transparent areas; however, you can set the overall Opacity (= 100% - transparency) of the form.

Note also, that, since you don't have a title bar any more (because of FormBorderStyle = None), you will not be able to close the form with the close button and to move the window by dragging the title bar. You will have to handle these things yourself by handling mouse or key events.

And of course, you can place any kind of controls on this form as usual.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
2

If you needed full, Form-level functionality, one way you could solve this would be to make a Form with FormBorderStyle = None. This would remove the entire border from the form, and it'll just appear as a flat rectangle with whatever background color you'd put on there.

There are a couple issues with this:

  1. The user won't be able to move it, unless you implement some sort of click handler to allow dragging it around the screen.
  2. The user will have to use the task bar (assuming you've left it visible there) or Alt-F4 to close the window.

Based on your question about how to solve #2, there's an SO page on How to drag a borderless Windows Form by mouse. In case it gets deleted anytime soon, I'll reproduce Joey's code (based on a linked article):

// DllImportAttribute requires reference to 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();

public Form1()
{
    InitializeComponent();
}

private void Form1_MouseDown_1(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

Note that I haven't done anything to test this other than just pasting it in and verifying that, yes, you can drag your Form with the mouse within its content area. Note also that this drag action will only work on empty areas of the Form - you won't be able to drag it if your mouse is over one of the Form's controls.

Edit: The title of the question was changed since it was originally posted - originally, the question was not substantially dedicated to how to create a non-rectangular form and more about how to create a borderless form - but the non-rectangular borders portion of the question as it stands now is solved by Olivier Jacot-Descombes below. I won't steal his contribution, but a combination of these two answers should give you exactly what you want.

Community
  • 1
  • 1
furkle
  • 5,019
  • 1
  • 15
  • 24
  • How could I implement the 2nd point? I'm stil a begginer (just a student). – Drumnbass Nov 01 '14 at 22:05
  • @Drumnbass Added some code that may work for you. If it helps you out, please don't forget to check this as your selected answer. – furkle Nov 01 '14 at 22:30
  • Being honest, I didn't understand almost anything of that code (why dll imports are needed, what are (or represent) those constants), but it absolutely worked. I really would like to understand it (I'm not a conformist at all), but I consider that you already did enough to help me so, only if you have time, the knowledge and are able to explain it to me by a simple way, I would appreciate it so much ;D – Drumnbass Nov 01 '14 at 22:45
  • @Drumnbass I'm not terrifically well-versed in .NET interoperabilty, but the Form1_MouseDown_1 code is essentially sending Windows the same sorts of messages that would ordinarily be sent by .NET when a window is moved - essentially just telling it "The Form's border is being dragged right now, move the Form with the mouse," even though it's not really the border that's being dragged. Anything more technical/specific than that is beyond me. – furkle Nov 01 '14 at 22:49
  • Aditional info: it seems that I can Still move the form if the mouse is over any control if I suscribe its (control) `MouseDown` event to the `Form1_MouseDown_1` method that you gave me ;D – Drumnbass Nov 02 '14 at 11:08