• 判断是否有全屏程序正在运行(C#)


    注册一个AppBar(什么是AppBar?Using Application Desktop Toolbars ),通过SHAppBarMessage向系统注册AppBar,这样,当有程序全屏运行时系统会向我们的程序发送消息,在窗体WndProc中处理即可。

    声明要使用到的API和常量:

    view plaincopy to clipboardprint?
    public class APIWrapper  
        {  
            [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]  
            public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);  
            [DllImport("User32.dll", CharSet = CharSet.Auto)]  
            public static extern int RegisterWindowMessage(string msg);  
        }  
    [StructLayout(LayoutKind.Sequential)]  
    public struct RECT  
    {  
    public int left;  
    public int top;  
    public int right;  
    public int bottom;  
    }  
    [StructLayout(LayoutKind.Sequential)]  
        public struct APPBARDATA  
        {  
            public int cbSize;  
            public IntPtr hWnd;  
            public int uCallbackMessage;  
            public int uEdge;  
            public RECT rc;  
            public IntPtr lParam;  
        }  
    public enum ABMsg : int 
        {  
            ABM_NEW = 0,  
            ABM_REMOVE,  
            ABM_QUERYPOS,  
            ABM_SETPOS,  
            ABM_GETSTATE,  
            ABM_GETTASKBARPOS,  
            ABM_ACTIVATE,  
            ABM_GETAUTOHIDEBAR,  
            ABM_SETAUTOHIDEBAR,  
            ABM_WINDOWPOSCHANGED,  
            ABM_SETSTATE  
        }  
    public enum ABNotify : int 
        {  
            ABN_STATECHANGE = 0,  
            ABN_POSCHANGED,  
            ABN_FULLSCREENAPP,  
            ABN_WINDOWARRANGE  
        }  
     public enum ABEdge : int 
        {  
            ABE_LEFT = 0,  
            ABE_TOP,  
            ABE_RIGHT,  
            ABE_BOTTOM  
        } 
    public class APIWrapper
        {
            [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
            public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int RegisterWindowMessage(string msg);
        }
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public int left;
    public int top;
    public int right;
    public int bottom;
    }
    [StructLayout(LayoutKind.Sequential)]
        public struct APPBARDATA
        {
            public int cbSize;
            public IntPtr hWnd;
            public int uCallbackMessage;
            public int uEdge;
            public RECT rc;
            public IntPtr lParam;
        }
    public enum ABMsg : int
        {
            ABM_NEW = 0,
            ABM_REMOVE,
            ABM_QUERYPOS,
            ABM_SETPOS,
            ABM_GETSTATE,
            ABM_GETTASKBARPOS,
            ABM_ACTIVATE,
            ABM_GETAUTOHIDEBAR,
            ABM_SETAUTOHIDEBAR,
            ABM_WINDOWPOSCHANGED,
            ABM_SETSTATE
        }
    public enum ABNotify : int
        {
            ABN_STATECHANGE = 0,
            ABN_POSCHANGED,
            ABN_FULLSCREENAPP,
            ABN_WINDOWARRANGE
        }
     public enum ABEdge : int
        {
            ABE_LEFT = 0,
            ABE_TOP,
            ABE_RIGHT,
            ABE_BOTTOM
        }  

    在窗口Load事件中注册AppBar:

    view plaincopy to clipboardprint?
    private void RegisterAppBar(bool registered)  
            {  
                APPBARDATA abd = new APPBARDATA();  
                abd.cbSize = Marshal.SizeOf(abd);  
                abd.hWnd = this.Handle;  
                if(!registered)  
                {  
                    //register  
                    uCallBackMsg = APIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER");  
                    abd.uCallbackMessage = uCallBackMsg;  
                    uint ret = APIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);  
                }  
                else 
                {  
                    APIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);  
                }  
            }  
     private void Form1_Load(object sender, EventArgs e)  
            {  
                this.RegisterAppBar(false);  
            } 
    private void RegisterAppBar(bool registered)
            {
                APPBARDATA abd = new APPBARDATA();
                abd.cbSize = Marshal.SizeOf(abd);
                abd.hWnd = this.Handle;
                if(!registered)
                {
                    //register
                    uCallBackMsg = APIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER");
                    abd.uCallbackMessage = uCallBackMsg;
                    uint ret = APIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);
                }
                else
                {
                    APIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);
                }
            }
     private void Form1_Load(object sender, EventArgs e)
            {
                this.RegisterAppBar(false);
            }  

    重载窗口消息处理函数:

    view plaincopy to clipboardprint?
    protected override void WndProc(ref System.Windows.Forms.Message m)  
            {  
                if (m.Msg == uCallBackMsg)  
                {  
                    switch (m.WParam.ToInt32())  
                    {  
                        case (int)ABNotify.ABN_FULLSCREENAPP:  
                            {  
                                if ((int)m.LParam == 1)  
                                    this.RunningFullScreenApp = true;  
                                else 
                                    this.RunningFullScreenApp = false;  
                                break;  
                            }  
                        default:  
                            break;  
                    }  
                }  
                base.WndProc(ref m);  
            } 
    protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (m.Msg == uCallBackMsg)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case (int)ABNotify.ABN_FULLSCREENAPP:
                            {
                                if ((int)m.LParam == 1)
                                    this.RunningFullScreenApp = true;
                                else
                                    this.RunningFullScreenApp = false;
                                break;
                            }
                        default:
                            break;
                    }
                }
                base.WndProc(ref m);
            }

    在程序退出时,不要忘了Unregister我们注册的AppBar:

    view plaincopy to clipboardprint?
    this.RegisterAppBar(true); 
    this.RegisterAppBar(true);

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jingzhongrong/archive/2010/03/16/5385951.aspx

    作者:观海看云个人开发历程知识库 - 博客园
    出处:http://www.cnblogs.com/zhangtao/
    文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    HDU1069:Monkey and Banana(DP+贪心)
    hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)
    vb6.0 倒计时
    硬盘分区表
    踽踽独行的岁月,感谢与你的相遇
    每天学点Linux:二
    windows下Qt Creator5.1.0编写程序以及调用OpenCV库
    【每周一译】愚蠢的指标:Java中使用最多的关键字
    centos 7 没有ifconfig 命令
    About stats collected
  • 原文地址:https://www.cnblogs.com/zhangtao/p/1691906.html
Copyright © 2020-2023  润新知