在64位应用程序中,在64位Windows上pinvoke user32.dll是错误的吗?我成功完成了很多次并且从未出现过错误,但这似乎是矛盾的.我应该寻找user64.dll吗?
我使用这块板作为键盘用于演示目的.
无论如何,长话短说一切都很好,除了很少的情况.我使用user32.dll中的SendInput函数发送击键.
所以我的程序看起来像:
static void Main(string[] args)
{
Console.Write("Press enter an on the next secont the key combination shift+end will be send");
Console.Read();
Thread.Sleep(1000);
SendKeyDown(KeyCode.SHIFT);
SendKeyPress(KeyCode.END);
SendKeyUp(KeyCode.SHIFT);
Console.Read();
Console.Read();
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
/// <summary>
/// simulate key press
/// </summary>
/// <param name="keyCode"></param>
public static void SendKeyPress(KeyCode keyCode)
{
INPUT input = new INPUT {
Type = 1
};
input.Data.Keyboard = new KEYBDINPUT() { …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个小应用程序,除了其他功能外,还可以在键入时将快捷方式扩展为全文.例如:用户在某处写"BNN"并按下相关的键盘组合,应用程序将用"Hi I am Banana"替换"BNN".
经过一些研究,我了解到它可以使用user32.dll
,完成这项任务的过程如下:
1)获取活动窗口句柄
2)获取活动窗口线程句柄
3)将输入连接到活动线程
4)获得聚焦控制句柄(+插入位置但不是问题)
5)从活动线程分离输入
6)得到来自聚焦控件的文本使用其句柄
这是我目前的代码:
try
{
IntPtr activeWindowHandle = GetForegroundWindow();
IntPtr activeWindowThread = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero);
IntPtr thisWindowThread = GetWindowThreadProcessId(this.Handle, IntPtr.Zero);
AttachThreadInput(activeWindowThread, thisWindowThread, true);
IntPtr focusedControlHandle = GetFocus();
AttachThreadInput(activeWindowThread, thisWindowThread, false);
if (focusedControlHandle != IntPtr.Zero)
{
TB_Output.Text += focusedControlHandle + " , " + GetText(focusedControlHandle) + Environment.NewLine;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
//...
//...
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet …
Run Code Online (Sandbox Code Playgroud) 可能重复:
在C#中以编程方式锁定Windows工作站
我目前正在开发一个Visual Studio Windows窗体应用程序,它需要一个锁定工作站的功能.当调用该函数时,如何使用user32.dll进行锁定(Windows + L)?
有没有办法使用Win32,在创建新窗口时注册通知.我正在尝试保留当前打开的窗口列表,但现在只是使用查询当前窗口列表EnumWindows()
.
有人做过类似的事吗?
谢谢
我不确定我是否正确,但我无法启动SetWindowsHookEx方法.
想到什么?
这是我的剪辑
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
private static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
const int HSHELL_WINDOWCREATED = 1;
private static HookProc winDelegate = ShellHookProcDelegate;
internal static void RegisterWindowCreatedEvent()
{
SetWindowsHookEx(HookType.WH_SHELL, winDelegate, IntPtr.Zero, 0);
}
private static int ShellHookProcDelegate(int code, IntPtr wParam, IntPtr lParam)
{
if (code != HSHELL_WINDOWCREATED)
{
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}
//App …
Run Code Online (Sandbox Code Playgroud) 我试过[DllImport("user32.dll")]静态extern bool SetCursorPos(int X,int Y);
并且它可以很好地将光标移动到所需的点.我从来没有尝试过这种类型的DLL导入,但它的工作原理:).但是我想要更多我还能提取什么?主要是我想双击,点击或使用滚轮选项而无需任何鼠标输入,只是代码我该怎么做?以及如何查看user32dll中还包含哪些内容?
感谢名单
考虑这两个定义GetWindowText
.一个使用a string
作为缓冲区,另一个使用a StringBuilder
代替:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, string lpString, int nMaxCount);
Run Code Online (Sandbox Code Playgroud)
这是你怎么称呼他们:
var windowTextLength = GetWindowTextLength(hWnd);
// You can use either of these as they both work
var buffer = new string('\0', windowTextLength);
//var buffer = new StringBuilder(windowTextLength);
// Add 1 to windowTextLength for the trailing null character
var readSize …
Run Code Online (Sandbox Code Playgroud) 我试过了:
要添加user32.dll中从参考经理,并从进口它的Windows\SYSTEM32\user32.dll中,我得到了错误信息:
无法添加对"C:\ Windows\System32\user32.dll"的引用.请确保该文件是可访问的,并且它是有效的程序集或COM组件.
using System.Runtime.InteropServices; [DllImport("user32")]
以管理员身份启动Visual Studio
什么都行不通......它让我感到紧张我试着用2个小时来导入这个该死的.dll ......
我创建了一个无边框窗口应用程序和一个"假"标题栏来拖动它.
我正在使用user32.dll,
这开始拖动窗口(由统一IBeginDragHandler触发):
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int WM_NCLBUTTONUP = 0x00A2;
public const int WM_LBUTTONUP = 0x0202;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
IntPtr window = GetActiveWindow();
...
...
ReleaseCapture();
SendMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, 0);
Run Code Online (Sandbox Code Playgroud)
这是为了停止拖动(不确定这部分):
ReleaseCapture();
SendMessage(window, WM_NCLBUTTONUP, HTCAPTION, 0);
SendMessage(window, WM_LBUTTONUP, HTCAPTION, 0);
Run Code Online (Sandbox Code Playgroud)
它在编辑器和构建上运行良好,但在developpement构建上产生错误:
发生了异常情况:已经递归调用了PlayerLoop内部函数.请通过示例项目联系客户支持,以便我们重现问题并对其进行故障排除.LauncherWindow:SendMessage(IntPtr,Int32,Int32,Int32)LauncherWindow:StartWindowDrag()(在E:\ Unity Projects\Crime Club Launcher\Assets\Scripts\Lib\LauncherWindow.cs:115)WindowDragZone:UnityEngine.EventSystems.IBeginDragHandler.OnBeginDrag (PointerEventData)(在E:\ Unity Projects\Crime Club Launcher\Assets\WindowDragZone.cs:9)UnityEngine.EventSystems.ExecuteEvents:Execute(IBeginDragHandler,BaseEventData)(在C:\ buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\ExecuteEvents.cs:64)UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject,BaseEventData,EventFunction`1)(在C:\ buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\ExecuteEvents) .cs:261)UnityEngine.EventSystems.PointerInputModule:ProcessDrag(PointerEventData)(在C:\ buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\InputModules\PointerInputModule.cs:261)UnityEngine.EventSystems.StandaloneInputModule: ProcessMouseEvent(Int32)(在C:\ buildslave\unity\build\Exte nsions\guisystem\UnityEngine.UI\EventSystem\InputModules\StandaloneInputModule.cs:434)UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent()(在C:\ buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\InputModules\StandaloneInputModule.cs:412)UnityEngine.EventSystems.StandaloneInputModule:Process()(在C:\ …
我正在尝试找到一个最小化的窗口并显示它.
该程序可以从三星下载,标题为"SideSync".要完全复制我的问题,您需要安装此问题并使用三星手机插入计算机.
以下是完全配置和运行的屏幕截图:
观察到有两个窗口,A和B.我使用一个名为Microsoft Inspect的工具来确定两个程序窗口是正常的窗口.他们没有孩子的父母关系.但是,当我启动SideSync时,只显示窗口A. 然后我必须单击"电话屏幕",然后出现窗口B(除了窗口A).这可能是解决这个问题的线索吗?我们会看到.
以下是Microsoft Inspect中出现的两个窗口:
两个窗口都有窗口标题.使用下面的代码我可以检索Process
窗口(这是我的目标).
服务器代码:
public static Process GetProcessByWindowTitle(string windowTitleContains)
{
foreach (var windowProcess in GetWindowProcesses())
if (windowProcess.MainWindowTitle.Contains(windowTitleContains))
return windowProcess;
return null;
}
Run Code Online (Sandbox Code Playgroud)
然而,一些奇怪的行为正在发生.GetProcessByWindowTitle()
将返回一个但不是两个进程.我假设因为有两个窗口必须有两个进程.
这Process
则返回依赖于这是我和我的鼠标点击最后一个窗口.
例如,如果我上次点击了窗口A; 然后GetProcessByWindowTitle("SideSync")
将返回Process
,但随后GetProcessByWindowTitle("SAMSUNG")
将返回void
.
...反之亦然,如果我上次点击窗口B,GetProcessByWindowTitle("SideSync")
将返回一个void
,但随后GetProcessByWindowTitle("SAMSUNG")
将返回Process
.
客户代码:
[Ignore("Requires starting SideSync and clicking one of the windows. Only the last clicked will return a Process.")]
[Test]
public void NonMinimizedWindowProcessIsDetected()
{
Process p1 …
Run Code Online (Sandbox Code Playgroud)