SelectObject和DeleteObject 函数
Windows显示设备的属性,共有下面几种:位图、画刷、字体、画笔、区域。如果要设置它们到当前设备里,就需要使用SelectObject函数,比如上面介绍的字体设置,就会用到这个函数。当你创建一个位图时,这时Windows就会在内存里分配一块内存空间,用来保存位图的数据。当你创建字体时,也会分配一块内存空间保存字体。如果程序只是分配,而不去删除,就会造成内存使用越来越多,最后导到Windows这幢大楼倒下来。如果你忘记删除它,就造成了内存泄漏。因此,当你创建显示设备资源时,一定要记得删除它们啊,否则运行你的程序越长,就导致系统不稳定。记得使用DeleteObject函数去删除它们,把占用的内存释放回去给系统。
函数SelectObject和DeleteObject声明如下:
WINGDIAPI HGDIOBJ WINAPI SelectObject(__in HDC hdc, __in HGDIOBJ h);
WINGDIAPI BOOL WINAPI DeleteObject( __in HGDIOBJ ho);
hDC是当前设备的句柄。
h,ho是设备对象,其实它就是内存的地址。
调用这个函数的例子如下:
#001 //
#002 //界面显示输出.
#003 //
#004 //蔡军生2007/09/01 QQ:9073204 深圳
#005 //
#006 void CCaiWinMsg::OnDraw(HDC hDC)
#007 {
#008 //
#009 std::wstring strShow(_T("C++窗口类的实现,2007-09-04"));
#010
#011 //设置输出字符串的颜色.
#012 COLORREF crOld = SetTextColor(hDC,RGB(255,0,0));
#013
#014 RECT rcText;
#015 rcText.left = 10;
#016 rcText.top = 10;
#017 rcText.right = 300;
#018 rcText.bottom = 80;
#019
#020 //创建黑色的画刷,
#021 HBRUSH hbrush = CreateSolidBrush(RGB(0, 0, 0));
#022
#023 //用黑色的画刷填充四边形的颜色.
#024 FillRect(hDC,&rcText,hbrush);
#025
#026 //删除画刷.
#027 DeleteObject(hbrush);
#028
#029
#030 rcText.left = 10;
#031 rcText.top = 10;
#032 rcText.right = 300;
#033 rcText.bottom = 40;
#034
#035 //显示字符串在四边形的中间位置.
#036 DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#037 DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
#038
#039
#040 rcText.left = 10;
#041 rcText.top = 40;
#042 rcText.right = 300;
#043 rcText.bottom = 80;
#044 //设置透明背景
#045 int nOldMode = SetBkMode(hDC,TRANSPARENT);
#046
#047 //设置新字体.
#048 HGDIOBJ hOldFont = SelectObject(hDC,GetFont());
#049
#050 //显示字符串在四边形的中间位置.
#051 DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#052 DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
#053
#054 //恢复原来的字体.
#055 HGDIOBJ hFont = SelectObject(hDC,hOldFont);
#056 DeleteObject(hFont);
#057
#058 //恢复原来的模式.
#059 SetBkMode(hDC,nOldMode);
#060
#061 //恢复原来的颜色.
#062 SetTextColor(hDC,crOld);
#063 }
以上转自:http://blog.csdn.net/caimouse/archive/2007/09/05/1773850.aspx
////////////////////////////////////////////////
CFont font;
font.CreatePointFont(g_myFont,_T("宋体"));
GetDlgItem(IDC_STATIC_TITLE)->SetFont(&font);
CFont *pOldFont=pDC->SelectObject(&font);
DeleteObject(pOldFont);
//////////////////////////////////////////////////
BoundsChecker
发现有错误发生:Argument 1 in DeleteObject(字体对象句柄值) is still selected in to hDC 0x01010058
修改 :
//////////////////////////////////////////////////
//字体大小
CFont font;
font.CreatePointFont(g_myFont,_T("宋体"));
CFont *pOldFont=pDC->SelectObject(&font);
GetDlgItem(IDC_STATIC_TITLE)->SetFont(&font);//为控件附上字号
pDC->SelectObject(pOldFont);
DeleteObject(font);//释放资源
//////////////////////////////////////////////////
另一列:
//////////////////////////////////////////////////
CRect rect;
CDC* pDC=GetDC();
GetWindowRect(&rect);
CPen spen;
//被选中,显示红色,否则显示灰色
if(bIsSelect)spen.CreatePen(PS_SOLID,3,RGB(0,255,0));
else spen.CreatePen(PS_SOLID,3,RGB(192,192,192));
pDC->SelectObject(&spen);
pDC->SelectObject(GetStockObject(NULL_BRUSH));//选择透明填充
pDC->Rectangle(0,0,rect.Width()-3,rect.Height()-3);
ReleaseDC(pDC);
//////////////////////////////////////////////////
修改:
//////////////////////////////////////////////////
CRect rect;
CDC* pDC=GetDC();
GetWindowRect(&rect);
CPen spen;
//被选中,显示红色,否则显示灰色
if(bIsSelect)spen.CreatePen(PS_SOLID,3,RGB(0,255,0));
else spen.CreatePen(PS_SOLID,3,RGB(192,192,192));
CPen *pSpen = pDC->SelectObject(&spen);
pDC->SelectObject(GetStockObject(NULL_BRUSH));//选择透明填充
pDC->Rectangle(0,0,rect.Width()-3,rect.Height()-3);
pDC->SelectObject(pSpen);
DeleteObject(spen);//释放资源
ReleaseDC(pDC);
//////////////////////////////////////////////////
C# 获取桌面
[System.Runtime.InteropServices.DllImport(”gdi32.dll”)]
public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);
//创建桌面句柄
[System.Runtime.InteropServices.DllImportAttribute(”gdi32.dll”)]
public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, int lpInitData);
[System.Runtime.InteropServices.DllImport(”gdi32.dll”)]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
//转换为本地的图像资源
[System.Runtime.InteropServices.DllImport(”gdi32.dll”)]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[System.Runtime.InteropServices.DllImport(”gdi32.dll”)]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[System.Runtime.InteropServices.DllImport(”gdi32.dll”)]
public static extern int DeleteDC(IntPtr hdc);
//释放用过的设备句柄
[DllImport(”user32.dll”)]
public static extern bool ReleaseDC(
IntPtr hwnd, IntPtr hdc
);
//释放用过的画笔等资源
[DllImport(”gdi32.dll”)]
public static extern bool DeleteObject(
IntPtr hdc
);
/// <summary>
/// 截取屏幕图像
/// </summary>
/// <param name=”Width”>宽</param>
/// <param name=”Height”>高</param>
/// <param name=”x”>x坐标(全屏时候为0)</param>
/// <param name=”y”>y坐标(全屏时候为0)</param>
/// <returns></returns>
public Bitmap fullphoto(int Width,int Height,int x,int y)
{
Bitmap bitmap;
//try
//{
IntPtr hScreenDc = CreateDC(”DISPLAY”, null, null, 0); // 创建桌面句柄
IntPtr hMemDc = CreateCompatibleDC(hScreenDc); // 创建与桌面句柄相关连的内存DC
IntPtr hBitmap = CreateCompatibleBitmap(hScreenDc, Width, Height);
IntPtr hOldBitmap = SelectObject(hMemDc, hBitmap);
BitBlt(hMemDc, x, y, Width, Height, hScreenDc, x, y, (UInt32)0xcc0020);
IntPtr map = SelectObject(hMemDc, hOldBitmap);
bitmap = Bitmap.FromHbitmap(map);
ReleaseDC(hBitmap, hScreenDc);
DeleteDC(hScreenDc);//删除用过的对象
DeleteDC(hMemDc);//删除用过的对象
DeleteDC(hOldBitmap);
DeleteObject(hBitmap);
//}
//catch (Exception wx)
//{
// return null;
//}
// number= number +1;
// bitmap.Save(”screen” + number + “.bmp”);
return bitmap;
}
C#用API实现指定颜色填充一个闭合区域
C#用API实现指定颜色填充一个闭合区域
C#用API实现指定颜色填充一个闭合区域代码如下:
using System.Runtime.InteropServices;
[DllImport( " gdi32.dll " )]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport( " gdi32.dll " )]
public static extern IntPtr CreateSolidBrush( int crColor);
[DllImport( " gdi32.dll " )]
public static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart,
int crColor, uint fuFillType);
[DllImport( " gdi32.dll " )]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport( " gdi32.dll " )]
public static extern int GetPixel(IntPtr hdc, int x, int y);
public static uint FLOODFILLBORDER = 0 ;
public static uint FLOODFILLSURFACE = 1 ;
private void button1_Click( object sender, EventArgs e)
{
Graphics vGraphics = Graphics.FromHwnd(Handle);
vGraphics.DrawRectangle(Pens.Blue, new Rectangle( 0 , 0 , 300 , 300 ));
vGraphics.DrawRectangle(Pens.Blue, new Rectangle( 50 , 70 , 300 , 300 ));
IntPtr vDC = vGraphics.GetHdc();
IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Red));
IntPtr vPreviouseBrush = SelectObject(vDC, vBrush);
ExtFloodFill(vDC, 10 , 10 , GetPixel(vDC, 10 , 10 ), FLOODFILLSURFACE);
SelectObject(vDC, vPreviouseBrush);
DeleteObject(vBrush);
vGraphics.ReleaseHdc(vDC);
}WinCE平台下C#引用API(GDI)一个值得警惕的内存泄漏由于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个小时(视圆角矩形绘图的多寡而定),该程序就会内存耗尽而死。