Winform implements the form hollow-out effect that can be penetrated by the mouse, and winform hollow-out

Source: Internet
Author: User

Winform implements the form hollow-out effect that can be penetrated by the mouse, and winform hollow-out

Today, I found a screen recording software called LICEcap. The recording interface is as follows:

This cool hollow window is the lens. Adjust the size and press Record at the desired location to generate a gif.

 

The slot is too NB. I also need to do one!

 

According to the StackOverflow site prompts (here), we need to use a hierarchical window API (setlayer=wattributes) that is available on the Windows2000 and later 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.

 

To call the Win32 API on the. NET platform, we need to review the content of P/Invoke:

  • 1. What is P/Invoke?

The full name of P/Invoke is Platform Invoke .. It is a call mechanism for exporting functions from an unmanaged dll on a managed platform.

  • 2. How to Use P/Invoke

The length is as follows:

[DllImportAttribute("user32.dll", EntryPoint="SetCursorPos")] public static extern  bool SetCursorPos(int X, int Y) ;

Specify the name of the called dll, export the function name, and define it as the C # standard method.

 

Therefore, we need to: Open Baidu encyclopedia, search for the API name, view the host dll, copy the function prototype, and define the required constant according to the instructions.

No, I found a more convenient method: Open pinvoke.net and search for the API Name:

Copy the Code according to the C # Signature and modify it according to the Sample Code.

Create a Winform project in Visual Studio and write it in the main window Code as follows:

    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); }

 

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

The second crKey is an int color value. The conversion method is (int) (0 xRRGGBB). In this example, Dec (0x00FF00) = 65280 is green.

The fourth parameter is the transparency mode. In this example, LWA_COLORKEY = 1 is used to set the parts of the window whose color is crKey to transparent.

Therefore, 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, effect:

 

One of the possible uses is to wrap several unrelated external programs into a whole.

Related Article

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.