ウィンドウメッセージを処理する方法

C#でもウィンドウメッセージを直接処理できます。
もうC++を使わなくてもいいですね。

プログラム

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NativeWindow {
  public partial class Form1: Form {
    const int WM_MOVE = 3;
    private WinHook hook;
    public Form1() {
      InitializeComponent();
    }
    private void Form1_Shown(object sender, EventArgs e) {
      hook = new WinHook();
      hook.AssignHandle(this.Handle);
    }
    private class WinHook: System.Windows.Forms.NativeWindow {
      protected override void WndProc(ref Message m) {
        switch (m.Msg) {
        case WM_MOVE:
          Console.WriteLine("ウィンドウが動いた");
          break;
        }
        base.WndProc(ref m);
      }
    }
  }
}

 

C#

Posted by @erestage