像.NET DrawEllipse一样使用GDI32 Ellipse

时间:2011-10-25 18:58:29

标签: c# .net winapi graphics gdi

在C#中,我可以使用.NET System.Graphics.DrawEllipse方法在Windows Calculator(calc.exe)窗口中的屏幕设备上下文中绘制一个椭圆。

我希望能够使用GDI32 Ellipse方法做同样的事情。如何将Ellipse绘制到屏幕上?

在以下代码中,此行有效: CalculatorGraphics.DrawEllipse(penRed,50,50,50,50); 但这条线不是: PlatformInvokeGDI32.Ellipse(hDC,100,100,100,100); 有什么问题?

        //In size variable we shall keep the size of the window.
        SIZE size;

        //Win32 API functions are imported in classes
        //PlatformInvokeGDI32
        //PlatformInvokeUSER32.cs

        //Get handle of calc.exe window.
        IntPtr hwnd = PlatformInvokeUSER32.FindWindow("SciCalc", "Calculator");

        //Get window dimensions
        PlatformInvokeUSER32.RECT rect;
        PlatformInvokeUSER32.GetWindowRect(hwnd, out rect);
        size.cx = rect._Right - rect._Left;
        size.cy = rect._Bottom - rect._Top;

        //Get the device context of Calculator.
        IntPtr hDC = PlatformInvokeUSER32.GetDC(hwnd);

        //Draw on the Calculator surface.
        Graphics CalculatorGraphics = Graphics.FromHdc(hDC);
        Color colorRed = Color.FromName("Red");
        Pen penRed = new Pen(colorRed);
        CalculatorGraphics.DrawEllipse(penRed, 50, 50, 50, 50);
        CalculatorGraphics.Save();

        PlatformInvokeGDI32.COLORREF cl;
        cl.R = 255;
        cl.G = 0;
        cl.B = 0;

        PlatformInvokeGDI32.SetDCBrushColor(hDC, cl);
        PlatformInvokeGDI32.SetDCPenColor(hDC, cl);
        //PlatformInvokeGDI32.SetBkColor(hDC, cl);
        PlatformInvokeGDI32.Ellipse(hDC, 100, 100, 100, 100);
        PlatformInvokeGDI32.SaveDC(hDC);


        //Here we make a compatible device context in memory for screen device context.
        IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);


        //Create a compatible bitmap of window size and using screen device context.
        m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);

        //As m_HBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used.
        if (m_HBitmap != IntPtr.Zero)
        {
            //Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap.
            IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, m_HBitmap);
            //We copy the Bitmap to the memory device context.
            PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
            //We select the old bitmap back to the memory device context.
            PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
            //We delete the memory device context.
            PlatformInvokeGDI32.DeleteDC(hMemDC);
            //We release the screen device context.
            PlatformInvokeUSER32.ReleaseDC(hwnd, hDC);
            //Image is created by Image bitmap handle and returned.
            return System.Drawing.Image.FromHbitmap(m_HBitmap);
        }
        //If m_HBitmap is null retunrn null.
        return null;

1 个答案:

答案 0 :(得分:0)

我看到我如何误解了Ellipse方法的定义。

Ellipse方法的最后两个参数是指从最左侧点到最右侧点的距离。相反,它们指的是最右边点和最底点的位置。

DrawEllipse方法的最后两个参数确实指的是右下角与边界矩形左上角的水平和垂直距离。

Ellipse(hDC, 100, 100, 200, 200);

大致相同
DrawEllipse(penRed, 100, 100, 100, 100); 

这是关于Ellipse方法的MSDN文档:

hdc [in] 设备上下文的句柄。

nLeftRect [in] 逻辑坐标中的x坐标,边界矩形的左上角。

nTopRect [in] 逻辑坐标中y坐标,边界矩形的左上角。

nRightRect [in] 逻辑坐标中的x坐标,位于边界矩形的右下角。

nBottomRect [in] 逻辑坐标中的y坐标,位于边界矩形的右下角。