在桌面上如何正确绘制Bug矩形

时间:2012-01-19 12:47:25

标签: c# system.drawing

我想在桌面上捕获一个绘制矩形的桌面区域。

为了达到这个目的,我开发了一个名为MouseHooker的类,它安装了一个全局鼠标钩子,处理鼠标消息并通过委托回调公开它。

钩子代码没问题,我认为外面的“回调”也有一个奇怪的错误。

它勾住鼠标,但只有在鼠标左键移动后才会绘制矩形,鼠标移动。

我不知道为什么。你能帮助我吗 ? 感谢

MouseHooker内部回调

private static IntPtr MyCallbackFunction(int code, UIntPtr wParam, IntPtr lParam)
    {
        // messsage not processed
        if ( code < 0 )
            SafeNativeMethods.CallNextHookEx(IntPtr.Zero,code,wParam,lParam);

        // check outside callback subscribed to delegate (NOT IMPORTANT )
        if ( state != HookState.CallBackError )
        {

            MSLLHOOKSTRUCT hk = (MSLLHOOKSTRUCT)Marshal.PtrToStructure((IntPtr)lParam, typeof(MSLLHOOKSTRUCT));
            MouseHookArgs m; // this struct only contains mouse coordinates and button
            m.x = hk.pt.x;   // clicked information
            m.y = hk.pt.y;

            // process mouse messages
            switch ((MouseMessages)wParam)
            {

              case MouseMessages.WM_MOUSEMOVE:
                  m.button = MouseButtons.M_MOVE;
                  mc(m); // call outside "callback" mc its a delegate
                break;          

                case MouseMessages.WM_LBUTTONUP:
                  m.button = MouseButtons.LB_UP;
                  mc(m);
                break;

                case MouseMessages.WM_LBUTTONDOWN:                     
                     m.button = MouseButtons.LB_DOWN;
                     mc(m);
                break;

                // more mouse message processing
         }
               // CallNextHookEx
         return SafeNativeMethods.CallNextHookEx(IntPtr.Zero,code,wParam,lParam);
}

表单中的“回调”(这里是错误,为什么?)

void callbackrect( MouseHookArgs args )
    {

        switch (args.button)
        {
            case ppk.Hooks.MouseButtons.LB_UP:

                clean = true;
                ok = true;

                dest.X = args.x; // this struct its a Point
                dest.Y = args.y;

                // put destination mouse coordinate in form label
                capt.Text = string.Format("X: {0} , Y: {1}", dest.X, dest.Y);

            break;

            case ppk.Hooks.MouseButtons.LB_DOWN:                

            if ( clean )
            {                    
                clean = false;
                ok = false;
            }
                origin.X = args.x; // get origin mouse coordinate (to draw rectangle )
                origin.Y = args.y;

                // put origin mouse coordinate in form label 
                rat.Text = string.Format("X: {0} , Y: {1}", origin.X, origin.Y);

            break;    

        }

        // Invalidate & Update Desktop RECT Unmanaged because it's a efficient
        SafeNativeMethods.InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
        SafeNativeMethods.UpdateWindow(IntPtr.Zero); 

        if (ok && clean)  // Graphics g it's static and created in another location.
                          // Managed because creating a Pen, get DC, etc it's a pain
                          // and need more unmanaged code. So, why not use graphics ?                                  
            g.DrawRectangle(Pens.Red, origin.X, origin.Y, dest.X - origin.X, dest.Y - origin.Y);                                                     
    }

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,你会说当绘制矩形时,它所绘制的位置偏离你点击的位置。

如果只是少量,则可能是舍入错误。 MSLHOOKSTRUCT将pt定义为一个点,它是两个浮点数的结构,而不是两个整数。 (More info

任何其他错误都与指针所处的位置(屏幕边缘)的差异以及绘制事件的位置差异(我们无法看到,因为您没有发布图形对象已设置)

希望这有帮助!