2010-07-29 6 views
0

마우스로 그래픽 객체를 움직이는 그래픽 응용 프로그램이 있습니다.마우스 정지 또는 이동

일부 조건에서는 개체가 움직이지 않습니다. 마우스 커서도 움직이지 않아야합니다.

가능합니까? MousePosition 속성이 ReadOnly에있는 것 같습니다.

예 :

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.X > 100) 
     { 
      Cursor.Position = new Point(100, Cursor.Position.Y); 
     } 
    } 
} 

편집, 두 번째 버전은 작동하지만, 커서는 "안정"아니다 - 깜박임 :

private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.X > 100) 
     { 
      Point mousePosition = this.PointToClient(Cursor.Position); 
      mousePosition.X = 100; 
      Point newScreenPosition = this.PointToScreen(mousePosition); 
      Cursor.Position = newScreenPosition; 
     } 
    } 
+0

'{0, 0, 100, Form.Height}' (분명히 클라이언트 좌표에서 화면 좌표로 변환). – GSerg

답변

3

ClipCursor 함수를 PInvoke를 통해 사용할 수 있습니다. 둥근 사각형이 충분히 작 으면 마우스가 움직이지 않습니다.


편집

예 : 당신은 사각형이다 ClipCursor, 단일 호출로이 코드를 대체 할 수

[StructLayout(LayoutKind.Sequential, Pack=1)] 
public struct RECT { 
    public int left; 
    public int top; 
    public int right; 
    public int bottom; 
}; 


public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    [DllImport("user32.dll")] 
    static extern bool ClipCursor([In()]ref RECT lpRect); 

    [DllImport("user32.dll")] 
    static extern bool ClipCursor([In()]IntPtr lpRect); 


    private bool locked = false; 

    private void button1_Click(object sender, EventArgs e) 
    { 

     if (locked) { 
      ClipCursor(IntPtr.Zero); 
     } 
     else { 
      RECT r; 

      Rectangle t = new Rectangle(0, 0, 100, this.ClientSize.Height); 
      t = this.RectangleToScreen(t); 

      r.left = t.Left; 
      r.top = t.Top; 
      r.bottom = t.Bottom; 
      r.right = t.Right; 

      ClipCursor(ref r); 
     } 

     locked = !locked; 

    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
} 
+0

.. 용도는 무엇입니까? 그물? – serhio

+0

"충분히 작습니까?" 그리고 그렇지 않다면? – serhio

+0

C# 및 vb .net 선언은이 문서의 맨 아래에 있습니다. – GSerg

2

당신이 Cursor.Position를 사용하여 시도 적이 있습니까?

예.

Cursor.Position = new Point(100, 100); 

(Vulcan의 말처럼) 일정한 값으로 설정할 수 있습니다.

+0

니스! 이 사실을 알지 못했습니다. –

+0

나는 OnMouseMove를 System.Windows.Forms.MouseEventArgs로 사용한다. 그리고 e.Location이 Cursor.Position과 동기화되지 않은 것처럼 보인다 ... 또한 커서를 사용할 때 "피드백"을 받는다. – serhio

+0

@serhio 이상하다. ... 동기화가 어때? – NickAldwin

관련 문제