码迷,mamicode.com
首页 > Windows程序 > 详细

win32创建工具栏的自定义图标

时间:2019-09-11 18:22:11      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:VID   图像   init   文件   ids   bad   ada   mamicode   map   

注意点:使用LoadImage函数加载bmp图片,这里特指BMP图片,其实LoadImage可以加载很多格式的图片

HBITMAP bitmap = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDB_LIST), IMAGE_BITMAP, 32, 32, NULL);

LoadImage的第一个参数是实例句柄,我在测试的时候,如果设为NULL或者父窗口的实例句柄hInstance,会报1814错误,错误信息: ERROR_RESOURCE_NAME_NOT_FOUND, 我将参数改为上面给出的即可成功加载。

IDB_LIST是资源图片的宏定义,需要事先将图片加载到资源文件里。

也可以直接用

HBITMAP bitmap = (HBITMAP)LoadImage(NULL, L"C:\\Users\\strives\\Desktop\\panda.bmp", IMAGE_BITMAP, 32, 32, LR_LOADFROMFILE);

这里是从本地文件中加载的图片,最后一个参数一定要改成LR_LOADFROMFILE

全部代码:

// WindowsProject42.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WindowsProject42.h"
#include <commctrl.h>

#pragma comment (lib,"Comctl32.lib")

#define MAX_LOADSTRING 100
#define IDC_MAIN_TOOL 102
#define ID_FILE_NEW 103

// Global Variables:
HINSTANCE g_hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
HWND hToolBar;
//TBBUTTON tbb[1];
//TBADDBITMAP tbab;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
HWND CreateSimpleToolbar(HWND hWndParent);
HIMAGELIST g_hImageList = NULL;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT42, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT42));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT42));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT42);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    g_hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
   CreateSimpleToolbar(hWnd);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

HWND CreateSimpleToolbar(HWND hWnd)
{
    TBBUTTON tbb[1];
    TBADDBITMAP tbab;
    const int bitmapSize = 32;
    const int ImageListID = 0;

    hToolBar = CreateWindowEx(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
        hWnd, NULL, g_hInst, NULL);
    HBITMAP bitmap = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDB_LIST), IMAGE_BITMAP, 32, 32, NULL);
    
    SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);


    ZeroMemory(&tbb, sizeof(tbb));
    tbb[0].iBitmap = 0;
    tbb[0].fsState = TBSTATE_ENABLED;
    tbb[0].fsStyle = TBSTYLE_BUTTON;
    tbb[0].idCommand = ID_FILE_NEW;
    tbb[0].iString = (INT_PTR)L"Create a new file";

    g_hImageList = ImageList_Create(bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
        ILC_COLOR24 | ILC_MASK,   // Ensures transparent background.
        1, 0);
    ImageList_Add(g_hImageList, bitmap, NULL);

    SendMessage(hToolBar, TB_SETIMAGELIST,
        (WPARAM)ImageListID,
        (LPARAM)g_hImageList);

    LRESULT I = SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tbb) / sizeof(TBBUTTON), (LPARAM)&tbb);

   
    SendMessage(hToolBar, TB_AUTOSIZE, 0, 0);
    return hToolBar;
}

Debug :

技术图片

 

简单介绍下主要函数的调用:

使用ImageList_Create()创建图像列表,再使用ImageList_Add()将BMP图像添加到其中,或者使用ImageList_ReplaceIcon()工具栏将其关联TB_SETIMAGELIST,然后使tbb[0].iBitmap在图像列表中设置为BMP的索引。

更为细节的可以参阅MSDN的官方案例:https://docs.microsoft.com/en-us/windows/win32/controls/create-toolbars

里面介绍了使用标准系统图标创建插图中显示的工具栏。

win32创建工具栏的自定义图标

标签:VID   图像   init   文件   ids   bad   ada   mamicode   map   

原文地址:https://www.cnblogs.com/strive-sun/p/11507247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!