2012-01-18 102 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 
using System.Threading; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("user32.dll")] 
     public static extern 
      bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk); 
     [DllImport("user32")] 
     public static extern 
      bool GetMessage(ref Message lpMsg, IntPtr handle, uint mMsgFilterInMain, uint mMsgFilterMax); 

     public const int MOD_ALT = 0x0001; 
     public const int MOD_CONTROL = 0x0002; 
     public const int MOD_SHIFT = 0x004; 
     public const int MOD_NOREPEAT = 0x400; 
     public const int WM_HOTKEY = 0x312; 
     public const int DSIX = 0x36; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      bool r = RegisterHotKey(Handle, 1, MOD_ALT, DSIX); 

      if (!r) 
      { 
       MessageBox.Show("can't do.."); 
       return; 
      } 

      Message msg = new Message(); 

      while (GetMessage(ref msg,IntPtr.Zero, 0, 0)) 
      { 
       if (msg.message == WM_HOTKEY) 
       { 
        MessageBox.Show("do work.."); 
       } 
      } 


     } 
    } 


    public class Message 
    { 
     public int message { get; set; } 
    } 
} 

按下目标的时候,我得到以下错误:的GetMessage()给出一个System.ExecutionEngineException

Exception of type 'System.ExecutionEngineException' was thrown. 

什么是什么?如何解决这个问题?提前致谢。

+0

您的消息类没有足够的成员,所以GetMessage()会破坏垃圾回收堆。改写WndProc()。搜索“setvisiblecore”以了解如何保持窗口不可见。 – 2012-01-18 16:52:46

回答

3

这注定会失败。如果您编写自己的消息循环,它将停止WinForms接收任何消息。

改为覆盖PreProcessMessageWndProc函数。

相关问题