- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我尝试将 XButton 1 和 2(鼠标的侧键)重定向到特定的 Visual Studio 操作。
当我按下 XButton1 时,我想编译/构建项目。此操作默认绑定(bind)到 F6。
当我按下 XButton2 时,我想在代码和设计 View (WinForms) 之间切换。这绑定(bind)到 F7。
在多次尝试使用 Visual Studio 内置工具后,我使用 AutoHotKey 创建了以下脚本:
XButton2::
IfWinActive Microsoft Visual Studio
{
Send {F7}
return
}
XButton1::
IfWinActive Microsoft Visual Studio
{
Send {F6}
return
}
但是我想知道是否有人知道使用 Visual Studio 2015 实现相同目标的 native 方法?
最佳答案
主要思想是注册全局鼠标钩子(Hook)并处理所需的鼠标事件并运行 visual studio 命令。为此:
SetWindowsHookEx
注册一个全局鼠标钩子(Hook)通过传递 WH_MOUSE_LL
并处理所需的鼠标事件,例如 WM_XBUTTONDOWN
.在加载解决方案时执行注册。使用 DTE.ExecuteCommand
运行所需的 Visual Studio 命令传递合适的命令,例如 Build.BuildSolution
:
var dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.ExecuteCommand("Build.BuildSolution");
UnhookWindowsHookEx
取消 Hook 当解决方案关闭时。注意:
要找到您需要的命令,请转到 工具 → 选项 → 环境 → 键盘
并找到您需要的命令。
您会找到很多关于如何注册全局鼠标 Hook 的资源,例如 this我改变了一下并用于测试。在帖子的末尾,您可以找到完整的源代码。
在 Visual Studio 2013 中,加载项已弃用,因此您可以使用 Visual Studio Add-in 执行相同的操作项目,但最好使用 VSPackages 来完成。
首先创建一个 Visual Studio 包项目并将包的代码更改为我在此处发布的代码。还将我用于全局鼠标 Hook 和 Windows API 的类添加到解决方案中。
打包
这是我的包的完整代码:
using Microsoft.VisualStudio.Shell;
using System;
using System.Runtime.InteropServices;
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)]
public sealed class VSPackage1Package : Package
{
public VSPackage1Package() { }
EnvDTE.DTE dte;
protected override void Initialize()
{
base.Initialize();
dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.Events.SolutionEvents.Opened += SolutionEvents_Opened;
dte.Events.SolutionEvents.AfterClosing += SolutionEvents_AfterClosing;
}
void SolutionEvents_AfterClosing() { MouseHook.Stop(); }
void SolutionEvents_Opened()
{
MouseHook.Start();
MouseHook.MouseAction += MouseHook_MouseAction;
}
void MouseHook_MouseAction(object sender, EventArgs e)
{
dte.ExecuteCommand("Build.BuildSolution");
}
}
Windows API 消息、结构和方法
using System;
using System.Runtime.InteropServices;
public class Win32
{
public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
public const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT { public int x; public int y; }
[StructLayout(LayoutKind.Sequential)]
public struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn,
IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam,
IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
}
全局鼠标钩子(Hook)
因为我的鼠标中没有 XButton,所以我处理了 WM_RBUTTONDOWN
事件。
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class MouseHook
{
public static event EventHandler MouseAction = delegate { };
private static Win32.LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public static void Start() { _hookID = SetHook(_proc); }
public static void Stop() { Win32.UnhookWindowsHookEx(_hookID); }
private static IntPtr SetHook(Win32.LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
var handle = Win32.GetModuleHandle(curModule.ModuleName);
return Win32.SetWindowsHookEx(Win32.WH_MOUSE_LL, proc, handle, 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
Win32.MouseMessages.WM_RBUTTONDOWN == (Win32.MouseMessages)wParam)
{
Win32.MSLLHOOKSTRUCT hookStruct =
(Win32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
typeof(Win32.MSLLHOOKSTRUCT));
MouseAction(null, new EventArgs());
}
return Win32.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
关于c# - 将鼠标侧键绑定(bind)到 VisualStudio 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925495/
我注意到今天我们在 VS 2015 中的一个解决方案发生了变化。似乎为解决方案生成的测试项目使用与同一解决方案中现有测试项目不同的命名空间。 仅引用的测试项目 Microsoft.VisualStud
更新测试项目 NuGet 后出现此错误: The type 'TestClassAttribute' exists in both 'Microsoft.VisualStudio.QualityToo
我正在尝试设置 tfs 和 sharepoint 之间的构建集成。 我们有一个单独的构建服务器。我们已关注 these steps并检查它们的正确性 3 次。但是在 sharepoint 构建中,我们
今天在安装VS2019的时候,在安装的过程中一直无法进入安装界面,在网上找了各种方法试了将近40分钟都没有找到有效的办法,不过就快放弃的时候,问题解决了,哈哈哈!!!! 1.下载地址:https:
在 Visual Studio 中,用于在 WinForms 项目中添加元素的上下文菜单命令与 WPF 项目中显示的命令不同。事实上,我可以在第一个中看到“Add->Windows Form”,在第二
我正在创建一个多项目模板 问题是当我运行模板时,每个项目都会创建一个与项目名称匹配的目录文件夹。 我不希望每个项目都默认创建文件夹,如下所示: solutionfolder\Libraries\BL\
我下载了这门课http://www.codeproject.com/file/VersionInfo.asp 我用它从我当前运行的几个程序中获取文件信息。 当我想从我编写的程序中获取信息时,它工作正常
我正在尝试调试一个树状结构,所以我在每一层都制作了一个 watch 。我走得越低, watch 变量名称变得太长了。有没有办法重命名它们: {,,HTM_Projekt.exe}*(Node*){*}
我已经编写了一个 DLL,然后在我的解决方案中添加了对它的引用,但是当我在 usings 部分中声明它时,它给我的错误是无法识别,您是否缺少引用? 这是我设置解决方案的方式... 1-首先我创建了我的
我们有一个大型 C# 程序,分为 2 个主要组:内核 库和(客户)特定 应用程序和服务。 我们的 TFS 结构(简化)是这样的: 内核 深度学习计划 第 1 版 第 2 版 第 3 版 ... 客户A
VS 2010 Pro:我已经为我的库生成了一个类图,现在向它添加了一些类,但是类图只显示了旧类并且不会自动更新。所以我想知道这是不是这样,我应该每次都删除并重新添加类图吗?或者这些是我应该在 VS
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我刚刚在 c# .net 中制作了一个程序,现在当我尝试保存或调试它时,我在消息框中收到此错误。这是我目前为我的项目编写的代码。错误代码“内存不足,无法继续执行程序”请帮忙。
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我想扩展 Visual Studio,这样当有人右键单击解决方案资源管理器时,上下文菜单应该有一个新菜单项,比如“打开自定义表单”,单击哪个应该打开一个表单(这个表单实际上会接受一些设置并修改配置相应
IDE在启动时在启动屏幕上崩溃。我查看了日志,发现了很多错误: 14 2017/07/09 07:28:50.672 Information VisualSt
有没有人想知道 VisualStudio 将区域状态存储在源代码中的什么位置(展开或折叠?)。据我所知,它们没有存储在解决方案、项目或源文件中...... 最佳答案 所有这些类型的设置(区域折叠状态、
我正在使用T4(文本模板转换工具包)进行一些工作,并试图达到可以创建自己的自定义文本模板宿主的地步。但是,所有这些都依赖于 Microsoft.VisualStudio.TextTemplating
我创建了一个名为“WordpressAutomation”的新解决方案 我在这个解决方案中创建了一个名为“WordpressAutomation”的新项目,一个类库 我在这个解决方案中创建了一个新项目
最近我试用了 Visual Studio 2017。我们有一个 MSDN 订阅。我们正在开发一些我们正在编写为 WPF 应用程序的内部应用程序。这些应用程序需要安装在用户没有管理权限的用户计算机(Wi
我是一名优秀的程序员,十分优秀!