在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在讲截图之前,先看看怎么隐藏窗体,在winform中,隐藏窗体也许很简单,是的,直接调用hide()方法就可以隐藏呢! 但是有时有这样的需求,比如你在窗体上布局了菜单,然后在隐藏窗体时你希望又可以调用,这时你就可以采用另外一种方式呢! private void Init() { SetStyle(ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer,true); TopMost = true; ShowInTaskbar = false; FormBorderStyle = FormBorderStyle.None; Bounds = Screen.GetBounds(this); BackgroundImage = GetDestopImage(); } private Image GetDestopImage() { Rectangle rect = Screen.GetBounds(this); Bitmap bmp = new Bitmap(rect.Width,rect.Height,PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); IntPtr gHdc = g.GetHdc(); IntPtr deskHandle = NativeMethods.GetDesktopWindow(); IntPtr dHdc = NativeMethods.GetDC(deskHandle); NativeMethods.BitBlt(gHdc,0,0,Width,Height,dHdc,0,0,NativeMethods.TernaryRasterOperations.SRCCOPY); NativeMethods.ReleaseDC(deskHandle,dHdc); g.ReleaseHdc(gHdc); return bmp; } 关键是这段: internal class NativeMethods { public const int WS_EX_TRANSPARENT = 0x00000020; [DllImport("user32.dll")] public static extern bool ClipCursor(ref RECT lpRect); [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool BitBlt( IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern IntPtr LoadLibrary(string lpFileName); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; public RECT(int left, int top, int right, int bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } public RECT(Rectangle rect) { Left = rect.Left; Top = rect.Top; Right = rect.Right; Bottom = rect.Bottom; } public Rectangle Rect { get { return new Rectangle( Left, Top, Right - Left, Bottom - Top); } } public Size Size { get { return new Size(Right - Left, Bottom - Top); } } public static RECT FromXYWH(int x, int y, int width, int height) { return new RECT(x, y, x + width, y + height); } public static RECT FromRectangle(Rectangle rect) { return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom); } } public enum TernaryRasterOperations { SRCCOPY = 0x00CC0020, /* dest = source*/ SRCPAINT = 0x00EE0086, /* dest = source OR dest*/ SRCAND = 0x008800C6, /* dest = source AND dest*/ SRCINVERT = 0x00660046, /* dest = source XOR dest*/ SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/ NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/ NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */ MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/ MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/ PATCOPY = 0x00F00021, /* dest = pattern*/ PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/ PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/ DSTINVERT = 0x00550009, /* dest = (NOT dest)*/ BLACKNESS = 0x00000042, /* dest = BLACK*/ WHITENESS = 0x00FF0062, /* dest = WHITE*/ } }
附件:在后面附上截图的功能的Demo:https://files.cnblogs.com/xiaolifeidao/WindowsFormsApplication2.rar |
请发表评论