2017-11-22 14 views
0

저는이 문제를 오랫동안 파악하려고 노력해 왔습니다. 나는 이것을 여러 번 수색했고, 내가 생각할 수있는 것보다 더 많은 기사와 질문을 읽었으며 정확히 무엇이 잘못 될지 알 수는 없다. 이것은 응용 프로그램에서 사용할 단축키 생성을 테스트하기 위해 컴파일하려고 시도한 작은 프로그램입니다. 다음과 같이왜 "보호 된 무시 무시한 WndProc (참조 메시지 m)"작동하지 않습니다

내가 알아 내기 위해 노력했습니다 시험의 소스는 다음과 같습니다

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace Prg 
{ 
    class MainClass 
    { 
     [DllImport("User32.dll")] 
     private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
     fsModifiers, int vk); 
     [DllImport("User32.dll")] 
     private static extern int UnregisterHotKey(IntPtr hWnd, int id); 

     public static Form f1 = new Form(); 

     public static int Register(Form f) 
     { 
      IntPtr ip = f.Handle; 
      return RegisterHotKey(ip, 1, 0, (int)Keys.Escape); 
     } 

     public static void b1_click(object sender, EventArgs e) 
     { 
      //Blah Blah stuff 
     } 
     protected override void WndProc(ref Message m) 
     { 
      if (m.Msg == 0x0312) 
      { 
       MessageBox.Show("wow"); 
      } 
      base.WndProc(ref m); 
     } 
     public static void Main() 
     { 
      Button b1 = new Button(); 
      b1.Location = new Point(10, 10); 
      b1.Text = "wow"; 
      b1.Click += new EventHandler(b1_click); 
      f1.Width = 200; 
      f1.Height = 200; 
      f1.Controls.Add(b1); 
      f1.ShowDialog(); 
      Register(f1); 
     } 
    } 
} 

내가에는 Csc.exe는 C# 4.0을 사용하여 컴파일하고있다. 이 코드를 컴파일하거나 컴파일 할 때마다이 오류가 계속 발생합니다.

Main.csx (37,27) : 오류 CS0115 : 'Prg.MainClass.WndProc (System.Windows.Forms.Message) ': 덮어 쓸 적절한 메소드가 없습니다.

단축키를 등록하기 위해 User32.dll을 사용하는 모든 예는 그 안에 "protected override WndProc"메소드를 가지고 있으며, 모든 사람들이 그것을 위해 잘 작동한다고 말했지만, 왜 그것이 내 삶을 위해 작동하지 않을지도 모르겠다. 누군가가 내가이 문제를 해결할 수 있다면 큰 도움이 될 것이다. I는 Windows 7 Professional 64 비트를 사용하고, 그리고에는 Csc.exe의 경로는 C : \ WINDOWS를 \ Microsoft.NET 프레임 워크 \ \ v4.0.30319 \에는 Csc.exe

감사합니다 :)


내가 지금 컴파일 왔


편집, 그러나 문제는 지금은 등록 및 단축키의 종류 또는 적어도 모든 키 입력을 따기없는 것 같습니다하지 않습니다. 내 코드에 오류가 있습니까?

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace Prg 
{ 
    class MainClass : Form 
    { 
     [DllImport("User32.dll")] 
     private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
     fsModifiers, int vk); 
     [DllImport("User32.dll")] 
     private static extern int UnregisterHotKey(IntPtr hWnd, int id); 

     public static Form f1 = new Form(); 

     public static int Register(Form f) 
     { 
      IntPtr ip = f.Handle; 
      return RegisterHotKey(ip, 1, 0, (int)Keys.Escape); 
     } 

     public static void b1_click(object sender, EventArgs e) 
     { 
      //Blah Blah stuff 
     } 
     protected override void WndProc(ref Message m) 
     { 
      if (m.Msg == 0x0312) 
      { 
       MessageBox.Show("wow"); 
      } 
      base.WndProc(ref m); 
     } 
     public static void Main() 
     { 
      Button b1 = new Button(); 
      b1.Location = new Point(10, 10); 
      b1.Text = "wow"; 
      b1.Click += new EventHandler(b1_click); 
      f1.Width = 200; 
      f1.Height = 200; 
      f1.Controls.Add(b1); 
      f1.ShowDialog(); 
      Register(f1); 
     } 
    } 
} 

여러 가지 해결책을 시도했지만 전혀 작동하지 않는 핫키를 얻지는 못했습니다. 나는 Application.Run (new MainClass())를 사용하기 위해 소스를 다시 작성하려고 시도했다. 그러나 형태가 집중 되더라도 Keypress를 아직도 검출하지 않았다. 내가 코드 내 실수를 해결 도움에 대한 문제가 컴파일에 도착 도와 zzxyz 덕분에 해결



편집 및 앙투안. 고마워. 이 코드는 동일한 문제가 있었거나 예제로 배우기를 좋아하는 사람을 위해 컴파일되고 작동하는 코드입니다. 다시 한번 감사드립니다.

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace Prg 
{ 
    class MainClass : Form 
    { 
     [DllImport("User32.dll")] 
     private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
     fsModifiers, int vk); 
     [DllImport("User32.dll")] 
     private static extern int UnregisterHotKey(IntPtr hWnd, int id); 

     public static MainClass f1 = new MainClass(); 

     public static int Register(Form f) 
     { 
      IntPtr ip = f.Handle; 
      return RegisterHotKey(ip, 1, 0, (int)Keys.Escape); 
     } 

     public static void b1_click(object sender, EventArgs e) 
     { 
      //Blah Blah stuff 
     } 
     protected override void WndProc(ref Message m) 
     { 
      if (m.Msg == 0x0312) 
      { 
       MessageBox.Show("wow"); 
      } 
      base.WndProc(ref m); 
     } 
     public static void Main() 
     { 
      Button b1 = new Button(); 
      b1.Location = new Point(10, 10); 
      b1.Text = "wow"; 
      b1.Click += new EventHandler(b1_click); 
      f1.Width = 200; 
      f1.Height = 200; 
      f1.Controls.Add(b1); 
      Register(f1); 
      f1.ShowDialog(); 
     } 
    } 
} 
+0

클래스는 '객체'이외의 것으로부터 파생되지 않습니다. 거기에 'WndProc'이 없습니다. –

+1

WndProc은'Form'에서 선언되었으므로이를 오버라이드하려면 클래스에서 파생되어야합니다. Form 파생 클래스는 일반적으로 버튼 등을 추가 할 곳이기도합니다. – zzxyz

답변

2

2 실수 :

  • 당신이 당신의 F1을 표시하기 전에 단축키를 등록해야합니다. 마지막 두 줄을 교환하십시오.

  • 현재 WndProc은 모든 양식이 아닌 MainClass에 우선합니다. 따라서 양식 f1은 재정의 된 Form.WndProc이 아닌 기본 Form.WndProc을 상속합니다. 그러면 f1을 MainClass로 선언하면됩니다.

+0

안녕하세요. 요약 해 주셔서 감사합니다. 문제를 해결했을뿐만 아니라 두 가지 간단한 문장으로 상당히 혼란 스럽습니다. 감사 :) –