Win32.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
  2. using System;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Kbg.NppPluginNET.PluginInfrastructure
  7. {
  8. public class Win32
  9. {
  10. /// <summary>
  11. /// Get the scroll information of a scroll bar or window with scroll bar
  12. /// @see https://msdn.microsoft.com/en-us/library/windows/desktop/bb787537(v=vs.85).aspx
  13. /// </summary>
  14. [StructLayout(LayoutKind.Sequential)]
  15. public struct ScrollInfo
  16. {
  17. /// <summary>
  18. /// Specifies the size, in bytes, of this structure. The caller must set this to sizeof(SCROLLINFO).
  19. /// </summary>
  20. public uint cbSize;
  21. /// <summary>
  22. /// Specifies the scroll bar parameters to set or retrieve.
  23. /// @see ScrollInfoMask
  24. /// </summary>
  25. public uint fMask;
  26. /// <summary>
  27. /// Specifies the minimum scrolling position.
  28. /// </summary>
  29. public int nMin;
  30. /// <summary>
  31. /// Specifies the maximum scrolling position.
  32. /// </summary>
  33. public int nMax;
  34. /// <summary>
  35. /// Specifies the page size, in device units. A scroll bar uses this value to determine the appropriate size of the proportional scroll box.
  36. /// </summary>
  37. public uint nPage;
  38. /// <summary>
  39. /// Specifies the position of the scroll box.
  40. /// </summary>
  41. public int nPos;
  42. /// <summary>
  43. /// Specifies the immediate position of a scroll box that the user is dragging.
  44. /// An application can retrieve this value while processing the SB_THUMBTRACK request code.
  45. /// An application cannot set the immediate scroll position; the SetScrollInfo function ignores this member.
  46. /// </summary>
  47. public int nTrackPos;
  48. }
  49. /// <summary>
  50. /// Used for the ScrollInfo fMask
  51. /// SIF_ALL => Combination of SIF_PAGE, SIF_POS, SIF_RANGE, and SIF_TRACKPOS.
  52. /// SIF_DISABLENOSCROLL => This value is used only when setting a scroll bar's parameters. If the scroll bar's new parameters make the scroll bar unnecessary, disable the scroll bar instead of removing it.
  53. /// SIF_PAGE => The nPage member contains the page size for a proportional scroll bar.
  54. /// SIF_POS => The nPos member contains the scroll box position, which is not updated while the user drags the scroll box.
  55. /// SIF_RANGE => The nMin and nMax members contain the minimum and maximum values for the scrolling range.
  56. /// SIF_TRACKPOS => The nTrackPos member contains the current position of the scroll box while the user is dragging it.
  57. /// </summary>
  58. public enum ScrollInfoMask
  59. {
  60. SIF_RANGE = 0x1,
  61. SIF_PAGE = 0x2,
  62. SIF_POS = 0x4,
  63. SIF_DISABLENOSCROLL = 0x8,
  64. SIF_TRACKPOS = 0x10,
  65. SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
  66. }
  67. /// <summary>
  68. /// Used for the GetScrollInfo() nBar parameter
  69. /// </summary>
  70. public enum ScrollInfoBar
  71. {
  72. SB_HORZ = 0,
  73. SB_VERT = 1,
  74. SB_CTL = 2,
  75. SB_BOTH = 3
  76. }
  77. /// <summary>
  78. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  79. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  80. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  81. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  82. /// </summary>
  83. [DllImport("user32")]
  84. public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
  85. /// <summary>
  86. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  87. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  88. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  89. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  90. /// </summary>
  91. [DllImport("user32")]
  92. public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam);
  93. /// <summary>
  94. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  95. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  96. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  97. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  98. /// </summary>
  99. [DllImport("user32")]
  100. public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
  101. /// <summary>
  102. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  103. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  104. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  105. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  106. /// </summary>
  107. [DllImport("user32")]
  108. public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, out IntPtr lParam);
  109. /// <summary>
  110. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  111. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  112. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  113. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  114. /// </summary>
  115. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, NppMenuCmd lParam)
  116. {
  117. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), new IntPtr((uint)lParam));
  118. }
  119. /// <summary>
  120. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  121. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  122. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  123. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  124. /// </summary>
  125. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam)
  126. {
  127. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  128. }
  129. /// <summary>
  130. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  131. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  132. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  133. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  134. /// </summary>
  135. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam)
  136. {
  137. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), new IntPtr(lParam));
  138. }
  139. /// <summary>
  140. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  141. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  142. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  143. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  144. /// </summary>
  145. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, out int lParam)
  146. {
  147. IntPtr outVal;
  148. IntPtr retval = SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), out outVal);
  149. lParam = outVal.ToInt32();
  150. return retval;
  151. }
  152. /// <summary>
  153. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  154. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  155. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  156. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  157. /// </summary>
  158. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, int lParam)
  159. {
  160. return SendMessage(hWnd, (UInt32)Msg, wParam, new IntPtr(lParam));
  161. }
  162. /// <summary>
  163. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  164. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  165. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  166. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  167. /// </summary>
  168. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam)
  169. {
  170. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  171. }
  172. /// <summary>
  173. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  174. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  175. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  176. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  177. /// </summary>
  178. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam)
  179. {
  180. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  181. }
  182. /// <summary>
  183. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  184. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  185. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  186. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  187. /// </summary>
  188. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, IntPtr wParam, int lParam)
  189. {
  190. return SendMessage(hWnd, (UInt32)Msg, wParam, new IntPtr(lParam));
  191. }
  192. /// <summary>
  193. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  194. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  195. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  196. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  197. /// </summary>
  198. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, int wParam, IntPtr lParam)
  199. {
  200. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  201. }
  202. /// <summary>
  203. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  204. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  205. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  206. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  207. /// </summary>
  208. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, int wParam, string lParam)
  209. {
  210. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  211. }
  212. /// <summary>
  213. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  214. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  215. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  216. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  217. /// </summary>
  218. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam)
  219. {
  220. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), lParam);
  221. }
  222. /// <summary>
  223. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  224. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  225. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  226. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  227. /// </summary>
  228. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, int wParam, int lParam)
  229. {
  230. return SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), new IntPtr(lParam));
  231. }
  232. /// <summary>
  233. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  234. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  235. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  236. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  237. /// </summary>
  238. public static IntPtr SendMessage(IntPtr hWnd, SciMsg Msg, IntPtr wParam, IntPtr lParam)
  239. {
  240. return SendMessage(hWnd, (UInt32)Msg, wParam, lParam);
  241. }
  242. /// <summary>
  243. /// You should try to avoid calling this method in your plugin code. Rather use one of the gateways such as
  244. /// <see cref="ScintillaGateway"/> or <see cref="NotepadPPGateway"/>.
  245. /// If gateways are missing or incomplete, please help extend them and send your code to the project
  246. /// at https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
  247. /// </summary>
  248. public static IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, ref LangType lParam)
  249. {
  250. IntPtr outVal;
  251. IntPtr retval = SendMessage(hWnd, (UInt32)Msg, new IntPtr(wParam), out outVal);
  252. lParam = (LangType)outVal;
  253. return retval;
  254. }
  255. public const int MAX_PATH = 260;
  256. [DllImport("kernel32")]
  257. public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
  258. [DllImport("kernel32")]
  259. public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
  260. [DllImport("kernel32")]
  261. public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
  262. public const int MF_BYCOMMAND = 0;
  263. public const int MF_CHECKED = 8;
  264. public const int MF_UNCHECKED = 0;
  265. [DllImport("user32")]
  266. public static extern IntPtr GetMenu(IntPtr hWnd);
  267. [DllImport("user32")]
  268. public static extern int CheckMenuItem(IntPtr hmenu, int uIDCheckItem, int uCheck);
  269. public const int WM_CREATE = 1;
  270. [DllImport("user32")]
  271. public static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
  272. [DllImport("kernel32")]
  273. public static extern void OutputDebugString(string lpOutputString);
  274. /// <summary>
  275. /// @see https://msdn.microsoft.com/en-us/library/windows/desktop/bb787583(v=vs.85).aspx
  276. /// </summary>
  277. /// <param name="hwnd"></param>
  278. /// <param name="nBar"></param>
  279. /// <param name="scrollInfo"></param>
  280. /// <returns></returns>
  281. [DllImport("user32")]
  282. public static extern int GetScrollInfo(IntPtr hwnd, int nBar, ref ScrollInfo scrollInfo);
  283. }
  284. }