2012-07-31 5 views
2

간단한 WPF 응용 프로그램을 작성 중입니다. 나는 투명하게 최대화WindowCanvas (canvas1) 있습니다.캔버스에 투명 배경으로 마우스 위치 가져 오기

마우스 위치canvas1 또는 MainWindow (이 경우는 동일한 것)에 가져오고 싶습니다.

Point p = Mouse.GetPosition(canvas1); //and then I have p.X and p.Y 

이 코드는 불투명Canvas을 위해 잘 작동 :

는이 일을 위해 나는이 코드를 사용합니다. 문제는 내가 투명Canvas,이 코드는 작동하지 않습니다 ... (나에게 오류를주지는 않지만 좌표는 p.X = 0p.Y = 0입니다)입니다.

이 문제를 어떻게 해결할 수 있습니까?

+0

당신이 응용 프로그램에있어 다른 물건을 가까이 할 수 있기 때문에 당신은 당신의 캔버스'Z-Index' 속성을 줄 수 – harry180

+2

Z-지수 = 어쩌면 당신은? 답을 줄 것입니다. 도움이된다면 +1하고 표시하겠습니다. –

+0

투명 배경 대신 불투명도를 사용하려고 시도 했습니까? – Alex

답변

2

한 가지 가능한 해결 방법은 GetCursorPos Win32 함수 사용하는 것입니다. 당신은 WPF에서 사용하려면 포인트 픽셀에서 좌표를 변환하는 당신이

[DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool GetCursorPos(out System.Drawing.Point lpPoint); 

사용 예 :.

System.Drawing.Point point; 
if(!GetCursorPos(out point)) 
    throw new InvalidOperationException("GetCursorPos failed"); 
// point contains cursor's position in screen coordinates. 
+0

@John,'System.Drawing.Point point; GetCursorPos (출력 포인트);'. 'point'는 마우스 코드를 포함 할 것입니다. System.Drawing.dll 참조를 추가하는 것을 잊지 마십시오. – Dmitry

+1

그냥 발언. 인수는 궁극적으로 'System.Drawing.Point'여야합니다. Win32에서는 [POINT] (http://msdn.microsoft.com/en-us/library/windows/desktop/dd162805 (v = vs.85) .aspx) 구조입니다. 당신은 두 개의 정수 멤버로 자신의 구조체를 정의 할 수도 있고 그것을 인수 타입으로 사용할 수도 있습니다. 따라서'System.Drawing.dll' 의존성을 제거 할 수 있습니다. – Clemens

+0

@ 존 그리고 질문. 이제이 기능을 어떻게 사용 하시겠습니까? 끝없는 루프에서 끊임없이 현재 마우스 위치를 업데이트합니까? 난 아직도 당신이 필요하다고 생각 [MouseHook] (http://stackoverflow.com/q/11607133/1136211). – Clemens

1

C#을

using System.Windows; 
    using System.Windows.Input; 
    using System.Diagnostics; 
    using System.Runtime.InteropServices; 
    using System; 
    using System.Windows.Threading; 
    namespace Test 
    { 
     public partial class MainWindow : Window 
     { 
      [DllImport("user32.dll")] 
      [return: MarshalAs(UnmanagedType.Bool)] 
      internal static extern bool GetCursorPos(ref Win32Point pt); 

      [StructLayout(LayoutKind.Sequential)] 
      internal struct Win32Point 
      { 
       public Int32 X; 
       public Int32 Y; 
      }; 
      public static Point GetMousePosition() 
      { 
       Win32Point w32Mouse = new Win32Point(); 
       GetCursorPos(ref w32Mouse); 
       return new Point(w32Mouse.X, w32Mouse.Y); 
      } 

      private double screenWidth; 
      private double screenHeight; 

      public MainWindow() 
      { 
       InitializeComponent(); 
      } 

      private void Window_Loaded(object sender, RoutedEventArgs e) 
      { 
       screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; 
       screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight; 

       this.Width = screenWidth; 
       this.Height = screenHeight; 
       this.Top = 0; 
       this.Left = 0; 
       DispatcherTimer timer = new DispatcherTimer(); 
       timer.Tick += new EventHandler(timer_Tick); 
       timer.Start(); 
      } 

      void timer_Tick(object sender, EventArgs e) 
      { 
       var mouseLocation = GetMousePosition(); 
       elipse1.Margin = new Thickness(mouseLocation.X, mouseLocation.Y, screenWidth - mouseLocation.X - elipse1.Width, screenHeight - mouseLocation.Y- elipse1.Height); 
      } 
     } 
    } 

XAML

<Window x:Class="Test.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Title="MainWindow" 
      Height="350" Width="525" 
      WindowStyle="None" 
      Topmost="True" 
      AllowsTransparency="True" 
      Background="Transparent" 
      ShowInTaskbar="False" 
      Loaded="Window_Loaded"> 
     <Grid> 
       <Ellipse Width="20" Height="20" Fill="Red" Name="elipse1"/> 
     </Grid> 
    </Window> 

해결! 그러나 후반에 :(

+1

오, 고마워. 하지만 내가 반투명 한 캔버스를 원하지 않는다는 의견에서 말했듯이 Opacity = "0.01"... 다른 창 (예 : Firefox 창)을 클릭 할 수 있기 때문에 WPF 창이 맨 위에 남아 있습니다. –

+1

@ John 두 응용 프로그램에서 동시에 마우스 이벤트를 어떻게 가져올 것으로 예상합니까? 투명 오버레이 창에서 마우스를 움직여 다른 응용 프로그램에서 클릭하십시오? 그것은 작동하지 않습니다! – Clemens

+0

@Clemens 나는 그것이 Win32를 통해 행해질 수 있다고 믿는다. 세부 사항을 기억하지는 못했지만 이전에 win32 message hackery에 대해 읽은 것을 기억합니다. – Dmitry

관련 문제