Mouse translation from System.Windows.Forms.MouseButtons to System.Windows.Input.MouseButton

本文详细介绍了如何将Winforms中的MouseButton类转换为WPF中的MouseButton类,提供了具体的代码实现,帮助开发者在不同UI系统间进行无缝转换。
部署运行你感兴趣的模型镜像

In the UI world, there are two system, the Winforms and the WPF.   The WPF ones come later and have a face lift on the overall classes and designs. 


The team has made tremendous effort to bridge the two world. However, situation exists that you have to convert between the two of them , for example the MouseButton class.

  • System.Windows.Forms.MouseButtons
  • System.Windows.Input.MouseButton


Below shows the conversion code that does the code between the two.



    private static MouseButton ToMouseButton(Forms.MouseButtons button)
    {
      switch (button)
      {
        case Forms.MouseButtons.Left:
          return MouseButton.Left;
        case Forms.MouseButtons.Right:
          return MouseButton.Right;
        case Forms.MouseButtons.Middle:
          return MouseButton.Middle;
        case Forms.MouseButtons.XButton1:
          return MouseButton.XButton1;
        case Forms.MouseButtons.XButton2:
          return MouseButton.XButton2;
      }

      throw new InvalidOperationException();
    }




您可能感兴趣的与本文相关的镜像

EmotiVoice

EmotiVoice

AI应用

EmotiVoice是由网易有道AI算法团队开源的一块国产TTS语音合成引擎,支持中英文双语,包含2000多种不同的音色,以及特色的情感合成功能,支持合成包含快乐、兴奋、悲伤、愤怒等广泛情感的语音。

Windows 程序自动化中,使用 `System.Windows.Automation` 可以有效地获取弹窗中的按钮名称。该过程主要依赖于 UI 自动化框架提供的 `AutomationElement` 类来查找控件,并通过控件模式(如 `InvokePattern`)与控件交互。 ### 获取弹窗中的按钮名称 在获取按钮名称之前,需要先定位到目标弹窗。可以使用 `AutomationElement.RootElement` 获取桌面根元素,然后通过 `FindAll` 方法查找所有窗口级别的元素。一旦找到目标窗口,可以通过递归遍历其子元素来查找按钮控件: ```csharp using System.Windows.Automation; public static void FindButtonsInWindow(AutomationElement window) { if (window == null) return; Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button); AutomationElementCollection buttons = window.FindAll(TreeScope.Descendants, condition); foreach (AutomationElement button in buttons) { MessageBox.Show($"按钮名称: {button.Current.Name}"); } } ``` 该方法适用于查找指定窗口中的所有按钮控件,并获取其名称属性[^1]。 ### 使用 `InvokePattern` 与按钮交互 在找到按钮后,可以使用 `InvokePattern` 来模拟点击操作。首先检查按钮是否支持 `InvokePattern`,然后调用 `Invoke` 方法执行点击: ```csharp foreach (AutomationElement button in buttons) { try { InvokePattern invokePattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; invokePattern.Invoke(); } catch (InvalidOperationException) { // 控件不支持 InvokePattern } } ``` 此方式可用于自动化测试或 UI 自动化脚本中,模拟用户点击按钮的行为。 ### 使用 `TreeScope` 和 `Condition` 精确查找控件 在查找按钮时,可以通过 `TreeScope` 指定查找范围(如 `Descendants` 表示查找所有子元素),并通过 `Condition` 筛选特定类型的控件。例如,使用 `PropertyCondition` 查找 `ControlType` 为 `Button` 的控件: ```csharp Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button); AutomationElementCollection buttons = window.FindAll(TreeScope.Descendants, condition); ``` 此方法可提高查找效率并减少不必要的元素遍历[^1]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值