Hellboy
驱动牛犊
驱动牛犊
  • 注册日期2003-07-22
  • 最后登录2005-06-22
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1288回复:3

【请教】关于GDI的若干问题!

楼主#
更多 发布于:2004-05-20 13:45
1,HWND hwnd = FindWindow();
   这样得到的窗口句柄好像不用CloseHandle来释放。如果直接使hwnd=NULL,会不会出现内存泄漏? (难道hwnd指向的是系统的内存区域?)

2,CDC * pdc = GetDC() ;
   在处理完成以后,是不是一定要ReleaseDC(pdc)来释放? ReleaseDC()以后是否还需要调用 delete pdc? (我看到一本关于windows图像编程的书,上面写的比较模糊,书上提供的代码从来都没调用ReleaseDC)

3,CDC * pdc = new CDC ; pdc->CreateCompatibleDC()
   pdc->CreateCompatibleDC()以后应该ReleaseDC()吧?
   CreateCompatibleDC()以后,我用pdc画图时,然后用BitBlt将pdc的内容copy到可以显示的DC上面,但是BitBlt好像不能正常执行,执行了以后没有任何显示。但是用下面的代码就没问题了。我有点看不太懂。
HDC tmphdc = GetDC(GetDesktopWindow());
HDC hComDC = CreateCompatibleDC(tmphdc);//hComDC做为全局变量,用来保存画图结果
                                                   //显示时调用BitBlt
HBITMAP hbmp = CreateCompatibleBitmap(tmphdc,rc.right,rc.bottom);
SelectObject(hComDC,hbmp);      
           TextOut(hComDC,10,10,\"test\",4);//在兼容DC里面画图  

ReleaseDC(GetDesktopWindow(),tmphdc);

DeleteObject(hbmp);
CloseHandle(hbmp);
//为什么需要CreateCompatibleBitmap,然后SelectObject以后才能让hComDC像正常的DC一样来操作?

4,SelectObject以后,hbmp里面的内容就和hComDC相关联了,以后操作hComDC就会改变hbmp的内容。
可以简单解释一下SelectObject的作用吗?
hzken
驱动牛犊
驱动牛犊
  • 注册日期2004-05-19
  • 最后登录2004-05-23
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-05-20 14:17
1,HWND hwnd = FindWindow();
这样得到的窗口句柄好像不用CloseHandle来释放。如果直接使hwnd=NULL,会不会出现内存泄漏?
A: hWnd is your applciaiton data which alloced in local stack and you can use \"hwnd=NULL\", it\'s safe.

 (难道hwnd指向的是系统的内存区域?)
A: Yes

2,CDC * pdc = GetDC() ;
在处理完成以后,是不是一定要ReleaseDC(pdc)来释放?
A: No, you don\'t need to do that. If you insist to release this DC, I guess it doesn nothing and it\'s still safe, for example, you try to relase the desktop DC.

ReleaseDC()以后是否还需要调用 delete pdc?
A: No, although you can call delete DC and it\'s safe, but it does nothing and just increase your binary size.

 (我看到一本关于windows图像编程的书,上面写的比较模糊,书上提供的代码从来都没调用ReleaseDC)

3,CDC * pdc = new CDC ; pdc->CreateCompatibleDC()
pdc->CreateCompatibleDC()以后应该ReleaseDC()吧?
A: I am not familir with MFC but I guess you should call delete pdc.

CreateCompatibleDC()以后,我用pdc画图时,然后用BitBlt将pdc的内容copy到可以显示的DC上面,但是BitBlt好像不能正常执行,执行了以后没有任何显示。
但是用下面的代码就没问题了。我有点看不太懂。
HDC tmphdc = GetDC(GetDesktopWindow());
HDC hComDC = CreateCompatibleDC(tmphdc);//hComDC做为全局变量,用来保存画图结果
//显示时调用BitBlt
HBITMAP hbmp = CreateCompatibleBitmap(tmphdc,rc.right,rc.bottom);
SelectObject(hComDC,hbmp);
TextOut(hComDC,10,10,\"test\",4);//在兼容DC里面画图

ReleaseDC(GetDesktopWindow(),tmphdc);

DeleteObject(hbmp);
CloseHandle(hbmp);
//为什么需要CreateCompatibleBitmap,然后SelectObject以后才能让hComDC像正常的DC一样来操作?
A: DC essentially is a pointer point to system mainatined global table element which is a data strucure to describe the device attributes, and drawing object including brush, pen, region, clip, bitmap and so on, all of which are NOT initialized ( = NULL), for example, of course you can not perform BitBlt command to a NULL bitmap, although it\'s safe but the bitmap render nothing for you.

So you need to create a bitmap and SELECT it to DC, essentially, SelectObject fills the bimtap element thus you can perform bitmap relative GDI calls.


4,SelectObject以后,hbmp里面的内容就和hComDC相关联了,以后操作hComDC就会改变hbmp的内容。
可以简单解释一下SelectObject的作用吗?

Hope it helps.
Hellboy
驱动牛犊
驱动牛犊
  • 注册日期2003-07-22
  • 最后登录2005-06-22
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-05-20 15:30
It does helps very much .
Thanks a lot!
Hellboy
驱动牛犊
驱动牛犊
  • 注册日期2003-07-22
  • 最后登录2005-06-22
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-05-21 14:24
 
Remarks
The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common DC.

An application cannot use the ReleaseDC function to release a DC that was created by calling the CreateDC function; instead, it must use the DeleteDC function. ReleaseDC must be called from the same thread that called GetDC.


在msdn找到了这样的资料,每次调用GetDC,GetWindowDC的时候都需要ReleaseDC,每次CreateDC,CreateCompitableDC以后需要DeleteDC
游客

返回顶部