首页新闻找找看学习计划

WinForm状态最小化问题

0
悬赏园豆:50 [已解决问题] 解决于 2012-08-09 13:32

为什么

在winform窗体全屏的时候,没有办法用LWin+M最小化到状态栏呢

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
switch (keyData)
{
case Keys.Escape:
CloseFullScreen();//esc关闭窗体
return true;
case Keys.F11:
CloseFullScreen();
return true;
case Keys.LWin | Keys.M:
this.WindowState = FormWindowState.Minimized;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

youdesou的主页 youdesou | 初学一级 | 园豆:173
提问于:2012-08-08 14:50
< >
分享
最佳答案
0
Keys.LWin & Keys.M
收获园豆:30
artwl | 专家六级 |园豆:16526 | 2012-08-08 15:02

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
switch (keyData)
{
case Keys.Escape:
CloseFullScreen();//esc关闭窗体
return true;
case Keys.F11:
CloseFullScreen();
return true;
case Keys.LWin & Keys.M:
this.WindowState = FormWindowState.Minimized;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

这个貌似也不行

youdesou | 园豆:173 (初学一级) | 2012-08-08 15:08

@Code Is Easy: 我这边测试着可以啊

artwl | 园豆:16526 (专家六级) | 2012-08-08 15:09

@artwl: 你是如何写的呢,主要这个窗体一个开始是无边框的状态

youdesou | 园豆:173 (初学一级) | 2012-08-08 15:19

@Code Is Easy: 我用的代码跟你的一样啊

artwl | 园豆:16526 (专家六级) | 2012-08-08 15:27

@artwl: 是在this.FormBorderStyle = FormBorderStyle.None

这种状态下去设置的吗

youdesou | 园豆:173 (初学一级) | 2012-08-08 15:39

@artwl: 你把你代码发给我看看

youdesou | 园豆:173 (初学一级) | 2012-08-08 16:01

@Code Is Easy: 我开始的代码没有用this.FormBorderStyle = FormBorderStyle.None,加上之后也不行了,我正在测试

artwl | 园豆:16526 (专家六级) | 2012-08-08 16:02

@artwl: 好的

希望可以成功,分就给你

youdesou | 园豆:173 (初学一级) | 2012-08-08 16:04

@artwl: 兄弟实现得如何呢

youdesou | 园豆:173 (初学一级) | 2012-08-08 16:46

钩子:

View Code
public class KeyboardHookLib
    {
        private const int WH_KEYBOARD_LL = 13; //键盘

        //键盘处理事件委托.
        private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);

        //客户端键盘处理事件
        public delegate void ProcessKeyHandle(HookStruct param, out bool handle);

        //接收SetWindowsHookEx返回值
        private static int _hHookValue = 0;

        //勾子程序处理事件
        private HookHandle _KeyBoardHookProcedure;

        //Hook结构
        [StructLayout(LayoutKind.Sequential)]
        public class HookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        //设置钩子
        [DllImport("user32.dll")]
        private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);

        //取消钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern bool UnhookWindowsHookEx(int idHook);

        //调用下一个钩子
        [DllImport("user32.dll")]
        private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        //获取当前线程ID
        [DllImport("kernel32.dll")]
        private static extern int GetCurrentThreadId();

        //Gets the main module for the associated process.
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string name);

        private IntPtr _hookWindowPtr = IntPtr.Zero;

        //构造器
        public KeyboardHookLib() { }

        //外部调用的键盘处理事件
        private static ProcessKeyHandle _clientMethod = null;

        /// &lt;summary&gt;
        /// 安装勾子
        /// &lt;/summary&gt;
        /// &lt;param name="hookProcess"&gt;外部调用的键盘处理事件&lt;/param&gt;
        public void InstallHook(ProcessKeyHandle clientMethod)
        {
            _clientMethod = clientMethod;

            // 安装键盘钩子
            if (_hHookValue == 0)
            {
                _KeyBoardHookProcedure = new HookHandle(GetHookProc);

                _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                _hHookValue = SetWindowsHookEx(
                    WH_KEYBOARD_LL,
                    _KeyBoardHookProcedure,
                    _hookWindowPtr,
                    0);

                //如果设置钩子失败.
                if (_hHookValue == 0)

                    UninstallHook();
            }
        }

        //取消钩子事件
        public void UninstallHook()
        {
            if (_hHookValue != 0)
            {
                bool ret = UnhookWindowsHookEx(_hHookValue);
                if (ret) _hHookValue = 0;
            }
        }

        //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。
        private static int GetHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode <= 0)
            {
                //转换结构
                HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));

                if (_clientMethod != null)
                {
                    bool handle = false;
                    //调用客户提供的事件处理程序。
                    _clientMethod(hookStruct, out handle);
                    if (handle) return 1; //1:表示拦截键盘,return 退出
                }
            }
            return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
        }
    }

窗体:

public static bool flag = false;
private KeyboardHookLib _keyboardHook = null;
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    this.FormBorderStyle = FormBorderStyle.None;
    _keyboardHook = new KeyboardHookLib();
    _keyboardHook.InstallHook(this.KeyPress);
}

public void KeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)
{
    handle = false;
    if (hookStruct.vkCode == 91)
    {
        flag = true;
        handle = true;
    }
    if (hookStruct.vkCode == (int)Keys.M)
    {
        if (flag)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}

可以运行,但我这边测试时会弹一个警告框,目前还不知道为什么

artwl | 园豆:16526 (专家六级) | 2012-08-08 17:20

@artwl: 用是可以用,不过好像在别的窗体下按LWIN+M不能用了

youdesou | 园豆:173 (初学一级) | 2012-08-08 17:25
其他回答(1)
0
收获园豆:20
悟行 | 园豆:12238 (专家六级) | 2012-08-08 17:23

这个能行吗

支持(0) 反对(0) youdesou | 园豆:173 (初学一级) | 2012-08-08 17:28

@Code Is Easy: 你试试就知道了。

支持(0) 反对(0) 悟行 | 园豆:12238 (专家六级) | 2012-08-08 17:29

@荒野的呼唤: 不行,达不到这个效果

支持(0) 反对(0) youdesou | 园豆:173 (初学一级) | 2012-08-08 17:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册