1
// ------------------------------------------------------------------
2 // Copyright (C) 2015-2016 Maruko Toolbox Project
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 // express or implied.
14 // See the License for the specific language governing permissions
15 // and limitations under the License.
16 // -------------------------------------------------------------------
20 using System
.Collections
.Generic
;
22 using System
.Runtime
.InteropServices
;
30 /// Class hosting Native Win32 APIs
32 public partial class NativeMethods
37 /// Specifies the type of scroll bar.
39 public enum ScrollBarConsts
: int
42 /// Window's standard horizontal scroll bar.
46 /// Window's standard vertical scroll bar.
50 /// Retrieves the position of the scroll box in a scroll bar control.
51 /// The hWnd parameter must be the handle to the scroll bar control.
55 /// Window's standard scroll bar.
61 /// Specifies the commands to scroll bar.
63 public enum ScrollBarCmds
: int
66 /// Scrolls one line down.
70 /// Scrolls left by one unit.
74 /// Scrolls one line up.
78 /// Scrolls right by one unit.
82 /// Scrolls one page up.
86 /// Scrolls left by the width of the window.
90 /// Scrolls one page down.
94 /// Scrolls right by the width of the window.
98 /// The user has dragged the scroll box (thumb) and released the mouse button.
99 /// The HIWORD indicates the position of the scroll box at the end of the drag operation.
101 SB_THUMBPOSITION
= 4,
103 /// The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button.
104 /// The HIWORD indicates the position that the scroll box has been dragged to.
108 /// Scrolls to the upper left.
112 /// Scrolls to the upper left.
116 /// Scrolls to the lower right.
120 /// Scrolls to the lower right.
130 /// Indicate the parameters to set or get.
132 public enum ScrollBarFlags
: uint
135 /// The nMin and nMax members contain the minimum and maximum values for the scrolling range.
139 /// The nPage member contains the page size for a proportional scroll bar.
143 /// The nPos member contains the scroll box position, which is not updated while the user drags the scroll box.
147 /// This value is used only when setting a scroll bar's parameters.
148 /// If the scroll bar's new parameters make the scroll bar unnecessary, disable the scroll bar instead of removing it.
150 SIF_DISABLENOSCROLL
= 0x8,
152 /// The nTrackPos member contains the current position of the scroll box while the user is dragging it.
156 /// Combination of SIF_PAGE, SIF_POS, SIF_RANGE, and SIF_TRACKPOS.
158 SIF_ALL
= (SIF_RANGE
| SIF_PAGE
| SIF_POS
| SIF_TRACKPOS
)
162 /// The flash status.
164 public enum FlashWindowFlags
: uint
167 /// Stop flashing. The system restores the window to its original state.
171 /// Flash the window caption.
173 FLASHW_CAPTION
= 0x1,
175 /// Flash the taskbar button.
179 /// Flash both the window caption and taskbar button.
180 /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
182 FLASHW_ALL
= (FLASHW_CAPTION
| FLASHW_TRAY
),
184 /// Flash continuously, until the FLASHW_STOP flag is set.
188 /// Flash continuously until the window comes to the foreground.
190 FLASHW_TIMERNOFG
= 0xC
196 public enum Win32Msgs
: uint
199 /// The WM_HSCROLL message is sent to a window when a scroll event occurs in the window's standard horizontal scroll bar.
200 /// This message is also sent to the owner of a horizontal scroll bar control when a scroll event occurs in the control.
204 /// The WM_VSCROLL message is sent to a window when a scroll event occurs in the window's standard vertical scroll bar.
205 /// This message is also sent to the owner of a vertical scroll bar control when a scroll event occurs in the control.
209 /// The WM_CTLCOLORSCROLLBAR message is sent to the parent window of a scroll bar control when the control is about to be drawn.
210 /// By responding to this message, the parent window can use the display context handle to set the background color of the scroll bar control.
212 WM_CTLCOLORSCROLLBAR
= 0x137,
214 /// Used to define private messages for use by private window classes, usually of the form WM_USER+x, where x is an integer value.
220 /// Contains scroll bar parameters to be set by the SetScrollInfo function, or retrieved by the GetScrollInfo function.
221 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/bb787537(v=vs.85).aspx</para>
223 [StructLayout(LayoutKind
.Sequential
)]
224 public struct SCROLLINFO
227 /// Specifies the size, in bytes, of this structure.
231 /// Specifies the scroll bar parameters to set or retrieve.
233 public ScrollBarFlags fMask
;
235 /// Specifies the minimum scrolling position.
239 /// Specifies the maximum scrolling position.
243 /// Specifies the page size, in device units.
244 /// A scroll bar uses this value to determine the appropriate size of the proportional scroll box.
248 /// Specifies the position of the scroll box.
252 /// Specifies the immediate position of a scroll box that the user is dragging.
253 /// An application cannot set the immediate scroll position; the SetScrollInfo function ignores this member.
255 public int nTrackPos
;
259 /// Contains the flash status for a window and the number of times the system should flash the window.
260 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms679348(v=vs.85).aspx</para>
262 [StructLayout(LayoutKind
.Sequential
)]
263 public struct FLASHWINFO
266 /// The size of the structure, in bytes.
270 /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
274 /// The Flash Status.
276 public FlashWindowFlags dwFlags
;
278 /// The number of times to Flash the window.
282 /// The rate at which the Window is to be flashed, in milliseconds.
283 /// If Zero, the function uses the default cursor blink rate.
285 public uint dwTimeout
;
289 /// Creates a value for use as a wParam parameter in a message.
290 /// The macro concatenates the specified values.
291 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms632664(v=vs.85).aspx</para>
293 /// <param name="wLow">The low-order word of the new value.</param>
294 /// <param name="wHi">The high-order word of the new value.</param>
295 /// <returns>The return value is a WPARAM value.</returns>
296 public static UIntPtr
MakeWParam(uint wLow
, uint wHi
)
298 return (UIntPtr
)MakeLong(wLow
, wHi
);
302 /// Creates a value for use as an lParam parameter in a message.
303 /// The macro concatenates the specified values.
304 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms632661(v=vs.85).aspx</para>
306 /// <param name="wLow">The low-order word of the new value.</param>
307 /// <param name="wHi">The high-order word of the new value. </param>
308 /// <returns>The return value is an LPARAM value. </returns>
309 public static IntPtr
MakeLParam(uint wLow
, uint wHi
)
311 return (IntPtr
)MakeLong(wLow
, wHi
);
319 /// Retrieves the high-order word from the specified 32-bit value.
320 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms632657(v=vs.85).aspx</para>
322 /// <param name="ptr">The value to be converted.</param>
323 /// <returns>The return value is the high-order word of the specified value.</returns>
324 public static uint HiWord(IntPtr ptr
)
326 return ((uint)ptr
>> 16) & 0xFFFFu
;
330 /// Retrieves the low-order word from the specified value.
331 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms632659(v=vs.85).aspx</para>
333 /// <param name="ptr">The value to be converted.</param>
334 /// <returns>The return value is the low-order word of the specified value.</returns>
335 public static uint LoWord(IntPtr ptr
)
337 return (uint)ptr
& 0xFFFFu
;
341 /// Creates a LONG value by concatenating the specified values.
342 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms632660(v=vs.85).aspx</para>
344 /// <param name="wLow">The low-order word of the new value.</param>
345 /// <param name="wHi">The high-order word of the new value.</param>
346 /// <returns>The return value is a LONG value.</returns>
347 public static uint MakeLong(uint wLow
, uint wHi
)
349 return (wLow
& 0xFFFFu
) | ((wHi
& 0xFFFFu
) << 16);
357 /// Windows Common Controls Messages
359 public enum Win32CommCtrlMsgs
: uint
362 /// Edit control messages
367 /// Set the cue banner with the lParm = LPCWSTR
369 EM_SETCUEBANNER
= ECM_FIRST
+ 1,
372 /// Sets the state of the progress bar.
374 PBM_SETSTATE
= Win32Msgs
.WM_USER
+ 16
378 /// ProgressBar States
380 public enum ProgressBarState
: uint
399 /// The GetScrollInfo function retrieves the parameters of a scroll bar,
400 /// including the minimum and maximum scrolling positions, the page size,
401 /// and the position of the scroll box (thumb).
402 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/bb787583(v=vs.85).aspx</para>
404 /// <param name="hWnd">Handle to a scroll bar control or a window with a standard scroll bar,
405 /// depending on the value of the fnBar parameter. </param>
406 /// <param name="fnBar">Specifies the type of scroll bar for which to retrieve parameters.</param>
407 /// <param name="lpsi">Pointer to a SCROLLINFO structure. Before calling GetScrollInfo, set the cbSize member to sizeof(SCROLLINFO),
408 /// and set the fMask member to specify the scroll bar parameters to retrieve.
409 /// Before returning, the function copies the specified parameters to the appropriate members of the structure.</param>
410 /// <returns>If the function retrieved any values, the return value is nonzero.</returns>
411 [DllImport("user32.dll")]
412 [return: MarshalAs(UnmanagedType
.Bool
)]
413 public static extern bool GetScrollInfo(
415 ScrollBarConsts fnBar
,
416 ref SCROLLINFO lpsi
);
419 /// The SetScrollInfo function sets the parameters of a scroll bar, including the minimum and maximum scrolling positions,
420 /// the page size, and the position of the scroll box (thumb). The function also redraws the scroll bar, if requested.
421 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/bb787595(v=vs.85).aspx</para>
423 /// <param name="hWnd">Handle to a scroll bar control or a window with a standard scroll bar,
424 /// depending on the value of the fnBar parameter. </param>
425 /// <param name="fnBar">Specifies the type of scroll bar for which to set parameters.</param>
426 /// <param name="lpsi">Pointer to a SCROLLINFO structure. Before calling SetScrollInfo, set the cbSize member of the structure to sizeof(SCROLLINFO),
427 /// set the fMask member to indicate the parameters to set, and specify the new parameter values in the appropriate members.</param>
428 /// <param name="fRedraw">Specifies whether the scroll bar is redrawn to reflect the changes to the scroll bar.
429 /// If this parameter is TRUE, the scroll bar is redrawn, otherwise, it is not redrawn. </param>
430 /// <returns>The current position of the scroll box.</returns>
431 [DllImport("user32.dll")]
432 public static extern int SetScrollInfo(
434 ScrollBarConsts fnBar
,
436 [MarshalAs(UnmanagedType
.Bool
)] bool fRedraw
);
439 /// Flashes the specified window. It does not change the active state of the window.
440 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms679347(v=vs.85).aspx</para>
442 /// <param name="pwfi">A pointer to a FLASHWINFO structure.</param>
443 /// <returns>The return value specifies the window's state before the call to the FlashWindowEx function.
444 /// If the window caption was drawn as active before the call, the return value is nonzero. Otherwise, the return value is zero.</returns>
445 [DllImport("user32.dll")]
446 public static extern bool FlashWindowEx(ref FLASHWINFO pwfi
);
449 /// Places (posts) a message in the message queue associated with the thread that created
450 /// the specified window and returns without waiting for the thread to process the message.
451 /// <para>http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx</para>
453 /// <param name="hWnd">A handle to the window whose window procedure is to receive the message.</param>
454 /// <param name="Msg">The message to be posted.</param>
455 /// <param name="wParam">Additional message-specific information.</param>
456 /// <param name="lParam">Additional message-specific information.</param>
457 /// <returns>If the function succeeds, the return value is nonzero.</returns>
458 [DllImport("user32.dll")]
459 [return: MarshalAs(UnmanagedType
.Bool
)]
460 public static extern bool PostMessage(
463 [MarshalAs(UnmanagedType
.SysUInt
)] UIntPtr wParam
,
464 [MarshalAs(UnmanagedType
.SysInt
)] IntPtr lParam
);
467 /// Another overload for PostMessage.
469 [DllImport("user32.dll")]
470 [return: MarshalAs(UnmanagedType
.Bool
)]
471 public static extern bool PostMessage(
473 Win32CommCtrlMsgs Msg
,
474 [MarshalAs(UnmanagedType
.SysUInt
)] UIntPtr wParam
,
475 [MarshalAs(UnmanagedType
.SysInt
)] IntPtr lParam
);
478 /// Sets the text that is displayed as the textual cue, or tip, for an edit control.
479 /// <para>https://msdn.microsoft.com/en-us/library/windows/desktop/bb761701%28v=vs.85%29.aspx</para>
481 /// <param name="hWnd">A handle to the edit control.</param>
482 /// <param name="lpcwText">A pointer to a Unicode string that contains the text to set as the textual cue.</param>
483 /// <returns>If the method succeeds, it returns true. Otherwise it returns false.</returns>
484 public static bool Edit_SetCueBannerText(IntPtr hWnd
, string lpcwText
)
486 var lp
= Marshal
.StringToHGlobalAnsi(lpcwText
);
487 var result
= PostMessage(hWnd
, Win32CommCtrlMsgs
.EM_SETCUEBANNER
,
488 new UIntPtr(0), Marshal
.StringToHGlobalUni(lpcwText
));
489 Marshal
.FreeHGlobal(lp
);