如何在MFC中向按钮添加位图图像?
我正在尝试将图像添加到现有按钮中。我已经在一定程度上做到了这一点,问题是我可以添加所有者绘制的图像,但无法添加我想要的确切图像。例如,请参阅下面的示例 使用上述代码的代码
CButton* pBtn= (CButton*)GetDlgItem(ID_WIZBACK);
pBtn->ModifyStyle( 0, BS_ICON );
HICON hIcn= (HICON)LoadImage(
AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDI_ICON3),
IMAGE_ICON,
0,0, // use actual size
LR_DEFAULTCOLOR
);
pBtn->SetIcon( hIcn );
正在将位图转换为图标以添加到我的按钮...我如何将精确的位图图像直接添加到现有按钮。请帮助我 frnds..
I am Trying to add an image to an existing button..I have done that to an extent, the problem is I can add an ownerdrawn Image but am not able to add the extact image that I want.. for the example see the below code
CButton* pBtn= (CButton*)GetDlgItem(ID_WIZBACK);
pBtn->ModifyStyle( 0, BS_ICON );
HICON hIcn= (HICON)LoadImage(
AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDI_ICON3),
IMAGE_ICON,
0,0, // use actual size
LR_DEFAULTCOLOR
);
pBtn->SetIcon( hIcn );
with the above code am converting the bitmap to an icon to add to my button...how can I add the exact Bitmap image directly to an existing button.Please help me frnds..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 mfc 中将位图分配给按钮的步骤:
代码:
Steps for assigning bitmap to button in mfc :
Code :
我实际上解决了这个问题..我所做的是我用 HBITMAP 替换了 HICON,它的工作完美...基本上两者都可以正常工作,但在我的情况下,当我将图标加载到按钮中时,图标的背景没有改变。 ..我尝试了位图,然后效果很好。现在正在努力定位图像并添加文本......我想我可以通过
I actually fixed the problem..what I did is I replaced the HICON with HBITMAP and its working perfect...basically both would work fine but in my case when I loaded the icon into the button the background of the icon was not changing...I tried Bitmap then it work great. Now am working on positioning the Image and to add text...think I could go through
你不知道这有多大帮助。感谢您发帖。还必须将其他一些东西更改为位图......
you don't know how much this helped out. Thanks for posting. Also have to change a few other things to bitmap as well ...
您可以使用
CBitmapButton::SubclassWindow
对现有按钮进行子类化,然后使用LoadBitmaps
。You could subclass existing button using
CBitmapButton::SubclassWindow
, then useLoadBitmaps
.使用功能包中的按钮类。它们支持在按钮上显示文本和图像,但常规按钮无法做到这一点。查看 VS 安装目录中的“samples”目录。
Use the button classes from the Feature Pack. They have support for showing both text and images on buttons, your regular button can't do that. Look at the 'samples' directory in your VS installation directory.
我想在 @Amruta Ghodke 的答案中添加一些想法:
您可以使用
GetWindowRect
和SetWindowPos
函数调整按钮的大小。请看下面的例子:如果要显示透明图像,可以使用软件 Pixelformer 来将您的 PNG 转换为启用 Alpha 的 BMP。您必须:
带 alpha 通道的 RGB 颜色
A8:R8:G8:B8
导出文件并禁用预乘 alpha
和自上而下的行顺序
I want to add some ideas to @Amruta Ghodke 's answer:
You can resize your button using the
GetWindowRect
andSetWindowPos
functions. See an example below:If you want to display transparent images, use the software Pixelformer to convert your PNGs to Alpha-enabled BMPs. You will have to:
RGB color with alpha channel
A8:R8:G8:B8
and disabledPremultiplied alpha
andTop-down row order