求用快捷键实现最大最小化窗体到托盘.
网上有直接加的,那种方式可以实现.可是利用快捷键怎么也不实现不了.现在我写了一部分.
只是实现了按了F12后最小化了,可是再按时就出错了.
请高手指教. 为了不影响大家的思路,先不贴我写的代码.

解决方案 »

  1.   

    这个问题其实分成两个:
    1.注册/删除热键问题
    2.最大最小化窗体到托盘.第2个问题不说了,太简单了,我觉得你的问题可能是注册热键.帖一段我写的代码给你看看: /// <summary>
    /// 自定义控制键枚举
    /// </summary>
    [Flags()]
    public enum KeyModifiers
    {  
    None = 11,
    Alt = 12,    
    Control = 13,    
    Shift = 14,    
    Windows = 15
    }

    //为窗体注册热键
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool RegisterHotKey(int hWnd,int id,KeyModifiers fsModifiers,Keys vk); 
      
    //为窗体注销热键
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool UnregisterHotKey(int hWnd,int id);
    public Keys m_hotkey;
    public bool m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey; public HotKeys()
    {
    } public HotKeys(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
    {
    this.m_hotkey = c;
    this.m_althotkey = bAlt;
    this.m_ctrlhotkey = bCtrl;
    this.m_shifthotkey = bShift;
    this.m_winhotkey = bWindows;
    }
      

  2.   

    None = 11,
    Alt = 12,    
    Control = 13,    
    Shift = 14,    
    Windows = 15
    你这部分是随便写的值吗?
      

  3.   

    我给你个方法
    System.Timers.Timer tim;
    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError=true)]
    public static extern short GetAsyncKeyState(int id); 
    private void Form1_Load(object sender, System.EventArgs e)
    {
             tim=new System.Timers.Timer(1);
    tim.Elapsed+=new System.Timers.ElapsedEventHandler(tim_Elapsed);
    tim.Start();
    }
    private void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    short s=GetAsyncKeyState(123);//123 就是F12
    if(s<0)
    {
    this.Visible=false;//这里写你要干什么,要是还有问题,字条我
    }
    }
      

  4.   

    用相似的方法可以实现两个热键的打开与缩小,那么单键的呢?RegisterHotKey方法没有三个参数的重载吗?
      

  5.   

    单键还不是一样?
    if(s<0)
    {
    //this.Visible=false;//这里写你要干什么,要是还有问题,字条我
    if(iStat==0)
    {
    隐藏;
    iStat=1;
    }
    else
    {
    打开;
    iStat=0;
    }
    }
      

  6.   

    回复人:jing_xin(我的程序人生) ( 二级(初级)) 信誉:98  2007-03-09 17:49:38  得分:0

    F12不行.其它都行.不知道为什么?======================================2种可能,你的f12坏掉了,你的F12被注册过了,关掉所有程序,跑一次.注意关掉VS
      

  7.   

    光一个F12不能注册为全局HotKey
    如下是Ctrl+F12public const uint MOD_ALT = 1;
    public const uint MOD_CONTROL = 2;
    public const uint MOD_SHIFT = 4;
    public const uint MOD_WIN = 8;
    private const int F12IDENT = 0x0010;
    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, 
        uint fsModifiers, int vk);[DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);private void Form1_Load(object sender, EventArgs e)
    {
        RegisterHotKey(Handle, F12IDENT, MOD_CONTROL, (int)Keys.F12);
    }private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        UnregisterHotKey(Handle, F12IDENT);
    }
    public const int WM_HOTKEY = 0x0312;
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        switch (m.Msg)
        {
            case WM_HOTKEY:
                if (m.WParam == (IntPtr)F12IDENT)
                    MessageBox.Show("Zswang 路过");
                break;
        }
    }