Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit 9ecf26c

Browse files
authored
Merge pull request #384 from vatsanm/master2
Adding User32.SetWindowLongPtr
2 parents 2eaa7f2 + bb32926 commit 9ecf26c

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/User32/PublicAPI.Unshipped.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ PInvoke.User32.SendMessageTimeoutFlags.SMTO_NOTIMEOUTIFNOTHUNG = 8 -> PInvoke.Us
3636
PInvoke.User32.WindowMessage.WM_DPICHANGED_AFTERPARENT = 739 -> PInvoke.User32.WindowMessage
3737
PInvoke.User32.WindowMessage.WM_DPICHANGED_BEFOREPARENT = 738 -> PInvoke.User32.WindowMessage
3838
PInvoke.User32.WindowMessage.WM_GETDPISCALEDSIZE = 740 -> PInvoke.User32.WindowMessage
39+
PInvoke.User32.WindowLongIndexFlags.GWLP_ID = PInvoke.User32.WindowLongIndexFlags.GWL_STYLE | PInvoke.User32.WindowLongIndexFlags.DWLP_DLGPROC -> PInvoke.User32.WindowLongIndexFlags
40+
PInvoke.User32.WindowLongIndexFlags.GWLP_USERDATA = -21 -> PInvoke.User32.WindowLongIndexFlags
41+
PInvoke.User32.WindowLongIndexFlags.GWLP_WNDPROC = PInvoke.User32.WindowLongIndexFlags.GWL_STYLE | PInvoke.User32.WindowLongIndexFlags.DWLP_DLGPROC | PInvoke.User32.WindowLongIndexFlags.DWLP_USER -> PInvoke.User32.WindowLongIndexFlags
3942
PInvoke.User32.mouse_eventFlags
4043
PInvoke.User32.mouse_eventFlags.MOUSEEVENTF_ABSOLUTE = 32768 -> PInvoke.User32.mouse_eventFlags
4144
PInvoke.User32.mouse_eventFlags.MOUSEEVENTF_HWHEEL = 4096 -> PInvoke.User32.mouse_eventFlags
@@ -53,6 +56,7 @@ static PInvoke.User32.AdjustWindowRectExForDpi(System.IntPtr lpRect, PInvoke.Use
5356
static PInvoke.User32.CreateWindowEx(PInvoke.User32.WindowStylesEx dwExStyle, short lpClassName, string lpWindowName, PInvoke.User32.WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, System.IntPtr hWndParent, System.IntPtr hMenu, System.IntPtr hInstance, System.IntPtr lpParam) -> System.IntPtr
5457
static PInvoke.User32.CreateWindowEx(PInvoke.User32.WindowStylesEx dwExStyle, short lpClassName, string lpWindowName, PInvoke.User32.WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, System.IntPtr hWndParent, System.IntPtr hMenu, System.IntPtr hInstance, void* lpParam) -> System.IntPtr
5558
static PInvoke.User32.GetNextWindow(System.IntPtr hWnd, PInvoke.User32.GetNextWindowCommands wCmd) -> System.IntPtr
59+
static PInvoke.User32.SetWindowLongPtr(System.IntPtr hWnd, PInvoke.User32.WindowLongIndexFlags nIndex, System.IntPtr dwNewLong) -> System.IntPtr
5660
static PInvoke.User32.SystemParametersInfoForDpi(PInvoke.User32.SystemParametersInfoAction uiAction, int uiParam, System.IntPtr pvParam, PInvoke.User32.SystemParametersInfoFlags fWinIni, int dpi) -> bool
5761
static PInvoke.User32.mouse_event(PInvoke.User32.mouse_eventFlags dwFlags, int dx, int dy, int dwData, System.IntPtr dwExtraInfo) -> void
5862
static extern PInvoke.User32.AdjustWindowRectExForDpi(PInvoke.RECT* lpRect, PInvoke.User32.WindowStyles dwStyle, bool bMenu, PInvoke.User32.WindowStylesEx dwExStyle, int dpi) -> bool
@@ -78,6 +82,7 @@ static extern PInvoke.User32.SendMessageTimeout(System.IntPtr hWnd, PInvoke.User
7882
static extern PInvoke.User32.SetDialogControlDpiChangeBehavior(System.IntPtr hwnd, PInvoke.User32.DIALOG_CONTROL_DPI_CHANGE_BEHAVIORS mask, PInvoke.User32.DIALOG_CONTROL_DPI_CHANGE_BEHAVIORS values) -> bool
7983
static extern PInvoke.User32.SetDialogDpiChangeBehavior(System.IntPtr hDlg, PInvoke.User32.DIALOG_DPI_CHANGE_BEHAVIORS mask, PInvoke.User32.DIALOG_DPI_CHANGE_BEHAVIORS values) -> bool
8084
static extern PInvoke.User32.SetLastErrorEx(uint dwErrCode, uint dwType) -> void
85+
static extern PInvoke.User32.SetWindowLongPtr(System.IntPtr hWnd, PInvoke.User32.WindowLongIndexFlags nIndex, void* dwNewLong) -> void*
8186
static extern PInvoke.User32.SetProcessDpiAwarenessContext(System.IntPtr dpiAWarenessContext) -> bool
8287
static extern PInvoke.User32.SetThreadDpiAwarenessContext(System.IntPtr dpiContext) -> System.IntPtr
8388
static extern PInvoke.User32.SetThreadDpiHostingBehavior(PInvoke.User32.DPI_HOSTING_BEHAVIOR dpiHostingBehavior) -> PInvoke.User32.DPI_HOSTING_BEHAVIOR

src/User32/User32+WindowLongIndexFlags.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public enum WindowLongIndexFlags : int
1717
GWLP_HINSTANCE = -6,
1818
GWLP_HWNDPARENT = -8,
1919
GWL_ID = -12,
20+
GWLP_ID = GWL_ID,
2021
GWL_STYLE = -16,
2122
GWL_USERDATA = -21,
23+
GWLP_USERDATA = GWL_USERDATA,
2224
GWL_WNDPROC = -4,
25+
GWLP_WNDPROC = GWL_WNDPROC,
2326
DWLP_USER = 0x8,
2427
DWLP_MSGRESULT = 0x0,
2528
DWLP_DLGPROC = 0x4

src/User32/User32.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,87 @@ public delegate void WinEventProc(
256256
[DllImport(nameof(User32), SetLastError = true)]
257257
public static extern int MessageBoxIndirect(ref MSGBOXPARAMS lpMsgBoxParams);
258258

259-
[DllImport(nameof(User32), CharSet = CharSet.Unicode)]
259+
[DllImport(nameof(User32), SetLastError = true)]
260260
public static extern int SetWindowLong(IntPtr hWnd, WindowLongIndexFlags nIndex, SetWindowLongFlags dwNewLong);
261261

262+
/// <summary>
263+
/// Changes an attribute of the specified window. The function also sets a value at the specified
264+
/// offset in the extra window memory.
265+
/// </summary>
266+
/// <param name="hWnd">A handle to the window and, indirectly, the class to which the window belongs.
267+
/// The SetWindowLongPtr function fails if the process that owns the window specified by the
268+
/// <paramref name="hWnd"/> parameter is at a higher process privilege in the UIPI hierarchy than the
269+
/// process the calling thread resides in.</param>
270+
/// <param name="nIndex">The zero-based offset to the value to be set. Valid values are in the range zero
271+
/// through the number of bytes of extra window memory, minus the size of a LONG_PTR. To set any other value,
272+
/// specify one of the following values.
273+
/// +---------------------+---------------------------------------------------------------------------------------------------+
274+
/// | Value | Meaning |
275+
/// +---------------------+---------------------------------------------------------------------------------------------------+
276+
/// | GWL_EXSTYLE(-20) | Sets a new extended window style. |
277+
/// | GWLP_HINSTANCE(-6) | Sets a new application instance handle. |
278+
/// | GWLP_ID(-12) | Sets a new identifier of the child window.The window cannot be a top-level window. |
279+
/// | GWL_STYLE (-16) | Sets a new window style. |
280+
/// | GWLP_USERDATA (-21) | Sets the user data associated with the window.This data is intended for use by the application |
281+
/// | | that created the window. Its value is initially zero. |
282+
/// | GWLP_WNDPROC (-4) | Sets a new address for the window procedure. |
283+
/// +---------------------+---------------------------------------------------------------------------------------------------+
284+
///
285+
/// The following values are also available when the hWnd parameter identifies a dialog box.
286+
///
287+
/// +-------------------------------------------------+---------------------------------------------------------------------------+
288+
/// | Value | Meaning |
289+
/// +-------------------------------------------------+---------------------------------------------------------------------------+
290+
/// | DWLP_DLGPROC (DWLP_MSGRESULT + sizeof(LRESULT)) | Sets the new pointer to the dialog box procedure. |
291+
/// | DWLP_MSGRESULT (0) | Sets the return value of a message processed in the dialog box procedure. |
292+
/// | DWLP_USER (DWLP_DLGPROC + sizeof(DLGPROC)) | Sets new extra info |
293+
/// +-------------------------------------------------+---------------------------------------------------------------------------+
294+
/// </param>
295+
/// <param name="dwNewLong">The replacement value.</param>
296+
/// <returns>
297+
/// <para>If the function succeeds, the return value is the previous value of the specified offset.</para>
298+
/// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
299+
/// <para>If the previous value is zero and the function succeeds, the return value is zero, but the function does
300+
/// not clear the last error information. To determine success or failure, clear the last error information by
301+
/// calling SetLastError with 0, then call SetWindowLongPtr. Function failure will be indicated by a return value of
302+
/// zero and a GetLastError result that is nonzero.</para>
303+
/// </returns>
304+
/// <remarks>
305+
/// <list type="bullet">
306+
/// <item>The return type, and the type of <paramref name="dwNewLong"/> are both LONG_PTR.
307+
/// LONG_PTR is defined as <code>__int64</code> on 64-bit platforms, and it is defined as <code>long</code>
308+
/// on 32-bit platforms. This definition fits nicely with now <see cref="IntPtr"/> works on 32-bit vs. 64-bit
309+
/// platforms.</item>
310+
/// <item>Windows XP/2000: The SetWindowLongPtr function fails if the window specified by the
311+
/// <paramref name="hWnd"/> parameter does not belong to the same process as the calling thread.</item>
312+
/// <item>
313+
/// <para>Certain window data is cached, so changes you make using SetWindowLongPtr will not take effect until you call
314+
/// the SetWindowPos function.</para>
315+
/// <para>If you use SetWindowLongPtr with the <see cref="WindowLongIndexFlags.GWLP_WNDPROC"/> index to replace the window procedure,
316+
/// the window procedure must conform to the guidelines specified in the description of the WindowProc callback function.</para>
317+
/// <para>If you use SetWindowLongPtr with the <see cref="WindowLongIndexFlags.DWLP_MSGRESULT"/> index to set the return value for a
318+
/// message processed by a dialog box procedure, the dialog box procedure should return TRUE directly afterward. Otherwise, if you call
319+
/// any function that results in your dialog box procedure receiving a window message, the nested window message could overwrite the return value
320+
/// you set by using <see cref="WindowLongIndexFlags.DWLP_MSGRESULT"/>.</para>
321+
/// <para>Calling SetWindowLongPtr with the <see cref="WindowLongIndexFlags.GWLP_WNDPROC"/> index creates a subclass of the window
322+
/// class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process.
323+
/// The SetWindowLongPtr function creates the window subclass by changing the window procedure associated with a particular
324+
/// window class, causing the system to call the new window procedure instead of the previous one. An application must pass
325+
/// any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc.
326+
/// This allows the application to create a chain of window procedures.</para>
327+
/// <para>Reserve extra window memory by specifying a nonzero value in the <see cref="WNDCLASSEX.cbWndExtra"/> member of the
328+
/// <see cref="WNDCLASSEX"/> structure used with the RegisterClassEx function.</para>
329+
/// <para>Do not call SetWindowLongPtr with the <see cref="WindowLongIndexFlags.GWLP_HWNDPARENT"/> index to change the parent of a
330+
/// child window. Instead, use the SetParent function.</para>
331+
/// <para>If the window has a class style of <see cref="ClassStyles.CS_CLASSDC"/> or <see cref="ClassStyles.CS_PARENTDC"/>, do not set
332+
/// the extended window styles <see cref="WindowStylesEx.WS_EX_COMPOSITED"/> or <see cref="WindowStylesEx.WS_EX_LAYERED"/>.</para>
333+
/// <para>Calling SetWindowLongPtr to set the style on a progressbar will reset its position.</para>
334+
/// </item>
335+
/// </list>
336+
/// </remarks>
337+
[DllImport(nameof(User32), SetLastError = true)]
338+
public static extern unsafe void* SetWindowLongPtr(IntPtr hWnd, WindowLongIndexFlags nIndex, void* dwNewLong);
339+
262340
[DllImport(nameof(User32), SetLastError = true, CharSet = CharSet.Unicode)]
263341
public static extern int GetWindowLong(IntPtr hWnd, WindowLongIndexFlags nIndex);
264342

0 commit comments

Comments
 (0)