CreatePatternBrush / Ellipse 不适用于 WinMobile 设备,但适用于模拟器
为了在屏幕上绘制位图的圆形部分,我使用从位图创建的 PatternBrush 来填充椭圆。 我对本机函数使用 P/Invoke,因为如果使用托管函数,CF2.0 中似乎存在错误(有关更多详细信息,请参阅此处:http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/ e831ea2f-039a-4b92-adb6-941954bee060/)。
这是我使用的代码:
[DllImport("coredll.dll")]
private extern static int Ellipse(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePatternBrush(IntPtr hImage);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);
[DllImport("coredll.dll")]
private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hBrush);
[DllImport("coredll.dll")]
private extern static bool DeleteObject(IntPtr hBrush);
private void DrawCircleOfBitmap(Graphics g, Bitmap bmp, Rectangle rect)
{
IntPtr hBitmap = bmp.GetHbitmap(); // get HBitmap
IntPtr hBrush = CreatePatternBrush(hBitmap); // create the PatternBrush
IntPtr hPen = CreatePen(5, 1, 0); // empty Pen (PS_NULL = 5)
IntPtr hDC = g.GetHdc(); // get HDC
IntPtr hOldBrush = SelectObject(hDC, hBrush); // select Brush into context
IntPtr hOldPen = SelectObject(hDC, hPen); // select Pen into context
Ellipse(hDC, rect.Left, rect.Top, rect.Right, rect.Bottom);
// Release of native GDI objects
SelectObject(hDC, hOldBrush);
SelectObject(hDC, hOldPen);
DeleteObject(hBrush);
DeleteObject(hPen);
g.ReleaseHdc(hDC);
DeleteObject(hBitmap);
}
这在每个模拟器(WM6、WM6.1.4、WM6.5)上都能完美运行,但如果我在真实设备(HTC Tytn II)上使用完全相同的代码,我得到的只是一个白色圆圈。 该圆圈未填充位图。 我检查了设备上每一行的返回码 - 一切都没有报告错误。 用 CreateSolidBrush 替换 CreatePatternBrush 是有效的,它会用颜色填充圆圈。 有人知道为什么图案刷不起作用吗?
谢谢 麦克
To draw a circular part of a bitmap on the screen, I use a PatternBrush created from the bitmap to fill an ellipse. I use P/Invoke to the native functions because there seems to be a bug in CF2.0 if you use the managed functions (see here for more details: http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/e831ea2f-039a-4b92-adb6-941954bee060/).
Here is the code I use:
[DllImport("coredll.dll")]
private extern static int Ellipse(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePatternBrush(IntPtr hImage);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);
[DllImport("coredll.dll")]
private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hBrush);
[DllImport("coredll.dll")]
private extern static bool DeleteObject(IntPtr hBrush);
private void DrawCircleOfBitmap(Graphics g, Bitmap bmp, Rectangle rect)
{
IntPtr hBitmap = bmp.GetHbitmap(); // get HBitmap
IntPtr hBrush = CreatePatternBrush(hBitmap); // create the PatternBrush
IntPtr hPen = CreatePen(5, 1, 0); // empty Pen (PS_NULL = 5)
IntPtr hDC = g.GetHdc(); // get HDC
IntPtr hOldBrush = SelectObject(hDC, hBrush); // select Brush into context
IntPtr hOldPen = SelectObject(hDC, hPen); // select Pen into context
Ellipse(hDC, rect.Left, rect.Top, rect.Right, rect.Bottom);
// Release of native GDI objects
SelectObject(hDC, hOldBrush);
SelectObject(hDC, hOldPen);
DeleteObject(hBrush);
DeleteObject(hPen);
g.ReleaseHdc(hDC);
DeleteObject(hBitmap);
}
This works perfect on every emulator (WM6, WM6.1.4, WM6.5), but if I use exactly the same on my real device (HTC Tytn II) all I get is a white circle. The circle is not filled with the bitmap. I checked the return codes of every line on the device - everything reports no error. Replacing CreatePatternBrush with CreateSolidBrush works, than it fills the circle with a color.
Anybody an idea why the pattern brush is not working?
Thanks
Maik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据该行为,Tytn II 的显示驱动程序很可能不支持图案画笔。 它可能应该告诉 GDI 它不受支持,但事实并非如此。 但这并不罕见 - 通常 OEM 不会实现显示驱动程序的所有功能(Alpha 混合是一个典型的例子),并且不会让驱动程序将其报告为不受支持。
Based on the behavior, it's very likely that the display driver for the Tytn II doesn't support the pattern brush. It probably should tell GDI that it's not supported, but it's not. This is not unusual though - very often OEMs will not implement every feature for the display driver (alpha blending being a classic example) and not have the driver report it as unsupported.