Create non-rectangular shapes that can be moved by dragging and dropping to any area of ​​the background area

I want to move the form window by left-clicking and holding the background area, just as we usually use the title bar.

And then I also want to change the form window of my application (i.e. a rectangular window) into my graphic window with a customizable graphical interface. I have seen this type of window in many other applications, so I believe that this is possible.

+4
source share
2 answers

Question 1:

To allow the form to move when you drag its client area, you must tell the window manager to process the client area as if it were a title bar (title bar). You suggest something similar in your question.

This can be done in .NET by overriding the WndProc method of your form, responding to the WM_NCHITTEST message and returning HTCAPTION to indicate that everything should be considered as part of the header (header), not the default HTCLIENT , which indicates that it should be considered as an area of ​​the client form. Add the following code to the form class:

 private const int WM_NCHITTEST = 0x84; private const int HTCLIENT = 0x1; private const int HTCAPTION = 0x2; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_NCHITTEST) { // Convert HTCLIENT to HTCAPTION if (m.Result.ToInt32() == HTCLIENT) { m.Result = (IntPtr)HTCAPTION; } } } 

Question 2:

You can create a shape of any non-rectangular shape by setting the Region property of your shape to a custom Region of your choice. If you have experience working with graphics programs such as Photoshop, you might think of it as setting up a “clipping region” for your form: the window manager will not draw anything outside the boundaries you specify. Pixels in the form described in this Region can even be disjoint.

The easiest way to create your region is probably to use the GraphicsPath class , and then use the Region class , which takes a single GraphicsPath object as a parameter.

And as I assume you already know, given the first question, you need to set the FormBorderStyle property to None to make sure that the default borders created by the window manager disappear.

Unfortunately, these regions cannot be smoothed out. For more information on these restrictions, see Hans for this question .

Finally, it is worth noting that this last approach to creating non-rectangular shapes can lead to some completely ugly user interfaces that do not at all improve the usability of your product, for example:

Windows Media Player "alien head"

Use this technique sparingly and use common sense. When in doubt, rectangles are actually really good for windows.

+5
source

Here's a link to an impressive CodeProject article that will answer your questions: Alpha Channel Composited Windows Form with Designer Support . The implementation uses a combination of front and rear windows. It uses WS_EX_LAYERED to display a window with a bitmap and captures mouse events in the main form to allow it to be dragged around.

One thing that looks a bit knocked down is when the window is dragged - there is a slight delay between the movement of the front and rear windows, which makes them look like they are chasing each other.

The following CodeProject article shows how to overcome this problem using DeferWindowPos: Alpha Blended Windows Forms .

Good luck

+1
source

Source: https://habr.com/ru/post/1396958/


All Articles