使用GDI32椭圆,如.net drawlipse
本文关键字:net drawlipse GDI32 椭圆 使用 | 更新日期: 2023-09-27 18:12:28
在c#中,我可以使用。net System.Graphics.DrawEllipse方法在从Windows Calculator (calc.exe)窗口获取的屏幕设备上下文上绘制椭圆。
我希望能够用GDI32椭圆方法做同样的事情。如何使椭圆绘制到屏幕上?
在下面的代码中,这行行得通:CalculatorGraphics。drawlipse (penRed, 50,50,50,50);但这句话没有:PlatformInvokeGDI32。椭圆(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;
我知道我误解了Ellipse方法的定义。
Ellipse方法的最后两个参数分别表示到最左点和最右点的距离。相反,它们指的是最右点和最底点的位置。
DrawEllipse方法的最后两个参数确实是指右下点到边界矩形左上点的水平和垂直距离。
Ellipse(hDC, 100, 100, 200, 200);
的意思与
大致相同DrawEllipse(penRed, 100, 100, 100, 100);
下面是关于Ellipse方法的MSDN文档:
hdc[在]设备上下文的句柄。
nLeftRect[在]边界矩形左上角的x坐标(以逻辑坐标表示)。
nTopRect[在]边界矩形左上角的y坐标(以逻辑坐标表示)。
nRightRect[在]边界矩形右下角的x坐标(以逻辑坐标表示)。
nBottomRect[在]边界矩形右下角的y坐标(以逻辑坐标表示)。