如何在文字按钮上放置图标

如何在文字按钮上放置图标

本文介绍了如何在文字按钮上放置图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一按钮上同时显示图像图标和文本,例如在Word中.

我在按钮上设置了一个图标,但文本消失了.

  HANDLE hBmp =(HBITMAP)LoadImage(g_hDllInstance,MAKEINTRESOURCE(IDB_BITMAP4),IMAGE_BITMAP,空值,空值,LR_DEFAULTCOLOR);HWND hwndButton = CreateWindowEx(空值,_T(按钮"),_T("SOME TEXT"),BS_BITMAP |WS_VISIBLE |WS_CHILD,point.x-47,点-3,36岁40岁hWnd,(HMENU)200,空值,空值);发信息((HWND)hwndButton,(UINT)BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBmp); 

我还尝试将图标设置在按钮上较小的子窗口中,但是由于某种原因,我的子窗口不可见.

解决方案

注意,您必须 ="nofollow noreferrer">启用视觉样式.

I want to have both an image icon and text on the same button, like here in Word for example.

I set an icon on a button, but the text disappears.

HANDLE hBmp = (HBITMAP)LoadImage(g_hDllInstance,
    MAKEINTRESOURCE(IDB_BITMAP4),
    IMAGE_BITMAP,
    NULL,
    NULL,
    LR_DEFAULTCOLOR);
HWND hwndButton = CreateWindowEx(
    NULL,
    _T("BUTTON"),
    _T("SOME TEXT"),
    BS_BITMAP | WS_VISIBLE | WS_CHILD,
    point.x - 47,
    point.y - 3,
    36,
    40,
    hWnd,
    (HMENU)200,
    NULL,
    NULL);
SendMessage(
    (HWND)hwndButton,
    (UINT)BM_SETIMAGE,
    (WPARAM)IMAGE_BITMAP,
    (LPARAM)hBmp);

I have also tried to set the icon on a smaller subwindow on my button, but for some reason my subwindow is not visible.

解决方案

Instructions on how to get a button to display both an image and text are outlined in the Button Styles reference:

In other words: Don't set the BS_ICON or BS_BITMAP style (but do set the BS_TEXT style), and send a BM_SETIMAGE message once the button has been created.

To see this in action, create a standard Windows Desktop application in Visual Studio, and apply the following changes:

  • Enable visual styles. This is easiest done by placing a #pragma linker directive into the only compilation unit:

    #pragma comment(linker,"\"/manifestdependency:type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    

  • Create the button in the main window's WM_CREATE handler:

    case WM_CREATE:
        {
            HWND btn{ ::CreateWindowExW(0x0, L"BUTTON", L"Button text",
                                        WS_VISIBLE | WS_CHILD | BS_TEXT,
                                        10, 10, 200, 50, hWnd, (HMENU)110,
                                        nullptr, nullptr) };
            HICON icon{ (HICON)::LoadImageW(::GetModuleHandle(nullptr),
                                            MAKEINTRESOURCEW(107),
                                            IMAGE_ICON, 32, 32, 0x0) };
            ::SendMessageW(btn, BM_SETIMAGE, IMAGE_ICON, (LPARAM)icon);
        }
        break;
    

    Make sure to adjust the numeric constants as needed. 110 is the button's control identifier, 107 is the resource ID of the wizard-generated application icon resource, and 32 are the width and height of the requested icon.

This code produces the following output:


Note, that you have to enable visual styles for this to work.

这篇关于如何在文字按钮上放置图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:02