服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#调用Win32的API函数--User32.dll

C#调用Win32的API函数--User32.dll

2022-08-16 09:33Kevin Gao C#

这篇文章主要介绍了C#调用Win32_的API函数--User32.dll,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果。

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using System;
 
using System.Collections.Generic;
 
using System.Linq;
 
using System.Text;
 
using System.Runtime.InteropServices;
 
 
 
namespace WindowsAPI
 
{
 
  class CSharp_Win32Api
 
  {
 
    #region User32.dll 函数
 
    /// <summary>
 
    /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd:设备上下文环境被检索的窗口的句柄
 
    /// </summary>
 
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
 
    public static extern IntPtr GetDC(IntPtr hWnd);
 
    /// <summary>
 
    /// 函数释放设备上下文环境(DC)供其他应用程序使用。
 
    /// </summary>
 
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
 
    /// <summary>
 
    /// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。
 
    /// </summary>
 
    static public extern IntPtr GetDesktopWindow();
 
    /// <summary>
 
    /// 该函数设置指定窗口的显示状态。
 
    /// </summary>
 
    static public extern bool ShowWindow(IntPtr hWnd, short State);
 
    /// <summary>
 
    /// 通过发送重绘消息 WM_PAINT 给目标窗体来更新目标窗体客户区的无效区域。
 
    /// </summary>
 
    static public extern bool UpdateWindow(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
 
    /// </summary>
 
    static public extern bool SetForegroundWindow(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。
 
    /// </summary>
 
    static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);
 
    /// <summary>
 
    /// 打开剪切板
 
    /// </summary>
 
    static public extern bool OpenClipboard(IntPtr hWndNewOwner);
 
    /// <summary>
 
    /// 关闭剪切板
 
    /// </summary>
 
    static public extern bool CloseClipboard();
 
    /// <summary>
 
    /// 打开清空</summary>
 
    static public extern bool EmptyClipboard();
 
    /// <summary>
 
    /// 将存放有数据的内存块放入剪切板的资源管理中
 
    /// </summary>
 
    static public extern IntPtr SetClipboardData(uint Format, IntPtr hData);
 
    /// <summary>
 
    /// 在一个矩形中装载指定菜单条目的屏幕坐标信息
 
    /// </summary>
 
    static public extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, uint Item, ref RECT rc);
 
 
 
    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
 
    /// <summary>
 
    /// 该函数获得一个指定子窗口的父窗口句柄。
 
    /// </summary>
 
