1 C#中对 API函数的调用
2
3
4 using System;
5 using System.Runtime.InteropServices;
6
7GDI32#region GDI32
8 [DllImport("gdi32.dll",EntryPoint="DeleteDC")]
9 public static extern IntPtr DeleteDC(IntPtr hDc);
10
11 [DllImport("gdi32.dll",EntryPoint="DeleteObject")]
12 public static extern IntPtr DeleteObject(IntPtr hDc);
13
14 [DllImport("gdi32.dll",EntryPoint="BitBlt")]
15 public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);
16
17 [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
18 public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
19
20 [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
21 public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
22
23 [DllImport ("gdi32.dll",EntryPoint="SelectObject")]
24 public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);
25 [DllImport("gdi32.dll")]
26 public static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
27
28 #endregion
29
30USER32#region USER32
31 [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
32 public static extern IntPtr GetDesktopWindow();
33
34 [DllImport("user32.dll",EntryPoint="GetDC")]
35 public static extern IntPtr GetDC(IntPtr ptr);
36
37 [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
38 public static extern int GetSystemMetrics(int abc);
39
40 [DllImport("user32.dll",EntryPoint="GetWindowDC")]
41 public static extern IntPtr GetWindowDC(Int32 ptr);
42
43 [DllImport("user32.dll",EntryPoint="ReleaseDC")]
44 public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);
45
46 #endregion
47
48public struct SIZE
49 {
50 public int x;
51 public int y;
52 }
53
54 public const int SM_CXSCREEN=0;
55 public const int SM_CYSCREEN=1;
56 public const int SM_CXMAXIMIZED=3;
57 public const int SM_CYMAXIMIZED=4;
58 private const int PHYSICALOFFSETX = 112;
59 private const int PHYSICALOFFSETY = 113;
60 public const int SRCCOPY = 13369376;
61 public int HardMarginLeft;
62 public int HardMarginTop;
63
64 public int WindowsWidth=0;
65 public int WindowsHeight=0;
66
67//获取所需的数据
68private void InvokeApiFunction()
69{
70 SIZE size;
71 IntPtr hDC = GetDC(GetDesktopWindow());
72 IntPtr hMemDC =CreateCompatibleDC(hDC);
73 size.cx = GetSystemMetrics(SM_CXSCREEN);
74 size.cy = GetSystemMetrics(SM_CYSCREEN);
75 this.WindowsHeight=GetSystemMetrics(SM_CYMAXIMIZED ) ;
76 this.WindowsWidth =GetSystemMetrics(SM_CXMAXIMIZED ) ;
77 HardMarginLeft = GetDeviceCaps(hDC , PHYSICALOFFSETX);
78 HardMarginTop = GetDeviceCaps(hDC , PHYSICALOFFSETY);
79 hBitmap = CreateCompatibleBitmap(hDC, size.x, size.y);
80 if (hBitmap!=IntPtr.Zero)
81 {
82 IntPtr hOld = (IntPtr) SelectObject(hMemDC, hBitmap);
83 BitBlt(hMemDC, 0, 0,size.x,size.y, hDC, 0, 0, SRCCOPY);
84 SelectObject(hMemDC, hOld);
85 DeleteDC(hMemDC);
86 ReleaseDC(GetDesktopWindow(), hDC);
87 Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
88 DeleteObject(hBitmap);
89 GC.Collect();
90 }
91}
92 附:
GetSystemMetrics的相关参数说明:
http://vbworld.sxnw.gov.cn/vbapi/detail/GetSystemMetrics.htm