usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingControlexs;namespacetransform{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); ////defined on the form, the cursor is displayed as a hand shape This. Cursor =System.Windows.Forms.Cursors.Hand; ////define the title name of the form This. Text ="Transparent WinForm Form! "; ////define the start display position of the form is the middle of the screen This. StartPosition =System.Windows.Forms.FormStartPosition.CenterScreen; ////The boundary of the form is FIXED3D type //This . FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; ////foreground color of the desktop as the foreground color of the form This. ForeColor =System.Drawing.SystemColors.Desktop; ////define font type, size This. Font =NewSystem.Drawing.Font ("Song Body",9); ////define the background color to be blue This. BackColor =System.Drawing.Color.White; //set the size of a form//This . ClientSize = new System.Drawing.Size (440, 170); //The Opacity property establishes the transparency of the form and is valid only for Windows 2000 This. Opacity =0.60; } Private voidForm1_Load (Objectsender, EventArgs e) { This. Setwindowtransparent ( -); } Private voidSetwindowtransparent (byteBalpha) { Try{Winapi.setwindowlong ( This. Handle, (int) WinAPI.WindowStyle.GWL_EXSTYLE, Winapi.getwindowlong ( This. Handle, (int) WinAPI.WindowStyle.GWL_EXSTYLE) | (UINT) WinAPI.ExWindowStyle.WS_EX_LAYERED); Winapi.setlayeredwindowattributes ( This. Handle,0, Balpha, Winapi.lwa_colorkey |Winapi.lwa_alpha); } Catch { } } protected Overridecreateparams CreateParams {Get{createparams cp=Base. CreateParams; Cp. Parent=Winapi.getdesktopwindow (); Cp. ExStyle=0x00000080|0x00000008;//Ws_ex_toolwindow | Ws_ex_topmost returnCP; } } }}
Public classWinAPI {[DllImport ("user32.dll")] Public extern StaticIntPtr GetDesktopWindow (); [DllImport ("user32.dll")] Public extern Static BOOLSetLayeredWindowAttributes (INTPTR hwnd,UINTCrkey,byteBalpha,UINTdwFlags); Public Static UINTLwa_colorkey =0x00000001; Public Static UINTLwa_alpha =0x00000002; [DllImport ("user32.dll")] Public extern Static UINTSetWindowLong (INTPTR hwnd,intNIndex,UINTDwnewlong); [DllImport ("user32.dll")] Public extern Static UINTGetWindowLong (INTPTR hwnd,intNIndex); Public enumWindowStyle:int{Gwl_exstyle =- - } Public enumExwindowstyle:UINT{ws_ex_layered =0x00080000 } }