    public static extern IntPtr GetParent(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。 
 
    /// </summary>
 
    /// <param name="hWnd">其窗口程序将接收消息的窗口的句柄</param>
 
    /// <param name="msg">指定被发送的消息</param>
 
    /// <param name="wParam">指定附加的消息指定信息</param>
 
    /// <param name="lParam">指定附加的消息指定信息</param>
 
    /// <returns></returns>
 
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);   
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);
 
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);   
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTON lParam);   
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);  
 
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO lParam);  
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TVITEM lParam);   
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref LVITEM lParam); 
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HDITEM lParam); 
 
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HD_HITTESTINFO hti);
 
    /// <summary>
 
    /// 该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里
 
    /// </summary>
 
    public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 
    public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);
 
 
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
 
    public static extern bool UnhookWindowsHookEx(IntPtr hhook);
 
 
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
 
    public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
 
    /// <summary>
 
    /// 该函数对指定的窗口设置键盘焦点。
 
    /// </summary>
 
    public static extern IntPtr SetFocus(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数在指定的矩形里写入格式化文本,根据指定的方法对文本格式化(扩展的制表符,字符对齐、折行等)。
 
    /// </summary>
 
    public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, int uFormat);
 
    /// <summary>
 
    /// 该函数改变指定子窗口的父窗口。
 
    /// </summary>
 
    public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);
 
    /// <summary>
 
    /// 获取对话框中子窗口控件的句柄
 
    /// </summary>
 
    public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);
 
    /// <summary>
 
    /// 该函数获取窗口客户区的坐标。
 
    /// </summary>
 
    public extern static int GetClientRect(IntPtr hWnd, ref RECT rc);
 
    /// <summary>
 
    /// 该函数向指定的窗体添加一个矩形,然后窗口客户区域的这一部分将被重新绘制。
 
    /// </summary>
 
    public extern static int InvalidateRect(IntPtr hWnd, IntPtr rect, int bErase);
 
    /// <summary>
 
    /// 该函数产生对其他线程的控制,如果一个线程没有其他消息在其消息队列里。
 
    /// </summary>
 
    public static extern bool WaitMessage();
 
    /// <summary>
 
    /// 该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构。
 
    /// </summary>
 
    public static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);
 
    /// <summary>
 
    /// 该函数从调用线程的消息队列里取得一个消息并将其放于指定的结构。此函数可取得与指定窗口联系的消息和由PostThreadMesssge寄送的线程消息。此函数接收一定范围的消息值。
 
    /// </summary>
 
    public static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);
 
    /// <summary>
 
    /// 该函数将虚拟键消息转换为字符消息。
 
    /// </summary>
 
    public static extern bool TranslateMessage(ref MSG msg);
 
    /// <summary>
 
    /// 该函数调度一个消息给窗口程序。
 
    /// </summary>
 
    public static extern bool DispatchMessage(ref MSG msg);
 
    /// <summary>
 
    /// 该函数从一个与应用事例相关的可执行文件(EXE文件)中载入指定的光标资源.
 
    /// </summary>
 
    public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);
 
    /// <summary>
 
    /// 该函数确定光标的形状。
 
    /// </summary>
 
    public static extern IntPtr SetCursor(IntPtr hCursor);
 
    /// <summary>
 
    /// 确定当前焦点位于哪个控件上。
 
    /// </summary>
 
    public static extern IntPtr GetFocus();
 
    /// <summary>
 
    /// 该函数从当前线程中的窗口释放鼠标捕获,并恢复通常的鼠标输入处理。捕获鼠标的窗口接收所有的鼠标输入(无论光标的位置在哪里),除非点击鼠标键时,光标热点在另一个线程的窗口中。
 
    /// </summary>
 
    public static extern bool ReleaseCapture();
 
    /// <summary>
 
    /// 准备指定的窗口来重绘并将绘画相关的信息放到一个PAINTSTRUCT结构中。
 
    /// </summary>
 
    public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
 
    /// <summary>
 
    /// 标记指定窗口的绘画过程结束,每次调用BeginPaint函数之后被请求
 
    /// </summary>
 
    public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
 
    /// <summary>
 
    /// 半透明窗体
 
    /// </summary>
 
    public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
 
    /// <summary>
 
    /// 该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
 
    /// </summary>
 
    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
 
    /// <summary>
 
    /// 该函数将指定点的用户坐标转换成屏幕坐标。
 
    /// </summary>
 
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
 
    /// <summary>
 
    /// 当在指定时间内鼠标指针离开或盘旋在一个窗口上时,此函数寄送消息。
 
    /// </summary>
 
    public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);
 
    /// <summary>
 
    ///
 
    /// </summary>
 
    public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
 
    /// <summary>
 
    /// 该函数检取指定虚拟键的状态。
 
    /// </summary>
 
    public static extern ushort GetKeyState(int virtKey);
 
    /// <summary>
 
    /// 该函数改变指定窗口的位置和尺寸。对于顶层窗口,位置和尺寸是相对于屏幕的左上角的:对于子窗口,位置和尺寸是相对于父窗口客户区的左上角坐标的。
 
    /// </summary>
 
    public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
 
    /// <summary>
 
    /// 该函数获得指定窗口所属的类的类名。
 
    /// </summary>
 
    public static extern int GetClassName(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);
 
    /// <summary>
 
    /// 该函数改变指定窗口的属性
 
    /// </summary>
 
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
 
    /// <summary>
 
    /// 该函数检索指定窗口客户区域或整个屏幕的显示设备上下文环境的句柄,在随后的GDI函数中可以使用该句柄在设备上下文环境中绘图。
 
    /// </summary>
 
    public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);
 
    /// <summary>
 
    /// 获取整个窗口(包括边框、滚动条、标题栏、菜单等)的设备场景 返回值 Long。
 
    /// </summary>
 
    public static extern IntPtr GetWindowDC(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。
 
    /// </summary>
 
    public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);
 
    /// <summary>
 
    /// 该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置。
 
    /// </summary>
 
    public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT wp);
 
    /// <summary>
 
    /// 该函数改变指定窗口的标题栏的文本内容
 
    /// </summary>
 
    public static extern int SetWindowText(IntPtr hWnd, string text);
 
    /// <summary>
 
    /// 该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控制,则拷贝控制的文本。
 
    /// </summary>
 
    public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int maxCount);
 
    /// <summary>
 
    /// 用于得到被定义的系统数据或者系统配置信息.
 
    /// </summary>
 
    static public extern int GetSystemMetrics(int nIndex);
 
    /// <summary>
 
    /// 该函数设置滚动条参数,包括滚动位置的最大值和最小值,页面大小,滚动按钮的位置。
 
    /// </summary>
 
    static public extern int SetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si, int fRedraw);
 
    /// <summary>
 
    /// 该函数显示或隐藏所指定的滚动条。
 
    /// </summary>
 
    public static extern int ShowScrollBar(IntPtr hWnd, int bar, int show);
 
    /// <summary>
 
    /// 该函数可以激活一个或两个滚动条箭头或是使其失效。
 
    /// </summary>
 
    public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);
 
    /// <summary>
 
    /// 该函数将指定的窗口设置到Z序的顶部。
 
    /// </summary>
 
    public static extern int BringWindowToTop(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数滚动指定窗体客户区域的目录。
 
    /// </summary>
 
    static public extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy,ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, uint flags);
 
    /// <summary>
 
    /// 该函数确定给定的窗口句柄是否识别一个已存在的窗口。
 
    /// </summary>
 
    public static extern int IsWindow(IntPtr hWnd);
 
    /// <summary>
 
    /// 该函数将256个虚拟键的状态拷贝到指定的缓冲区中。
 
    /// </summary>
 
    public static extern int GetKeyboardState(byte[] pbKeyState);
 
    /// <summary>
 
    /// 该函数将指定的虚拟键码和键盘状态翻译为相应的字符或字符串。该函数使用由给定的键盘布局句柄标识的物理键盘布局和输入语言来翻译代码。
 
    /// </summary>
 
    public static extern int ToAscii(int uVirtKey,int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey,int fuState);
 
    #endregion
 
 
 
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/kevinGao/archive/2012/01/03/2323352.html

