zoukankan      html  css  js  c++  java
  • C# 通过消息捕获处理窗体最大化/最小化

    通过以下的一些代码可以实现捕获相关的一些消息事件;

    以及可以通过调用 SetCloseMenu();实现关闭一些按钮功能如屏蔽关闭按钮功能等;

    需要添加命名空间:using System.Runtime.InteropServices;

            private const int WM_SYSCOMMAND = 0x112;
            private const int MF_REMOVE = 0x1000;
            private const int SC_RESTORE = 0xF120;     //还原   
            private const int SC_MOVE = 0xF010;   //移动   
            private const int SC_SIZE = 0xF000;   //大小   
            private const int SC_MINIMIZE = 0xF020;   //最小化   
            private const int SC_MAXIMIZE = 0xF030;   //最大化   
            private const int SC_CLOSE = 0xF060;   //关闭     
            [DllImport("USER32.DLL")]
            public static extern int GetSystemMenu(int hwnd, int bRevert);
            [DllImport("USER32.DLL")]
            public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
         
             private void SetCloseMenu()
            {
                int hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);//移除关闭按钮
                RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);   
            }    
    
             /// <summary>
            /// 
            /// </summary>
            /// <param name="m"></param>
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_SYSCOMMAND:
                        switch (m.WParam.ToInt32())
                        {
                            case SC_MINIMIZE:
                                base.WndProc(ref m);
                                //捕获最小化消息
                                break;
                            case SC_RESTORE:
                                base.WndProc(ref m);
                                //捕获还原消息
                                break;
                            case SC_MAXIMIZE:
                                base.WndProc(ref m);
                                //捕获最大化消息
                                break;
                            default:
                                base.WndProc(ref m);
                                break;
                        }
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }
            }                
    View Code
  • 相关阅读:
    May 1 2017 Week 18 Monday
    April 30 2017 Week 18 Sunday
    April 29 2017 Week 17 Saturday
    April 28 2017 Week 17 Friday
    April 27 2017 Week 17 Thursday
    April 26 2017 Week 17 Wednesday
    【2017-07-04】Qt信号与槽深入理解之一:信号与槽的连接方式
    April 25 2017 Week 17 Tuesday
    April 24 2017 Week 17 Monday
    为什么丑陋的UI界面却能创造良好的用户体验?
  • 原文地址:https://www.cnblogs.com/mqxs/p/3354680.html
Copyright © 2011-2022 走看看