如何在 WinAPI 中本机将图像添加到按钮C++(没有 MFC)
How to natively add an image to a button in C++ WinAPI (without MFC)?
好的,我想将图像与文本一起添加到按钮中,而无需在我的 RC 文件中制作按钮。这甚至可能吗,或者我是否需要使用 RC 文件来使按钮能够在其中放置图像?我的图像在"resource.h"中#define
d,图像在"resources.rc"中声明。"main.cpp"和"resources.rc"都包含"resource.h"标头。我真的不想使用资源制作按钮,但如果它是制作带有图像和文本的按钮的唯一方法,然后我会这样做。我需要知道的只是如何将图像放入 WinAPI 中的按钮中。
更新:
- 将清单文件
添加到应用程序,清单文件应命名为
YourApp.exe.manifest
将其添加到您的清单文件中(有关清单文件的更多信息,请单击此处 http://msdn.microsoft.com/en-us/library/bb773175%28VS.85%29.aspx):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="CompanyName.ProductName.YourApplication" type="win32" /> <description>Your application description here.</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> </assembly>
将应用程序与
ComCtl32.lib
链接将清单添加到应用程序资源文件
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "YourApp.exe.manifest"
在WinMain开始时呼叫
InitCommonControls()
结束更新
-
按钮创建的示例代码(图像+文本),由于加载位图而容易出现内存泄漏:
HWND hwnd_button = CreateWindowEx( 0, "BUTTON", //ascii "Button text", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 10, 145, 50, 50, hwnd_parent, NULL, //GetModuleHandle(NULL) (HINSTANCE)GetWindowLong(hwnd_parent, GWL_HINSTANCE), NULL); SendMessage((HWND) m_hWndButton, (UINT) BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1)));
相关文章:
- 问:如何使用C++中的按钮从窗口打开窗口
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- QML按钮点击功能执行顺序
- 当用户超过按钮点击限制时报告
- 函数计算用户按下按钮的频率
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- Qt自定义QPush按钮未显示在布局上
- SDL 操纵杆按钮按下/C++
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- Qt中来自不同功能的按钮引用
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- ImGui 在单击按钮后冻结
- WinAPI 无法创建按钮
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 按钮按下打开 OLED ESP32
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件