. NET irregular, changeable size, and border translucent Form Design

Source: Internet
Author: User

I have been developing. Net winform for some time and have some knowledge of. net interface design. I think that all these things I learned are summed up after reading the experts online. I wrote these things for the first time.

Please give us some advice on some bad things.

Let's take a look at the results!

 

I have also seen a lot of examples of. Net forms on the Internet. I just integrated these online things, mainly including the following features:

1. The border is translucent and the transparency can be changed as needed;

2. You can change the size of the form, and the style remains unchanged;

3. The border of the form is irregular;

4. solved the problem that the form will flash and will not flash when the form moves;

5. Easy to use. You only need to drag alphaformpanel to a normal form to realize skin replacement;

 

Design ideas:

I. There are two methods for processing partial transparency of a form in. Net:

1. Use an image that supports the alhpa channel to process translucent data. This method works well and even uses a dynamic image as the background.

I believe someone has seen the program of the swimming fish. The edge of the fish is translucent. It is done in this way. In this way, the entire form is drawn through updatelayeredwindow.

If you want to add controls on them, all controls must be drawn by yourself. Obviously, this method will greatly increase development in a specific project.

Difficulty. If you are interested, you can look at this program:

/Files/liutao409/dynamic fish .rar

The key code is to draw a form based on the image that supports the Alhpa channel.

[DllImport ("user32.dll", ExactSpelling = true, SetLastError = true)]
Public static extern int UpdateLayeredWindow (IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize,

IntPtr hdcSrc, ref Point pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags );

Public void SetBits (Bitmap bitmap)
{
If (! HaveHandle) return;

If (! Bitmap. IsCanonicalPixelFormat (bitmap. PixelFormat) |! Bitmap. IsAlphaPixelFormat (bitmap. PixelFormat ))
Throw new ApplicationException ("the image must be a 32-bit image with Alhpa channel. ");

IntPtr oldBits = IntPtr. Zero;
IntPtr screenDC = Win32.GetDC (IntPtr. Zero );
IntPtr hBitmap = IntPtr. Zero;
Intptr memdc = win32.createcompatibledc (screendc );

Try
{
Win32.point toploc = new win32.point (left, top );
Win32.size bitmapsize = new win32.size (bitmap. Width, bitmap. Height );
Win32.blendfunction blendfunc = new win32.blendfunction ();
Win32.point srcloc = new win32.point (0, 0 );

HBitmap = bitmap. GetHbitmap (Color. FromArgb (0 ));
OldBits = Win32.SelectObject (memDc, hBitmap );

BlendFunc. BlendOp = Win32.AC _ SRC_OVER;
BlendFunc. SourceConstantAlpha = 255;
BlendFunc. AlphaFormat = Win32.AC _ SRC_ALPHA;
BlendFunc. BlendFlags = 0;

Win32.UpdateLayeredWindow (Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW _ ALPHA );
}
Finally
{
If (hBitmap! = IntPtr. Zero)
{
Win32.SelectObject (memDc, oldBits );
Win32.DeleteObject (hBitmap );
}
Win32.ReleaseDC (IntPtr. Zero, screenDC );
Win32.DeleteDC (memDc );
}
}

 

2. Use two forms to make the border translucent

The following form is used as a border, and other controls are placed on the front form. The front form follows the following form to move and change the size. This makes it easy to control the following form.

Translucent display. This method is a little more troublesome, but it does not need to draw all the controls as in the first method, so it can still be used in the project. My

The example is implemented in this way.

Ii. Processing of the form border

The border of my form is processed with images. In order to keep the style of the border of the form unchanged after the size is changed, the upper and lower borders are made into three segments, and the middle part is tiled, both ENDS remain unchanged

In this way, the style will not change after the form is scaled. If you are familiar with GDI +, you do not need to use images for processing. You can also draw gradient fills directly. The PictureBox that carries the image I have rewritten,

Let PictureBox send all messages to the parent form for processing, so that the parent form can uniformly process the scaling and movement of the form. The specific code is as follows:

 

