WPF:绘制自己的光标 - 平凡的问题 [英] WPF: drawing own cursor - nontrivial problem

查看:263
本文介绍了WPF:绘制自己的光标 - 平凡的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个游标,一些非常具体的功能:




  • 它必须动画

    • 因为n秒后自动点击 - 这样的动画是用户反馈时,点击会发生


  • 它捕捉到我们的一些控制

  • 的它有我们的应用程序



<外部p工作>的方法至今:




  • 渲染我的WPF控制成位图,使光标结构了出来,并用user32.dll中/ SetSystemCursor设置它

    • PRO

    • 光标具有后鼠标没有延迟,因为它是一个真正的光标

    • CON

    • 捕捉是挺难的,尤其是因为我们有绝对和相对inputdevices和我将不得不重新设置该mouseposition所有的时间,或使用user32.dll中/ ClipCursor函数(System.Windows.Forms.Cursor.Clip不相同),但抢购光标总是围绕着捕获的位置晃动(试图逃跑,让我们再重新....)

    • 我使用的代码一些随机的时间后,抛出奇怪异常 - 所以我当前的代码似乎相当不稳定


  • 渲染我自己的光标到最大化,最顶层,allowtransparent,windowstyle =无,不可见的窗口并手动将光标移动鼠标后(如Canvas.SetLeft(光标,MousePosition.X))

    • PRO

    • 捕捉可(容易)做

    • CON

    • 鼠标点击和命中时的光标移动光标获取的点击并不会在窗口之外

    • 轮询调度员背景环的mouseposition所有的时间似乎并不十分美好的事啊




要解决第二个接近我的光标就必须有至少一个透明像素
中的热点,让鼠标可以通过点击...这似乎并不像一个真正的解决方案给我...



任何想法的人



编辑:
一些示例代码显示的问题...



示例应用程序和放大器;源用鼠标捕捉到固定位置显示问题:ClipIt.rar



示例应用程序和放大器;随机时间后失败源 - 创下了自绘光标:TryOwnCur.rar



可以下找到:的 http://sourcemonk.com/Cursor


解决方案

感谢<一个HREF =http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f相对=nofollow> http://social.msdn.microsoft .COM /论坛/ EN-US / WPF /线程/ a3cb7db6-5014-430f-a5c2-c9746b077d4f



我可以通过我的自绘光标点击随后由
鼠标的位置设置窗口样式:无,allowtransparent因为我已经没有和
,则

 公共const int的WS_EX_TRANSPARENT = 0x00000020; 
公共const int的GWL_EXSTYLE =(-20);

函数[DllImport(user32.dll中)]
公共静态外部INT GetWindowLong(HWND的IntPtr,
INT指数);

函数[DllImport(user32.dll中)]
公共静态外部INT SetWindowLong函数(HWND的IntPtr,
INT指数,诠释newStyle);

公共静态无效makeTransparent(IntPtr的HWND){
INT extendedStyle = GetWindowLong(HWND,GWL_EXSTYLE);
SetWindowLong函数(HWND,GWL_EXSTYLE,extendedStyle | WS_EX_TRANSPARENT);
}

和来自OnSourceInitialized ...


调用makeTransparent

i need to implement a cursor with some very specific features:

  • it has to be animated
    • because after n seconds it automatically clicks - so the animation is feedback for the user when the click will happen
  • it has to snap to some of our controls
  • it has to work outside of our application

the approaches so far:

  • render my WPF-control into a bitmap, make a cursor-struct out of it and use user32.dll/SetSystemCursor to set it
    • PRO
    • the cursor has no delay after the mouse since it's a real cursor
    • CON
    • snapping is quite hard, especially since we have absolute and relative inputdevices and i would have to reset the mouseposition all the time or use user32.dll/ClipCursor (System.Windows.Forms.Cursor.Clip does the same) but the snapped cursor is always shaking around the snapped position (tries to escape, get's reset again....)
    • the code i use throws strange exceptions after some random time - so my current code seems quite unstable
  • render my own cursor into a maximized, topmost, allowtransparent, windowstyle=none, invisible window and manually move the cursor after the mouse (like Canvas.SetLeft(cursor, MousePosition.X))
    • PRO
    • snapping can be (easily) done
    • CON
    • when the mouse clicks and hit's the cursor the cursor get's clicked and not the window beyond
    • polling the mouseposition in a dispatcher-background-loop all the time doesn't seem very beautiful to me

to solve the second approach my cursor would have to have at least one transparent pixel in the hotspot, so that the mouse can click through... that doesn't seem like a real solution to me...

any idea's anyone?

EDIT: some example-source to show the problems...:

example app & source to show the problem with snapping the mouse to a fixed position: ClipIt.rar

example app & source that fails after random time - setting a self-drawn cursor: TryOwnCur.rar

can be found under: http://sourcemonk.com/Cursor

解决方案

thanks to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f

i can click through my self-drawn cursor which follows the mouse-position by setting the window style:none, and allowtransparent as i already did and then

public const int WS_EX_TRANSPARENT = 0x00000020;
  public const int GWL_EXSTYLE = (-20);

  [DllImport("user32.dll")]
  public static extern int GetWindowLong(IntPtr hwnd,
  int index);

  [DllImport("user32.dll")]
  public static extern int SetWindowLong(IntPtr hwnd,
  int index, int newStyle);

  public static void makeTransparent(IntPtr hwnd) {
     int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
     SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
  }

and call makeTransparent from OnSourceInitialized...

这篇关于WPF:绘制自己的光标 - 平凡的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