如何转换惠康在VC ++来HBITMAP? [英] How to convert HICON to HBITMAP in VC++?

查看:151
本文介绍了如何转换惠康在VC ++来HBITMAP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换惠康在向VC ++ HBITMAP?

我知道这是一个常见问题,但我已经在谷歌找到的所有解决方案不起作用。我需要的是一个函数,它接受一个参数HICON和HBITMAP返回

最大可能的话,使转换为32比特的位图,即使该图标是24位,16位或8位。

这是code,我不知道哪里出了问题:

  HBITMAP icon_to_bitmap(HICON Icon_Handle){
  HDC Screen_Handle =的GetDC(NULL);
  HDC Device_Handle = CreateCompatibleDC(Screen_Handle);  HBITMAP Bitmap_Handle =
  CreateCompatibleBitmap(Device_Handle,GetSystemMetrics的(SM_CXICON)
  GetSystemMetrics的(SM_CYICON));  HBITMAP Old_Bitmap =(HBITMAP)选择对象(Device_Handle,Bitmap_Handle);
  DrawIcon(Device_Handle,0,0,Icon_Handle);
  选择对象(Device_Handle,Old_Bitmap);  DeleteDC(Device_Handle);
  ReleaseDC(NULL,Screen_Handle);
  返回Bitmap_Handle;
}


解决方案

我没有code一应俱全分享,但我认为这是pretty容易。你必须创建HBITMAP,创建一个设备上下文,选择位图到DC(这将使位图此DC的绘图区域)。最后调用DrawIcon()函数来绘制此DC上的图标。之后分离从DC位图和破坏直流。现在你的位图应该是准备好了。

看着你的code后更新:

我相信这个问题是在createCompatibleBitmap电话。你所要求的与内存DC兼容的位图,但内存的DC开始选入其中1位/像素的位图。试着问了与屏幕DC,而不是兼容的位图。

更新2:你可能想看看这个问题因为这似乎与你的问题。

How to convert HICON to HBITMAP in VC++?

I know this is an FAQ but all the solutions I've found on Google don't work. What I need is a function which takes a parameter HICON and returns HBITMAP.

Greatest if possible to make conversion to 32-bit bitmap even the icon is 24-bit, 16-bit or 8-bit.

This is the code, I don't know where it goes wrong:

HBITMAP icon_to_bitmap(HICON Icon_Handle) {
  HDC Screen_Handle = GetDC(NULL);
  HDC Device_Handle = CreateCompatibleDC(Screen_Handle);

  HBITMAP Bitmap_Handle = 
  CreateCompatibleBitmap(Device_Handle,GetSystemMetrics(SM_CXICON),
  GetSystemMetrics(SM_CYICON));

  HBITMAP Old_Bitmap = (HBITMAP)SelectObject(Device_Handle,Bitmap_Handle);
  DrawIcon(Device_Handle, 0,0, Icon_Handle);
  SelectObject(Device_Handle,Old_Bitmap);

  DeleteDC(Device_Handle);
  ReleaseDC(NULL,Screen_Handle);
  return Bitmap_Handle;
}

解决方案

I don't have code readily available to share, but I think this is pretty easy. You have to create the HBITMAP, create a device context, select the bitmap into the DC (this will make the bitmap the drawing area for this DC). Finally call the DrawIcon() function to draw your icon on this DC. After that detach the bitmap from the DC and destroy the DC. Your bitmap now should be ready to go.

Update after looking at your code:

I believe the problem is in the createCompatibleBitmap call. You are asking for a bitmap compatible with the memory DC, but memory DCs start with a 1 bit/pixel bitmap selected into them. Try asking for a bitmap compatible with the screen DC instead.

Update 2: you may want to look at this question as it seems related to your problem.

这篇关于如何转换惠康在VC ++来HBITMAP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