WinForm realization of the effect of the mouse can penetrate the form _c# tutorial

Source: Internet
Author: User

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

The cool, hollow window is the lens, resize it, and then press the record where you want it to be, to generate the GIF.

The groove is too NB, I also want to make one!

Based on the StackOverflow station tip (here), we need to use a layered window API (Setlayerwindowattributes) that is available on the Windows2000 and beyond platforms to implement irregular forms. According to Baidu we first need to use a Win32 API named SetWindowLong to set the form as a hierarchical form.

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

1. What is P/invoke

P/invoke's full name is platform invoke. is an invocation mechanism that uses an unmanaged DLL to export functions under a managed platform.

2. How to use P/invoke

It grows like this:

[DllImportAttribute ("User32.dll", entrypoint= "Setcursorpos")]
public static extern bool Setcursorpos (int X, int Y);

In turn, indicate the name of the DLL that was invoked, export the function name, and then define the C # standard method.

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

No, I found a more convenient way: Open Pinvoke.net, search API name:

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

Then create a new WinForm project in Visual Studio, which is written in the main window code:

public partial class Form1:form
{public
Form1 ()
{
InitializeComponent ();
This. topmost = true;
SetWindowLong (this. Handle, Gwl_exstyle, ws_ex_layered);
SetLayeredWindowAttributes (this. Handle, 65280, 255, lwa_colorkey);
Private Const UINT ws_ex_layered = 0x80000;
Private Const int gwl_exstyle = -20;
Private Const int lwa_colorkey = 1;
[DllImport ("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong (IntPtr hwnd,int nindex,uint dwnewlong);
[DllImport ("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int setlayeredwindowattributes (IntPtr hwnd,int crkey,int balpha,int dwflags);

First use SetWindowLong to define the window as a hierarchical form, and then call the SetLayeredWindowAttributes method to set transparency.

The second parameter, Crkey, is the color value of an int, which is converted to (int) (0xRRGGBB), and in this case the DEC (0x00ff00) =65280 is green.

The fourth parameter is transparent, and Lwa_colorkey = 1 is used in this example, which means that the portion of the window color to Crkey is set to transparent.

So, accordingly, we need to draw a green square in the window designer. In this example, a PictureBox is used and the background color is set.

F5 run, the effect is as shown:

One use to think of is to package several unrelated external programs as a whole.

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.