zoukankan      html  css  js  c++  java
  • C# 隐藏窗体

    在讲截图之前,先看看怎么隐藏窗体,在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

  • 相关阅读:
    漫谈iOS程序的证书和签名机制
    (转) Xcode 7 Bitcode
    iOS: How To Make AutoLayout Work On A ScrollView
    Objective C运行时(runtime)
    如何让iOS 保持界面流畅?这些技巧你知道吗
    iPhone 6 屏幕揭秘
    用HTML和CSS实现WWDC 2015上的动画效果
    桌球歷史:削球、快攻、弧圈球
    [WPF 自定义控件]自定义控件库系列文章
    UWP 自定义控件:了解模板化控件 系列文章
  • 原文地址:https://www.cnblogs.com/xiaolifeidao/p/2875323.html
Copyright © 2011-2022 走看看