gpt4 book ai didi

c# - User32 SetWindowLong 接受 int 而不是 long

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:31 24 4
gpt4 key购买 nike

当我尝试在 C# 中调用 User32.dll 中的函数 SetWindowLong 时,没有任何反应。我知道为什么,但我不知道如何“修复”这个。这是一段代码。

[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern long SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int WS_EX_TOPMOST = 0x00000008;
const int GWL_EXSTYLE = -20;
public static bool IsWindowTopMost(int id)
{
return (GetWindowLong(GetHWNDById(id), GWL_EXSTYLE) & WS_EX_TOPMOST) == WS_EX_TOPMOST;
}
public static void SetAlwaysOnTop(int id)
{
IntPtr hWnd = GetHWNDById(id);
long actuallyStyle = GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_TOPMOST;
SetWindowLong(hWnd, GWL_EXSTYLE, (int)actuallyStyle));
}

IsWindowTopMost 工作正常,但 SetAlwaysOnTop 不工作。快速检查代码后,我发现了一些有趣的东西。 GetWindowLong 后的变量“actuallyStyle”等于 4295295232,OR 运算后为 4295295240。这里是问题所在,函数 SetWindowLong 接受一个 Integer 作为 dwNewLong。当我在 SetWindowLong 的定义中将 int 更改为 long 时,pinvoke 会抛出错误,因为“函数和目标函数不匹配”。

它是如何传播的?

最佳答案

将窗口置于最前面的正确方法是使用 SetWindowPos function .问题很可能是 SetWindowLong 只是设置了一个变量,但 SetWindowPos 实际上会通知窗口管理器进行所需的重绘,因此请改用它。

现在关于问题标题,SetWindowLongGetWindowLong 都必须声明为int,而不是long .
这有两个原因。首先,C和C#的区别。整个 Windows API 文档都是用 C 术语定义的,where long means 32 bits signed integer ,但 C# 将 long 威胁为 64 位带符号整数,因此给出了您遇到的错误。使用 64 位的函数在 API 文档中声明为 long long
造成这种差异的另一个原因是历史原因。这两个函数都是在 Windows 的 16 位时代创建的,其中 int 是 16 位,long 是 32 位。 int 类型后来被扩展了,但是 long 保持原样。名称未更改以保持兼容性。

关于c# - User32 SetWindowLong 接受 int 而不是 long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34577624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com