winapi SetWindowLongPtr和 SetWindowLong功能一样吗
- User
winapi SetWindowLongPtr和 SetWindowLong功能一样吗
- Assistant
在Win32 API中,SetWindowLongPtr和SetWindowLong函数用于设置窗口过程的属性,并返回先前设置的值。这两个函数可以用于32位和64位操作系统。SetWindowLongPtr的作用与SetWindowLong相同,但是SetWindowLongPtr支持指针大小平台(32位和64位)。因此,在64位操作系统上,只有SetWindowLongPtr可用。在32位操作系统上,两个函数都是可用的,但是使用SetWindowLongPtr更好,因为它可以在64位操作系统上工作,并且可以帮助您防止从32位代码中使用32位指针时的细微错误。
- User
C#中以下代码:WinApi.SetWindowLongPtr(this.Handle, ConstDefine.GWL_EXSTYLE, (int)(WinApi.GetWindowLongPtr(this.Handle, ConstDefine.GWL_EXSTYLE) | ConstDefine.WS_EX_TOOLWINDOW) & ~ConstDefine.WS_EX_APPWINDOW);中的错误应该怎么改
- Assistant
根据这段代码所在的上下文和错误提示,无法确定出现的具体问题。有可能是参数类型不匹配,也有可能是WinApi方法、ConstDefine常量定义存在问题。以下是一些常见的问题及其解决方法:
-
参数类型不匹配。WinApi.SetWindowLongPtr方法的第二个参数类型应该是IntPtr,而不是ConstDefine.GWL_EXSTYLE所代表的int类型。您可以这样修改代码:
WinApi.SetWindowLongPtr(this.Handle, ConstDefine.GWL_EXSTYLE, new IntPtr((int)(WinApi.GetWindowLongPtr(this.Handle, ConstDefine.GWL_EXSTYLE) | ConstDefine.WS_EX_TOOLWINDOW) & ~ConstDefine.WS_EX_APPWINDOW)); -
变量或常量名错误。请确保WinApi和ConstDefine都正确引用并命名。
-
没有对WinApi方法进行声明。如果您是手动定义WinApi的封装类,要确保WinApi.SetWindowLongPtr和WinApi.GetWindowLongPtr都正确地声明为静态方法。
无论出现何种问题,建议您仔细检查一下代码并确定所使用的变量、常量、方法等是否都正确定义和引用。如果还有问题,请提供更详细的错误信息以便更好地解答。
-
- User
使用 SetWindowLongPtr和GetWindowLongPtr函数,设置窗口样式,使用 WS_EX_TOOLWINDOW 样式,并同时取消 WS_EX_APPWINDOW 样式,用C# 怎么写
- Assistant
您可以使用以下代码在C#中实现使用SetWindowLongPtr和GetWindowLongPtr函数设置窗口样式,并同时取消WS_EX_APPWINDOW样式,同时启用WS_EX_TOOLWINDOW样式:
using System; using System.Runtime.InteropServices; public class WinApi { [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex); public static IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex) { if (IntPtr.Size == 8) { return GetWindowLongPtr64(hwnd, nIndex); } else { return GetWindowLongPtr32(hwnd, nIndex); } } [DllImport("user32.dll", EntryPoint = "GetWindowLong")] public static extern IntPtr GetWindowLongPtr32(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")] public static extern IntPtr GetWindowLongPtr64(IntPtr hwnd, int nIndex); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowLongPtr(IntPtr hwnd, int nIndex, IntPtr dwNewLong); public static IntPtr SetWindowLongPtr(IntPtr hwnd, int nIndex, IntPtr dwNewLong) { if (IntPtr.Size == 8) { return SetWindowLongPtr64(hwnd, nIndex, dwNewLong); } else { return SetWindowLongPtr32(hwnd, nIndex, dwNewLong); } } [DllImport("user32.dll", EntryPoint = "SetWindowLong")] public static extern IntPtr SetWindowLongPtr32(IntPtr hwnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] public static extern IntPtr SetWindowLongPtr64(IntPtr hwnd, int nIndex, IntPtr dwNewLong); public static class GWL_EXSTYLE { public const int GWL_EXSTYLE = -20; public const int WS_EX_TOOLWINDOW = 0x00000080; public const int WS_EX_APPWINDOW = 0x00040000; } } // 在窗体代码中使用以下代码设置窗口样式 IntPtr ptr = WinApi.GetWindowLongPtr(this.Handle, WinApi.GWL_EXSTYLE.GWL_EXSTYLE); IntPtr newPtr = new IntPtr(ptr.ToInt64() | WinApi.GWL_EXSTYLE.WS_EX_TOOLWINDOW); newPtr = new IntPtr(newPtr.ToInt64() & ~WinApi.GWL_EXSTYLE.WS_EX_APPWINDOW); WinApi.SetWindowLongPtr(this.Handle, WinApi.GWL_EXSTYLE.GWL_EXSTYLE, newPtr);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.