WinForm Achieve form cutout effect

Source: Internet
Author: User

Today, I found a screen recorder called Licecap, the recording interface is like this:

This cool, hollow window is the lens that adjusts the size and then presses the record to create the GIF if you want to record it.

I want to make one too NB!

Based on the tips of the StackOverflow station (here), we need to use a layered window API (Setlayerwindowattributes) that is available on the Windows2000 and after the platform to implement the irregular form. According to Baidu we first need to use a Win32 API called SetWindowLong to set the form as a layered form.

In order to be in. NET platform calls the Win32 API, we need to review the contents of P/invoke:

    • 1. What is P/invoke.

The full name of P/invoke is platform invoke, also known as platform invoke. is a calling mechanism that invokes an exported function in an unmanaged DLL under a managed platform.

    • 2. How to use P/invoke.

It looks like this:

[DllImportAttribute ("user32.dll", entrypoint="setcursorpos"  )]  publicstaticextern  bool setcursorpos (int int Y);

It is OK to specify the name of the called DLL, export the function name, and then define the method of the C # standard.

So, we need: open Baidu Encyclopedia, search API name, view the host DLL, copy the function prototype, follow the instructions to define the required constants.

No, lazy like us, usually find a more convenient way: Open Pinvoke.net, search API name:

  

According to the inside of the c#signature copied over, and then according to the code of the sample changed, OK.

Then create a new WinForm project in Visual Studio and write this in the main window code:

     Public Partial classForm1:form { PublicForm1 () {InitializeComponent ();  This. topmost =true; SetWindowLong ( This.            Handle, Gwl_exstyle, ws_ex_layered); SetLayeredWindowAttributes ( This. Handle,65280,255, Lwa_colorkey);
} Private Const UINTws_ex_layered =0x80000; Private Const intGwl_exstyle =- -; Private Const intLwa_colorkey =1; [DllImport ("User32", EntryPoint ="SetWindowLong")] Private Static extern UINTSetWindowLong (INTPTR hwnd,intNIndex,UINTDwnewlong); [DllImport ("User32", EntryPoint ="setlayeredwindowattributes")] Private Static extern intSetLayeredWindowAttributes (INTPTR hwnd,intCrkey,intBalpha,intDwFlags); }

Use SetWindowLong to define the window as a hierarchical form first. Then call the SetLayeredWindowAttributes method to set the transparency. Where the second parameter is Crkey a color value of type int, the conversion is (int) (0xRRGGBB), in this case dec (0x00FF00) = 65280 is green. The fourth parameter is transparent, in this case, Lwa_colorkey = 1, which means that the portion of the window with the color crkey is set to transparent. Accordingly, We need to draw a green block in the window designer. In this example, a PictureBox is used and the background color is set.

  

Then, the F5 runs, the effect:

  

One of the useful uses is to package several unrelated external programs as a whole.

WinForm Achieve form cutout effect

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.