由于C#精简框架集绘图函数不支持圆角矩形,所以引用了相关的API。
[DllImport("//windows//coredll.dll", EntryPoint = "RoundRect")]
private static extern int CeRoundRect(IntPtr hdc, int X1, int Y1, int X2, int Y2, int X3, int Y3);
这是有内存泄漏的源码:
public static int RoundRect(Graphics e, Pen pen, SolidBrush brush, int X1, int Y1, int X2, int Y2, int X3, int Y3)
{
IntPtr hpen;
IntPtr hbrush;
if(pen!=null)
{
hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,
(int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B)); //创建GDI画笔
}
else
{
hpen = GetStockObject(8); //空画笔
}
if (brush!= null)
{
hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());
}
else
{
hbrush = GetStockObject(5);
}
//pen.Dispose();
//brush.Dispose();
IntPtr hdc = e.GetHdc();
//---------------------
SelectObject(hdc, hbrush);
SelectObject(hdc, hpen);
int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);
DeleteObject(hbrush);
DeleteObject(hpen);
//---------------------
e.ReleaseHdc(hdc);
return intRet;
}
这是没有问题的源码:
public static int RoundRect(Graphics e, Pen pen, SolidBrush brush, int X1, int Y1, int X2, int Y2, int X3, int Y3)
{
IntPtr hpen,old_pen;
IntPtr hbrush, old_brush;
if(pen!=null)
{
hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,
(int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B)); //创建GDI画笔
}
else
{
hpen = GetStockObject(8); //空画笔
}
if (brush!= null)
{
hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());
}
else
{
hbrush = GetStockObject(5);
}
//pen.Dispose();
//brush.Dispose();
IntPtr hdc = e.GetHdc();
//---------------------
old_brush=SelectObject(hdc, hbrush);
old_pen=SelectObject(hdc, hpen);
int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);
SelectObject(hdc, old_brush);
SelectObject(hdc, old_pen);
DeleteObject(hbrush);
DeleteObject(hpen);
//---------------------
e.ReleaseHdc(hdc);
return intRet;
}
看出代码的区别来了没有?泄漏的原因其实很简单,就是没有重新选入旧的画笔画刷。同样的程序(当然PC端的API库是GDI32)在上位机Window XP平台上没有什么问题(测试大约3天以上),而在WinCE平台确非常明显,大约1~3个小时(视圆角矩形绘图的多寡而定),该程序就会内存耗尽而死。
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
相关推荐
WINCE下软键盘 C# API ,反Coredll,换掉同样可以在WIN下运行
本文主要介绍在基于Windows CE 平台的英创嵌入式主板下进行C#(Microsoft Visual Stdio.Net 2005)应用程序开发时会常常用到的一些功能函数以及开发方法,这些方法适用 于英创采用WinCE 平台的所有型号嵌入式主板,...
C#Wince访问WebApi及结果Json格式化功能,含源代码,可直接使用
C#调用API实现WINCE下播放背景音乐wav,在WINCE下调试通过。
c# GDI 图形移动 wince .net
wince下检测内存泄漏 网上找的 内存泄漏令人头疼,桌面端有不少工具 但ce下很少,这是自己写的
wince c# 开发 mc 3090调用API震动程序wince c# 开发 mc 3090调用API震动程序wince c# 开发 mc 3090调用API震动程序
基于WinCE平台C#编程要点之一 by 英创信息技术有限公司.pdf
wince下用C#画图的例子
wince下的C#读取xml文件,wince智能项目不支持相对路径,所以没有Setting.setting配置文件,所以需要读写xml,下载文件中有详细说明
wince5.0 下用C#写的蓝牙打印测试程序。程序使用MC3000手持和Zebra MZ220便携式打印机测试通过。 程序是建立serialport对象,指定com口连接打印机,界面上open打开蓝牙端口,close关闭蓝牙端口 打印时向蓝牙端口发送...
自己写的wince下使用C#双缓冲技术绘图仪表盘可用于汽车仪表、工控仪表,控可以直接拿到windows下用,demo程序需要在wicne5.0下运行,有做嵌入式仪表来拿,有需要帮忙,可以发邮件给我changhai203.com@163.com,主题...
wince5.0 下用C#写的蓝牙打印测试程序。程序使用MC3000手持和Zebra MZ220便携式打印机测试通过。 程序是建立serialport对象,指定com口连接打印机,界面上open打开蓝牙端口,close关闭蓝牙端口 打印时向蓝牙端口发送...
wince下c#实现pppoe,wince下c#实现pppoe,wince下c#实现pppoe,
c#编的串口测试程序!wince下可以使用的!sfwiefwsfas
C# wince 相册 绘图 C# wince 相册 绘图
wince 6.0获取摄像头数据,并在C#中调用例程
在WinCE环境下,利用VS 2005及C#窗体程序设计开发一个身份证号码验证系统,包括身份信息验证及校验码验证,符合GB11643-1999标准。系统能正确识别身份证号码,并将识别成功的信息展示到窗体中,如身份证持有者的所在...
wince下GDI绘图性能测试,用于跟踪WINCE下程序在绘图时的效率问题。
wince下用c#语言编写的sqlite程序demo wince下用c#语言编写的sqlite程序demo