Public partial class PictureBoxEX: PictureBox
{
Public delegate void delSetFormSize (int intInfo );
Public event delsetformsize evtsetformsize;

# Region attributes
Private bool _ btopleft = false;
Public bool btopleft
{
Get {return _ btopleft ;}
Set {_ btopleft = value ;}
}

Private bool _ BTOP = false;
Public bool BTOP
{
Get {return _ bTop ;}
Set {_ bTop = value ;}
}

Private bool _ bTopRight = false;
Public bool BTopRight
{
Get {return _ bTopRight ;}
Set {_ bTopRight = value ;}
}

Private bool _ bLeft = false;
Public bool BLeft
{
Get {return _ bLeft ;}
Set {_ bLeft = value ;}
}

Private bool _ bBottomLeft = false;
Public bool BBottomLeft
{
Get {return _ bBottomLeft ;}
Set {_ bBottomLeft = value ;}
}

Private bool _ bBottom = false;
Public bool BBottom
{
Get {return _ bBottom ;}
Set {_ bBottom = value ;}
}

Private bool _ bRight = false;
Public bool BRight
{
Get {return _ bRight ;}
Set {_ bRight = value ;}
}

Private bool _ bBottomRight = false;
Public bool BBottomRight
{
Get {return _ bBottomRight ;}
Set {_ bBottomRight = value ;}
}
# Endregion

# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public PictureBoxEX ()
{
;
}
# Endregion

# Region re-mouse message
Const int wm_nchittest = 0x0084;
Const int httransparent =-1;
Const int htleft = 10;
Const int htright = 11;
Const int httop = 12;
Const int httopleft = 13;
Const int httopright = 14;
Const int HTBOTTOM = 15;
Const int HTBOTTOMLEFT = 0x10;
Const int HTBOTTOMRIGHT = 17;

Protected override void WndProc (ref Message m)
{
Base. WndProc (ref m );
Switch (m. Msg)
{
Case WM_NCHITTEST:
Point vPoint = new Point (int) m. LParam & 0 xFFFF,
(Int) m. LParam> 16 & 0 xFFFF );
VPoint = PointToClient (vPoint );
Int intInfo =-1;
If (_ bTopLeft)
{
If (vpoint. x <= 10)
{
Intinfo = httopleft;
}
}
Else if (_ bleft)
{
Intinfo = htleft;
}
Else if (_ bbottomleft)
{
Intinfo = htbottomleft;
}
Else if (_ bBottom)
{
IntInfo = HTBOTTOM;
}
Else if (_ bBottomRight)
{
Intinfo = htbottomright;
}
Else if (_ bright)
{
Intinfo = htright;
}
Else if (_ bTopRight)
{
If (vPoint. X> = ClientSize. Width-10)
{
IntInfo = HTTOPRIGHT;
}
}
Else if (_ BTOP)
{
If (vpoint. Y <= 5)
{
M. Result = (IntPtr) HTTOP;
IntInfo = HTTOP;
}
}

If (evtSetFormSize! = Null & intInfo! =-1)
{
Evtsetformsize (intinfo );
}
// Send the message to the parent form for processing
M. Result = (intptr) httransparent;
Break;
}
}
# Endregion
}

 

Key Code Description:

1. Used messages

Private const int wm_nclbuttondblclk = 0x00a3;
Private int _ intinfo =-1; // message return value
Private const int htleft = 10;
Private const int htright = 11;
Private const int httop = 12;
Private const int httopleft = 13;
Private const int httopright = 14;
Private const int HTBOTTOM = 15;
Private const int HTBOTTOMLEFT = 0x10;
Private const int HTBOTTOMRIGHT = 17;
Private const int WM_NCHITTEST = 0x84;
Private const int HTCLIENT = 0x01;
Private const int HTCAPTION = 0x02;

1. Change the form size and position

All the messages related to the border are uploaded to the form for processing. The PictureBoxEx carrying the border will ignore the system messages sent to it.

