在C#中,我能够使用.NET System.Graphics.DrawEllipse方法在从窗口计算器(calc.exe)窗口获取的屏幕设备上下文上绘制椭圆。
我希望能够用GDI32椭圆方法做同样的事情。如何让Ellipse绘制到屏幕上?
在下面的代码中,这一行可以工作: CalculatorGraphics.DrawEllipse(penRed,50,50,50,50);但这行不能:PlatformInvokeGDI32。problem (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;发布于 2011-11-10 09:45:35
我明白我是如何误解了Ellipse方法的定义。
Ellipse方法的最后两个参数,用于引用与最左侧点和最右侧点之间的距离。相反,它们指的是最右边的点和最下面的点的位置。
DrawEllipse方法的最后两个参数确实引用右下点与边界矩形左上点之间的水平和垂直距离。
Ellipse(hDC, 100, 100, 200, 200);意思大致与
DrawEllipse(penRed, 100, 100, 100, 100); 以下是关于Ellipse方法的MSDN文档:
设备上下文的句柄中的hdc。
以逻辑坐标表示的边界矩形左上角x坐标中的nLeftRect。
以逻辑坐标表示的边界矩形左上角y坐标中的nTopRect。
以逻辑坐标表示的边界矩形右下角x坐标中的nRightRect。
以逻辑坐标表示的边界矩形右下角y坐标中的nBottomRect。
https://stackoverflow.com/questions/7894403
复制相似问题