Win32.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:19k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: Win32.cs,v 1.2 2005/04/20 08:45:48 okajima Exp $
  4. */
  5. using System;
  6. using System.Drawing;
  7. using System.Runtime.InteropServices;
  8. namespace Poderosa
  9. {
  10. public class Win32 {
  11. //娭悢
  12. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  13. public static extern IntPtr FindWindowEx(
  14. IntPtr hwndParent,      // handle to parent window
  15. IntPtr hwndChildAfter,  // handle to child window
  16. string lpszClass,    // class name
  17. string lpszWindow    // window name
  18. );
  19. [DllImport("user32.dll", ExactSpelling=false, CharSet=CharSet.Auto)]
  20. public static extern int GetWindowText(IntPtr hwnd, char[] buf, int size);
  21. [DllImport("user32.dll", ExactSpelling=false, CharSet=CharSet.Auto)]
  22. public static extern int GetWindowModuleFileName(IntPtr hwnd, char[] buf, int size);
  23. [DllImport("user32.dll")]
  24. public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  25. [DllImport("user32.dll")]
  26. public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  27. [DllImport("user32.dll")]
  28. public static extern IntPtr GetDesktopWindow();
  29. [DllImport("user32.dll")]
  30. public static extern int DestroyWindow(IntPtr hwnd);
  31. [DllImport("user32.dll")]
  32. public static extern IntPtr LoadIcon(IntPtr hModule, IntPtr iconName);
  33. [DllImport("user32.dll")]
  34. public static extern int GetSystemMetrics(int index);
  35. [DllImport("user32.dll")]
  36. public static extern int GetCaretBlinkTime();
  37. [DllImport("user32.dll")]
  38. public static extern int MessageBeep(int type);
  39. [DllImport("user32.dll")]
  40. public static extern short GetKeyState(int vk);
  41. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  42. public static extern short VkKeyScan(char ch);
  43. [DllImport("user32.dll")]
  44. public static extern bool GetKeyboardState(byte[] data);
  45. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  46. public static extern int RegisterWindowMessage(string lpString);
  47. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  48. public static extern int MessageBox(IntPtr hwnd, string text, string caption, int flags);
  49. [DllImport("user32.dll")]
  50. public static extern bool InvalidateRect(IntPtr hwnd, ref RECT rect, int erase);
  51. [DllImport("user32.dll")]
  52. public static extern bool InvalidateRect(IntPtr hwnd, IntPtr rect, int erase); //for invalidating all
  53. [DllImport("kernel32.dll")]
  54. public static extern int GetLastError();
  55. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  56. public static extern IntPtr CreateEvent(IntPtr lpSecurityAttribute, int manualReset, int initialState, string name);
  57. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  58. public static extern IntPtr CreateMutex(IntPtr lpSecurityAttribute, int initialOwner, string name);
  59. [DllImport("kernel32.dll")]
  60. public static extern bool CloseHandle(IntPtr handle);
  61. [DllImport("kernel32.dll")]
  62. public static extern bool ReleaseMutex(IntPtr handle);
  63. [DllImport("kernel32.dll")]
  64. public static extern int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
  65. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  66. public static extern int SetEnvironmentVariable(string name, string value);
  67. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  68. public static extern int GetEnvironmentVariable(string name, char[] buf, int len);
  69. [DllImport("kernel32.dll")]
  70. public static extern bool GetCommState(IntPtr handle, ref DCB dcb);
  71. [DllImport("kernel32.dll")]
  72. public static extern bool SetCommState(IntPtr handle, ref DCB dcb);
  73. [DllImport("kernel32.dll")]
  74. public static extern bool GetCommTimeouts(IntPtr handle, ref COMMTIMEOUTS timeouts);
  75. [DllImport("kernel32.dll")]
  76. public static extern bool SetCommTimeouts(IntPtr handle, ref COMMTIMEOUTS timeouts);
  77. [DllImport("kernel32.dll")]
  78. public static extern bool SetCommBreak(IntPtr handle);
  79. [DllImport("kernel32.dll")]
  80. public static extern bool ClearCommBreak(IntPtr handle);
  81. [DllImport("kernel32.dll")]
  82. public static extern bool WaitCommEvent(
  83. IntPtr hFile,                // handle to comm device
  84. out int lpEvtMask,           // event type
  85. ref OVERLAPPED lpOverlapped   // overlapped structure
  86. );
  87. [DllImport("kernel32.dll")]
  88. public static extern bool ClearCommError(
  89. IntPtr hFile,     // handle to communications device
  90. out int lpErrors, // error codes
  91. IntPtr lpStat  // communications status (杮摉偼CommStat)
  92. );
  93. [DllImport("kernel32.dll")]
  94. public static extern bool SetCommMask(
  95. IntPtr hFile,                // handle to comm device
  96. int flags
  97. );
  98. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  99. public static extern IntPtr CreateFile(
  100. string filename,
  101. uint dwDesiredAccess,                      // access mode
  102. uint dwShareMode,                          // share mode
  103. IntPtr lpSecurityAttributes, // SD
  104. uint dwCreationDisposition,                // how to create
  105. uint dwFlagsAndAttributes,                 // file attributes
  106. IntPtr hTemplateFile                        // handle to template file
  107. );
  108. [DllImport("kernel32.dll")]
  109. public static extern bool ReadFile(
  110. IntPtr hFile,                // handle to file
  111. byte[] lpBuffer,             // data buffer
  112. int nNumberOfBytesToRead,  // number of bytes to read
  113. ref int lpNumberOfBytesRead, // number of bytes read
  114. ref OVERLAPPED lpOverlapped    // overlapped buffer
  115. );
  116. [DllImport("kernel32.dll")]
  117. public static extern bool WriteFile(
  118. IntPtr hFile,                // handle to file
  119. byte[] lpBuffer,             // data buffer
  120. int nNumberOfBytesToRead,  // number of bytes to read
  121. ref int lpNumberOfBytesRead, // number of bytes read
  122. ref OVERLAPPED lpOverlapped    // overlapped buffer
  123. );
  124. [DllImport("kernel32.dll")]
  125. public static extern bool GetOverlappedResult(
  126. IntPtr hFile,                       // handle to file, pipe, or device
  127. ref OVERLAPPED lpOverlapped,          // overlapped structure
  128. ref int lpNumberOfBytesTransferred, // bytes transferred
  129. bool bWait                          // wait option
  130. );
  131. //昤夋傪僱僀僥傿僽僐乕僪偵
  132. [DllImport("gdi32.dll", CharSet=CharSet.Unicode)]
  133. public static extern unsafe int TextOut(IntPtr hdc, int x, int y, char* text, int length);
  134. [DllImport("gdi32.dll", CharSet=CharSet.Unicode)]
  135. public static extern unsafe int ExtTextOut(IntPtr hdc, int x, int y, int options, RECT* lprc, char* text, int length, int* lpdx);
  136. [DllImport("gdi32.dll")]
  137. public static extern int SetBkMode(IntPtr hDC, int mode);
  138. [DllImport("gdi32.dll", CharSet=CharSet.Unicode)]
  139. public static extern int GetTextExtentPoint32(IntPtr hdc, string text, int length, out SIZE size);
  140. [DllImport("gdi32.dll")]
  141. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  142. [DllImport("gdi32.dll")]
  143. public static extern int DeleteObject(IntPtr hObject);
  144. [DllImport("gdi32.dll")]
  145. public static extern IntPtr CreatePen(int style, int width, uint color);
  146. [DllImport("gdi32.dll")]
  147. public static extern IntPtr CreateSolidBrush(uint color);
  148. [DllImport("gdi32.dll")]
  149. public static extern int SetTextColor(IntPtr hDC, uint color);
  150. [DllImport("gdi32.dll")]
  151. public static extern int SetBkColor(IntPtr hDC, uint color);
  152. [DllImport("user32.dll")] //側偤user32?
  153. public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr brush);
  154. [DllImport("gdi32.dll")]
  155. public static extern int MoveToEx(IntPtr hDC, int x, int y, out POINT prev);
  156. [DllImport("gdi32.dll")]
  157. public static extern int LineTo(IntPtr hDC, int x, int y);
  158. [DllImport("kernel32.dll")]
  159. public static extern bool FlushFileBuffers(IntPtr handle);
  160. [DllImport("kernel32.dll", CharSet=CharSet.Ansi)] //WinExec偼LPCSTR偩偭偨丅"T"偑側偄丅
  161. public static extern int WinExec(string command, int uCmdShow);
  162. [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
  163. public static extern int EnumFontFamiliesEx(
  164. IntPtr hdc,                          // handle to DC
  165. ref tagLOGFONT lpLogfont,              // font information
  166. EnumFontFamExProc lpEnumFontFamExProc, // callback function
  167. IntPtr lParam,                    // additional data
  168. uint  dwFlags                     // not used; must be 0
  169. );
  170. [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
  171. public static extern IntPtr CreateFontIndirect(ref LOGFONT lf);
  172. [DllImport("gdiplus.dll")]
  173. public static extern int GdipCreateFromHDC(IntPtr hdc, ref IntPtr graphics);
  174. [DllImport("gdiplus.dll")]
  175. public static extern int GdipDeleteGraphics(IntPtr graphics);
  176. [DllImport("gdiplus.dll")]
  177. public static extern int GdipSetTextRenderingHint(IntPtr graphics, int mode);
  178. [DllImport("imm32.dll")]
  179. public static extern IntPtr ImmGetContext(IntPtr hWnd);
  180. [DllImport("imm32.dll")]
  181. public static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
  182. [DllImport("imm32.dll", CharSet=CharSet.Auto)]
  183. public static extern bool ImmSetCompositionFont(IntPtr hIMC, LOGFONT lf);
  184. [DllImport("imm32.dll")]
  185. public static extern bool ImmSetCompositionWindow(IntPtr hIMC, ref COMPOSITIONFORM form);
  186. [DllImport("imm32.dll")]
  187. public static extern bool ImmNotifyIME(IntPtr hIMC, int dwAction, int dwIndex, int dwValue);
  188. [DllImport("shell32.dll", CharSet=CharSet.Auto)]
  189. public static extern IntPtr ShellExecute(IntPtr hwnd, string verb, string file, string param, string dir, int cmd);
  190. [DllImport("msvcr71.dll", CallingConvention = CallingConvention.Cdecl)]
  191. public static extern int _controlfp(int n, int mask);
  192. public static void ClearFPUOverflowFlag() {
  193. _controlfp(0x9001f, 0xfffff); //JSPager栤戣偺懳墳丅忣曬偼http://support.microsoft.com/default.aspx?scid=kb;en-us;326219
  194. }
  195. //掕悢
  196. public const int WM_COPYDATA = 0x4A;
  197. public const int WM_NOTIFY = 0x4E;
  198. public const int WM_NCACTIVATE = 0x0086;
  199. public const int WM_CHAR = 0x0102;
  200. public const int WM_USER = 0x400;
  201. public const int WM_VSCROLL = 0x115;
  202. public const int WM_IME_STARTCOMPOSITION =        0x010D;
  203. public const int WM_IME_ENDCOMPOSITION   =        0x010E;
  204. public const int TCN_FIRST = -550;
  205. public const int TCN_SELCHANGING = (TCN_FIRST - 2);
  206. public const int VK_LSHIFT   =      0xA0;
  207. public const int VK_RSHIFT   =      0xA1;
  208. public const int VK_LCONTROL =      0xA2;
  209. public const int VK_RCONTROL =      0xA3;
  210. public const int VK_LMENU    =      0xA4;
  211. public const int VK_RMENU    =      0xA5;
  212. public const uint GENERIC_READ  =                   (0x80000000);
  213. public const uint GENERIC_WRITE =                   (0x40000000);
  214. public const uint OPEN_EXISTING =      3;
  215. public const uint FILE_ATTRIBUTE_NORMAL =              0x00000080;
  216. public const uint FILE_FLAG_OVERLAPPED =           0x40000000;
  217. public const int IDI_APPLICATION =    32512;
  218. public const int IDI_HAND        =    32513;
  219. public const int IDI_QUESTION    =    32514;
  220. public const int IDI_EXCLAMATION =    32515;
  221. public const int IDI_ASTERISK    =    32516;
  222. public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  223. public const int CFS_DEFAULT =                    0x0000;
  224. public const int CFS_RECT =                       0x0001;
  225. public const int CFS_POINT =                      0x0002;
  226. public const int CFS_FORCE_POSITION =             0x0020;
  227. public const int CFS_CANDIDATEPOS =               0x0040;
  228. public const int CFS_EXCLUDE =                    0x0080;
  229. //GetSystemMetrics偵搉偡掕悢丅偲傝偁偊偢昁梫側暘偩偗
  230. public const int SM_CXVSCROLL =           2;
  231. public const int SM_CXEDGE    =           45;
  232. public const int SM_CYEDGE    =           46;
  233. public const int NI_COMPOSITIONSTR =              0x0015;
  234. public const int CPS_CANCEL =                     0x0004;
  235. public const int ERROR_ALREADY_EXISTS = 183;
  236. public const int ERROR_IO_PENDING = 997;
  237. public const int WAIT_OBJECT_0 = 0;
  238. /*
  239. #define SW_HIDE             0
  240. #define SW_SHOWNORMAL       1
  241. #define SW_NORMAL           1
  242. #define SW_SHOWMINIMIZED    2
  243. #define SW_SHOWMAXIMIZED    3
  244. #define SW_MAXIMIZE         3
  245. #define SW_SHOWNOACTIVATE   4
  246. #define SW_SHOW             5
  247. #define SW_MINIMIZE         6
  248. #define SW_SHOWMINNOACTIVE  7
  249. #define SW_SHOWNA           8
  250. #define SW_RESTORE          9
  251. #define SW_SHOWDEFAULT      10
  252. #define SW_FORCEMINIMIZE    11
  253. #define SW_MAX              11
  254. */
  255. //峔憿懱
  256. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  257. public unsafe struct COPYDATASTRUCT {
  258. public uint dwData;
  259. public uint cbData;
  260. public void* lpData;
  261. }
  262. [StructLayout(LayoutKind.Sequential)]
  263. public struct NMHDR { 
  264. public IntPtr hwndFrom; 
  265. public uint idFrom; 
  266. public int code; 
  267. }
  268. [StructLayout(LayoutKind.Sequential)]
  269. public struct DCB { 
  270. public uint DCBlength; 
  271. public uint BaudRate;
  272. public uint Misc;
  273. /*
  274. DWORD fBinary: 1; 
  275. DWORD fParity: 1; 
  276. DWORD fOutxCtsFlow:1; 
  277. DWORD fOutxDsrFlow:1; 
  278. DWORD fDtrControl:2; 
  279. DWORD fDsrSensitivity:1; 
  280. DWORD fTXContinueOnXoff:1; 
  281. DWORD fOutX: 1; 
  282. DWORD fInX: 1; 
  283. DWORD fErrorChar: 1; 
  284. DWORD fNull: 1; 
  285. DWORD fRtsControl:2; 
  286. DWORD fAbortOnError:1; 
  287. DWORD fDummy2:17; 
  288. */
  289. public ushort wReserved; 
  290. public ushort XonLim; 
  291. public ushort XoffLim; 
  292. public byte ByteSize; 
  293. public byte Parity; 
  294. public byte StopBits; 
  295. public byte XonChar; 
  296. public byte XoffChar; 
  297. public byte ErrorChar; 
  298. public byte EofChar; 
  299. public byte EvtChar; 
  300. public ushort wReserved1; 
  301. //Font#ToLogFont偵搉偡偨傔偵偼struct偱偼偩傔偱class偵偟側偄偲偄偐傫
  302. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  303. public class LOGFONT {
  304. public int lfHeight;
  305. public int lfWidth;
  306. public int lfEscapement;
  307. public int lfOrientation;
  308. public int lfWeight;
  309. public byte lfItalic;
  310. public byte lfUnderline;
  311. public byte lfStrikeOut;
  312. public byte lfCharSet;
  313. public byte lfOutPrecision;
  314. public byte lfClipPrecision;
  315. public byte lfQuality;
  316. public byte lfPitchAndFamily;
  317. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
  318. public string lfFaceName;
  319. }
  320. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  321. public struct tagLOGFONT {
  322. public int lfHeight;
  323. public int lfWidth;
  324. public int lfEscapement;
  325. public int lfOrientation;
  326. public int lfWeight;
  327. public byte lfItalic;
  328. public byte lfUnderline;
  329. public byte lfStrikeOut;
  330. public byte lfCharSet;
  331. public byte lfOutPrecision;
  332. public byte lfClipPrecision;
  333. public byte lfQuality;
  334. public byte lfPitchAndFamily;
  335. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
  336. public string lfFaceName;
  337. }
  338. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  339. public struct ENUMLOGFONTEX {
  340. //LOGFONT part
  341. public int lfHeight;
  342. public int lfWidth;
  343. public int lfEscapement;
  344. public int lfOrientation;
  345. public int lfWeight;
  346. public byte lfItalic;
  347. public byte lfUnderline;
  348. public byte lfStrikeOut;
  349. public byte lfCharSet;
  350. public byte lfOutPrecision;
  351. public byte lfClipPrecision;
  352. public byte lfQuality;
  353. public byte lfPitchAndFamily;
  354. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
  355. public string lfFaceName;
  356. //ENUMLOGFONTEX part
  357. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
  358. public string elfFullName;
  359. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
  360. public string elfStyle;
  361. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
  362. public string elfScript;
  363. }
  364. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  365. public struct NEWTEXTMETRIC {
  366. public int   tmHeight; 
  367. public int   tmAscent; 
  368. public int   tmDescent; 
  369. public int   tmInternalLeading; 
  370. public int   tmExternalLeading; 
  371. public int   tmAveCharWidth; 
  372. public int   tmMaxCharWidth; 
  373. public int   tmWeight; 
  374. public int   tmOverhang; 
  375. public int   tmDigitizedAspectX; 
  376. public int   tmDigitizedAspectY; 
  377. public char  tmFirschar; 
  378. public char  tmLaschar; 
  379. public char  tmDefaulchar; 
  380. public char  tmBreakChar; 
  381. public byte   tmItalic; 
  382. public byte   tmUnderlined; 
  383. public byte   tmStruckOut; 
  384. public byte   tmPitchAndFamily; 
  385. public byte   tmCharSet; 
  386. public uint  ntmFlags; 
  387. public uint   ntmSizeEM; 
  388. public uint   ntmCellHeight; 
  389. public uint   ntmAvgWidth; 
  390. }
  391. [StructLayout(LayoutKind.Sequential)]
  392. public struct FONTSIGNATURE {
  393. [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
  394. public uint[]  fsUsb;
  395. [MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
  396. public uint[]  fsCsb;
  397. }
  398. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  399. public struct NEWTEXTMETRICEX {
  400. public NEWTEXTMETRIC ntmTm;
  401. public FONTSIGNATURE ntmFontSig;
  402. }
  403. [StructLayout(LayoutKind.Sequential)]
  404. public struct COMMTIMEOUTS {  
  405. public uint ReadIntervalTimeout; 
  406. public uint ReadTotalTimeoutMultiplier; 
  407. public uint ReadTotalTimeoutConstant; 
  408. public uint WriteTotalTimeoutMultiplier; 
  409. public uint WriteTotalTimeoutConstant; 
  410. }
  411. [StructLayout(LayoutKind.Sequential)]
  412. public struct POINT {
  413. public int x;
  414. public int y;
  415. }
  416. [StructLayout(LayoutKind.Sequential)]
  417. public struct SIZE {
  418. public int width;
  419. public int height;
  420. }
  421. [StructLayout(LayoutKind.Sequential)]
  422. public struct RECT {
  423. public int left;
  424. public int top;
  425. public int right;
  426. public int bottom;
  427. }
  428. public static RECT CreateRect(ref Rectangle rc) {
  429. RECT r = new RECT();
  430. r.left = rc.Left;
  431. r.right = rc.Right;
  432. r.top = rc.Top;
  433. r.bottom = rc.Bottom;
  434. return r;
  435. }
  436. [StructLayout(LayoutKind.Sequential)]
  437. public struct COMPOSITIONFORM {
  438. public int dwStyle;
  439. public POINT ptCurrentPos;
  440. public RECT  rcArea;
  441. }
  442. [StructLayout(LayoutKind.Sequential)]
  443. public struct OVERLAPPED { 
  444. public int  Internal; 
  445. public int  InternalHigh; 
  446. public int  Offset; 
  447. public int  OffsetHigh; 
  448. public IntPtr hEvent; 
  449. }
  450. //callbacks
  451. public delegate int EnumFontFamExProc(ref ENUMLOGFONTEX lpelfe,    // logical-font data
  452.  ref NEWTEXTMETRICEX lpntme,  // physical-font data
  453.  uint FontType,           // type of font
  454.  IntPtr lParam             // application-defined data
  455.  );
  456. internal class SystemMetrics {
  457. private int _vScrollBarWidth;
  458. private int _controlBorderWidth;
  459. private int _controlBorderHeight;
  460. public SystemMetrics() {
  461. _vScrollBarWidth     = Win32.GetSystemMetrics(Win32.SM_CXVSCROLL);
  462. _controlBorderWidth  = Win32.GetSystemMetrics(Win32.SM_CXEDGE);
  463. _controlBorderHeight = Win32.GetSystemMetrics(Win32.SM_CYEDGE);
  464. }
  465. public int ScrollBarWidth {
  466. get {
  467. return _vScrollBarWidth;
  468. }
  469. }
  470. public int ControlBorderWidth {
  471. get {
  472. return _controlBorderWidth;
  473. }
  474. }
  475. public int ControlBorderHeight {
  476. get {
  477. return _controlBorderHeight;
  478. }
  479. }
  480. }
  481. }
  482. }