Protected override void WndProc (ref Message m)
{
If (M. MSG = wm_nchittest)
{
This. defwndproc (ref m );

// Move the form position
If (M. Result. toint32 () = htclient & this. windowstate! = Formwindowstate. maximized)
{
M. Result = new intptr (htcaption );
}
Else
{
Base. wndproc (ref m );
}
// Change the form size
If (changeformsize & _ intinfo! =-1 & this. windowstate! = Formwindowstate. maximized)
{
M. Result = (intptr) _ intinfo;
}
_ Intinfo =-1;
}
// Double-click a message with the left mouse button
Else if (M. MSG = wm_nclbuttondblclk)
{
If (ChangeFormSize)
{
// It is equivalent to clicking the maximize button once.
BtnMax_MouseClick (null, EventArgs. Empty );
}
}
Else
{
Base. wndproc (ref m );
}
}

2. Reduce form flashes

The following are common methods to reduce form flashes.

Private void setstyles ()
{
Setstyle (
Controlstyles. userpaint |
Controlstyles. allpaintinginwmpaint |
ControlStyles. OptimizedDoubleBuffer |
ControlStyles. ResizeRedraw |
ControlStyles. SupportsTransparentBackColor, true );
SetStyle (ControlStyles. Selectable, false );
UpdateStyles ();
}

You can also set the form Double Cache to reduce flickering

This. DoubleBuffered = true;

In the process of creating a form control, if there are too many controls, you can use the following function to process the creation of the control to reduce flickering

Public class avoidcontrolflicker
{
Private int _ paintfrozen;
Public void freezepainting (control tofreezecontrol, bool istofreeze)
{
If (null = tofreezecontrol)
Throw new argumentnullexception ("tofreezecontrol ");

If (istofreeze & tofreezecontrol. ishandlecreated & tofreezecontrol. Visible)
{
If (0 = _ paintfrozen ++)
{
Nativemethods. sendmessage (tofreezecontrol. Handle, nativeconsts. wm_setredraw, 0, 0 );
}
}
If (! Istofreeze)
{
If (0 = _ paintfrozen) return;
If (0 = -- _ paintFrozen)
{
NativeMethods. SendMessage (toFreezeControl. Handle, NativeConsts. WM_SETREDRAW, 1, 0 );
ToFreezeControl. Invalidate (true );
}
}
}
}

3. To help beginners, other processing functions are also written.

/// <Summary>
/// Changes the form position
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void formstyle_locationchanged (Object sender, eventargs E)
{
If (pform! = NULL)
{

// Change the position of the subform at the same time
CForm. Location = new Point (this. Location. X + pbLeft. Width, this. Location. Y + panelTop. Height );
CForm. Update ();
}
}

 

/// <Summary>
/// Close button click event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>

Private void btnExit_MouseClick (object sender, EventArgs e)
{
This. Close ();
}

 

// When closing the form, you must close the parent form of the form at the same time.

Protected override void OnClosing (CancelEventArgs e)
{
E. Cancel = true;
Base. OnClosing (e );
Owner. Close ();
}

 

/// <Summary>
/// Click the event in the maximize button
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnMax_MouseClick (object sender, EventArgs e)
{
If (this. WindowState = FormWindowState. Maximized)
{
This. WindowState = FormWindowState. Normal;
This. Opacity = FormOpacity;
}
Else
{
This. WindowState = FormWindowState. Maximized;
This. Opacity = 1;
}
}

 

/// <Summary>
/// Click the event button
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnMin_MouseClick (object sender, EventArgs e)
{
This. WindowState = FormWindowState. Minimized;
}

 

/// <Summary>
/// Change the form size
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void formStyle_SizeChanged (object sender, EventArgs e)
{
If (PForm! = Null)
{

// Change the size of the subform at the same time
CForm. Size = new Size (this. Size. Width-pbLeft. Width-pbRight. Width, this. Size. Height-panelTop. Height-panelBottom. Height );
}
}

 

Finally, we provide the following controls and sample code for you to download:

/Files/liutao409/border half transparent window .rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.