延伸 · 阅读

精彩推荐
  • C#C#中backgroundworker的使用教程

    C#中backgroundworker的使用教程

    这篇文章主要介绍了C#中backgroundworker的使用教程,在文中有两点需要注意的,大家可以看下...

    秋漓10112022-02-23
  • C#详解C#如何读写config配置文件

    详解C#如何读写config配置文件

    这篇文章主要介绍了详解C#如何读写config配置文件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    阿凡卢4282022-02-13
  • C#使用C#编写15子游戏

    使用C#编写15子游戏

    这篇文章主要为大家详细介绍了使用C#编写15子游戏的具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    LazyChun5542022-01-04
  • C#protobuf对象二进制序列化存储(详解)

    protobuf对象二进制序列化存储(详解)

    下面小编就为大家带来一篇protobuf对象二进制序列化存储(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网8032021-12-24
  • C#轻松学习C#的哈希表

    轻松学习C#的哈希表

    轻松学习C#的哈希表,对C#的哈希表感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的哈希表。...

    丿木呈广予口贝11212021-11-04
  • C#C#中Override关键字和New关键字的用法详解

    C#中Override关键字和New关键字的用法详解

    这篇文章主要介绍了C#中Override关键字和New关键字的用法,需要的朋友可以参考下...

    C#教程网11382021-11-09
  • C#C#编程中使用设计模式中的原型模式的实例讲解

    C#编程中使用设计模式中的原型模式的实例讲解

    这篇文章主要介绍了C#编程中使用设计模式中的原型模式的实例讲解,原型模式创建新对象方便快捷,而且可在运行时根据需要通过克隆来添加和去除他们,也...

    张龙豪8592021-11-12
  • C#C#获取系统当前IE版本号

    C#获取系统当前IE版本号

    这篇文章主要为大家详细介绍了C#获取系统当前IE版本号,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    唐宋元明清21888112022-03-06