如何将c++代码转换成c#

本文关键字:转换 代码 c++ | 更新日期: 2023-09-27 18:06:22

我在 c++ 中有以下代码(不是完整代码),并希望转换为 c# 。我想只使用user32.dll

c++

HBITMAP image::takeImage(HWND handle, int x, int y, int w, int h, LPCWSTR fname ){
    HDC hdcs = GetWindowDC(handle);
    HDC hdcm = CreateCompatibleDC(hdcs);
    HDC hdcp = CreateCompatibleDC(hdcs);
    RECT rect;
    GetWindowRect(handle, &rect);
    hBitmap = CreateCompatibleBitmap( hdcs, w, h);
    hBitmap2 = CreateCompatibleBitmap(hdcs, rect.right - rect.left, rect.bottom - rect.top);
    HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcm, hBitmap);      
    int error = GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
    bool ret = PrintWindow(handle, hdcp, NULL);
    BitBlt(hdcm, 0, 0, w, h, hdcp, x, y, SRCCOPY);
    return ""; //something
}

我在 c# 中寻找等效的 c++ 函数。

GetWindowDC -能够找到这个函数在这里

[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);

CreateCompatibleDC
GetWindowRect
CreateCompatibleBitmap
SelectObject
GetObject
PrintWindow
BitBlt

我从来没有学过c++。只是寻找等效的函数名称或文档链接。谢谢。

如何将c++代码转换成c#

这是你需要的吗?

gdi32.dll

[DllImport("gdi32.dll",EntryPoint="DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll",EntryPoint="DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll",EntryPoint="BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest,int xDest,
    int yDest,int wDest,int hDest,IntPtr hdcSource,
    int xSrc,int ySrc,int RasterOp);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
    int nWidth, int nHeight);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport ("gdi32.dll",EntryPoint="SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);

user32.dll

[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll",EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("user32.dll",EntryPoint="GetWindowDC")]
public static extern IntPtr GetWindowDC(Int32 ptr);
[DllImport("user32.dll",EntryPoint="ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);

您可以使用本网站进行搜索。下面是我的发现:

[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr h);
[DllImport("gdi32.dll")]
static extern int GetObject(IntPtr hgdiobj, int cbBuffer, IntPtr lpvObject);
[DllImport("user32.dll")]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, 
       int nXSrc, int nYSrc, long dwRop);

Struct RECT是这样定义的(复制自http://www.pinvoke.net/default.aspx/Structures/RECT.html)

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
   public int Left, Top, Right, Bottom;
   public RECT(int left, int top, int right, int bottom)
   {
     Left = left;
     Top = top;
     Right = right;
     Bottom = bottom;
   }
   public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
   public int X
   {
     get { return Left; }
     set { Right -= (Left - value); Left = value; }
   }
   public int Y
   {
     get { return Top; }
     set { Bottom -= (Top - value); Top = value; }
   }
   public int Height
   {
     get { return Bottom - Top; }
     set { Bottom = value + Top; }
   }
   public int Width
   {
     get { return Right - Left; }
     set { Right = value + Left; }
   }
   public System.Drawing.Point Location
   {
     get { return new System.Drawing.Point(Left, Top); }
     set { X = value.X; Y = value.Y; }
   }
   public System.Drawing.Size Size
   {
     get { return new System.Drawing.Size(Width, Height); }
     set { Width = value.Width; Height = value.Height; }
   }
   public static implicit operator System.Drawing.Rectangle(RECT r)
   {
     return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
   }
   public static implicit operator RECT(System.Drawing.Rectangle r)
   {
     return new RECT(r);
   }
   public static bool operator ==(RECT r1, RECT r2)
   {
     return r1.Equals(r2);
   }
   public static bool operator !=(RECT r1, RECT r2)
   {
     return !r1.Equals(r2);
   }
   public bool Equals(RECT r)
   {
     return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
   }
   public override bool Equals(object obj)
   {
     if (obj is RECT)
       return Equals((RECT)obj);
     else if (obj is System.Drawing.Rectangle)
       return Equals(new RECT((System.Drawing.Rectangle)obj));
     return false;
   }
   public override int GetHashCode()
   {
     return ((System.Drawing.Rectangle)this).GetHashCode();
   }
   public override string ToString()
   {
     return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
   }
}