1

シンプルです。Linux os (ALT + ドラッグ) のように、ALT + MOUSE を押してウィンドウを移動したいです。

win32 api (move api) をクリックして興味のあるウィンドウに渡すことは可能ですか?

押されたキーをフックするWindowsサービスがあります(具体的にはALTボタン)。Altキーを押してマウスダウンイベントを確認したら、タイトルバーだけでなく、どこでもウィンドウクリックで移動したい!

現在、フォーム ウィンドウを次のように移動します。

using System.Runtime.InteropServices;

[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam );
[DllImportAttribute( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
public static extern bool ReleaseCapture();

private void Form1_MouseDown( object sender, MouseEventArgs e )
{
  ReleaseCapture();
  SendMessage( this.Handle, 0xa1, 0x2, 0 );
}

特定のウィンドウをクリックして SendMessage() を呼び出した後、特定のウィンドウのウィンドウハンドルを取得するにはどうすればよいですか?

それが可能だ?

4

2 に答える 2

1

これを行うには、Windows が送信する WM_NCHITTEST メッセージをトラップして、ウィンドウのどの領域がクリックされたかを確認します。HTCAPTION を返すことでだますことができ、ウィンドウのキャプションをクリックしたときに通常取得するマウス アクションを忠実に実行します。ウィンドウの移動を含む。このコードをフォームに貼り付けます。

    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        // Trap WM_NCHITTEST when the ALT key is down
        if (m.Msg == 0x84 && (Control.ModifierKeys == Keys.Alt)) {
            // Translate HTCLIENT to HTCAPTION
            if (m.Result == (IntPtr)1) m.Result = (IntPtr)2;
        }
    }
于 2010-06-23T13:54:18.893 に答える
0

私はこれを自分で解決し、自分の計算で面白いものを思いつき、どのウィンドウ(アクティブな前景ウィンドウ)でも完璧に機能しました。ちょっと長いですが、コメントに沿って従えば本当に理解しやすいです、それが役立つことを願っています:)それが機能する方法は、Ctrl + Alt + Mのような特定の登録済みキーコンボを押すと、マウスが中央に固定されることですアクティブなウィンドウの場合、マウスを動かすと、ウィンドウがそれに続き、同じコンボをもう一度押して放します。マウスをクリックする必要はありません。

public void MoveWindow_AfterMouse()
    {
      // 1- get a handle to the foreground window (or any window that you want to move).
      // 2- set the mouse pos to the window's center.
      // 3- let the window move with the mouse in a loop, such that:
      //    win(x) = mouse(x) - win(width)/2   
      //    win(y) = mouse(y) - win(height)/2
      // This is because the origin (point of rendering) of the window, is at its top-left corner and NOT its center!

      // 1- 
      IntPtr hWnd = WinAPIs.GetForegroundWindow();

      // 2- Then:
      // first we need to get the x, y to the center of the window.
      // to do this, we have to know the width/height of the window.
      // to do this, we could use GetWindowRect which will give us the coords of the bottom right and upper left corners of the window,
      // with some math, we could deduce the width/height of the window.
      // after we do that, we simply set the x, y coords of the mouse to that center.
      RECT wndRect = new RECT();
      WinAPIs.GetWindowRect(hWnd, out wndRect);
      int wndWidth = wndRect.right - wndRect.left;
      int wndHeight = wndRect.bottom - wndRect.top; // cuz the more you go down, the more y value increases.
      Point wndCenter = new Point(wndWidth / 2, wndHeight / 2); // this is the center of the window relative to itself.
      WinAPIs.ClientToScreen(hWnd, out wndCenter); // this will make its center relative to the screen coords.
      WinAPIs.SetCursorPos(wndCenter.X, wndCenter.Y);

      // 3- Moving :)))
      while (true)
      {
        Point cursorPos = new Point();
        WinAPIs.GetCursorPos(out cursorPos);
        int xOffset = cursorPos.X - wndWidth / 2;
        int yOffset = cursorPos.Y - wndHeight / 2;
        WinAPIs.MoveWindow(hWnd, xOffset, yOffset, wndWidth, wndHeight, true);
        Thread.Sleep(25);
      }
    }

そして今:

int moveCommandToggle = 0;
protected override void WndProc(ref Message m)
{
   if (m.Msg == 0x0312)
   {
     int keyID = m.WParam.ToInt32();
     if(keyID == some_key_combo_you_registered_for_the_moving)
     {
         if (moveCommandToggle++ % 2 == 0)
         {
            mover = new Thread(() => MoveWindow_AfterMouse());
            mover.Start();
         }
         else mover.Abort();
      }
    }
 }

RECTについて疑問がある場合:

  public struct RECT
  {
    public int left;    // xCoor of upper left corner.
    public int top;     // yCoor of upper left corner.
    public int right;   // xCoor of lower right corner.
    public int bottom;  // yCoor of lower right corner.
  };

WinAPIは、DllImportsを実行した静的クラスにすぎませんでした。

于 2013-01-30T03:03:29.487 に答える