Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Tạo ComboBoxEx để có thể hiển thị Image cùng với text trong window API?

    Mấy anh cho em hỏi là làm sao mình có thể tạo 1 comboboxEx mà các item của nó có thể hiển thị được image (như trong thanh Address của window explorer) ? Em đã viết đoạn code sau nhưng nó chỉ hiện thị được text, ko hiển thị Image, mặc dù em đã gán thành công image đến comboboxEx.Mong các anh giúp đỡ, viết cho em một cái demo thì càng tốt. [IMG]images/smilies/Cry.gif[/IMG]


    Mã:
    void CreateComboboxEx(HWND hWnd){    INITCOMMONCONTROLSEX initEx;    initEx.dwSize = sizeof(INITCOMMONCONTROLSEX);    initEx.dwICC = ICC_USEREX_CLASSES;    InitCommonControlsEx(&initEx);    hComboboxEx = CreateWindowEx(0, WC_COMBOBOXEX, _T(""), WS_CHILD|CBS_DROPDOWN, 0, 0, 300, 200,hWnd, NULL, hInst, NULL); } BOOL WINAPI InitComboboxEx(){    HIMAGELIST himl;        Shell_GetImageLists(NULL, &himl);        SendMessage(hComboboxEx, CBEM_SETIMAGELIST, 0, (LPARAM)&himl);    COMBOBOXEXITEM cbitem;    cbitem.mask = CBEIF_IMAGE|CBEIF_TEXT|CBEIF_SELECTEDIMAGE|CBEIF_INDENT;    cbitem.cchTextMax = 256;    cbitem.pszText = L"A";    cbitem.iImage = 0;    cbitem.iItem = 0;    cbitem.iIndent = 2;    cbitem.iSelectedImage = 0;    SendMessage(hComboboxEx, CBEM_INSERTITEM, 0, (LPARAM) &cbitem);    SetWindowPos(hComboboxEx, NULL, 0, 0, 300, 200, SWP_SHOWWINDOW);    return TRUE;} BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){    HWND hWnd;     hInst = hInstance; // Store instance handle in our global variable     hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);     if (!hWnd)    {        return FALSE;    }    CreateComboboxEx(hWnd);    InitComboboxEx();    ShowWindow(hWnd, nCmdShow);    UpdateWindow(hWnd);    return TRUE;}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Thành công đâu mà thành công! Sai nhiều kiểu nên cũng không biết phải nói thế nào, tham khảo mấy cái sau nhé!


    Mã:
    http://msdn.microsoft.com/en-us/library/52hkw8ts%28v=vs.90%29.aspxhttp://www.codeproject.com/Articles/53/A-Combobox-with-bitmapshttp://www.codeguru.com/cpp/controls/combobox/article.php/c1805/Icon-Picker-Combo-Box.htm

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Sai lầm của bạn ở chỗ:

    SendMessage(hComboboxEx, CBEM_SETIMAGELIST, 0, (LPARAM)&himl);
    Bạn chú ý tham số LPARAM là handle của imagelist chứ không phải địa chỉ của handle.
    Bạn cần sửa lại thành như sau:

    Mã:
    SendMessage(hComboboxEx, CBEM_SETIMAGELIST, 0, (LPARAM)himl);
    Nhân tiện mình cũng viết sẵn 1 cái demo cho bạn tham khảo thêm

    Đoạn code được viết trên visual c++ 2005, unicode

    Mã:
    // ComboBoxImage.cpp : Defines the entry point for the application.// #include "stdafx.h"#include "ComboBoxImage.h"#include <CommCtrl.h>#include <ShlObj.h>#pragma comment (lib, "comctl32.lib")#pragma comment(lib, "shell32.lib")  // Global Variables:HINSTANCE hInst;                                // current instanceHWND hCbx;HIMAGELIST hImg; // Forward declarations of functions included in this code module:ATOM                MyRegisterClass(HINSTANCE hInstance);BOOL                InitInstance(HINSTANCE, int);LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);void CreateComboBox(HWND hWnd); int APIENTRY _tWinMain(HINSTANCE hInstance,                       HINSTANCE hPrevInstance,                       LPTSTR    lpCmdLine,                       int       nCmdShow){    UNREFERENCED_PARAMETER(hPrevInstance);    UNREFERENCED_PARAMETER(lpCmdLine);     // TODO: Place code here.    MSG msg;     MyRegisterClass(hInstance);     // Perform application initialization:    if (!InitInstance (hInstance, nCmdShow))    {        return FALSE;    }      // Main message loop:    while (GetMessage(&msg, NULL, 0, 0))    {        TranslateMessage(&msg);        DispatchMessage(&msg);    }     return (int) msg.wParam;}  ATOM MyRegisterClass(HINSTANCE hInstance){    WNDCLASSEX 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, IDI_APPLICATION);    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);    wcex.lpszMenuName   = NULL;    wcex.lpszClassName  = TEXT("ComboBoxEx Demo");    wcex.hIconSm        = NULL;     return RegisterClassEx(&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){    HWND hWnd;     hInst = hInstance; // Store instance handle in our global variable     hWnd = CreateWindow(TEXT("ComboBox Demo"), TEXT("ComboBox Demo"), WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);     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_CREATE:        CreateComboBox(hWnd);        break;    case WM_CLOSE:        ImageList_Destroy(hImg);        return DefWindowProc(hWnd, message, wParam, lParam);    case WM_DESTROY:        PostQuitMessage(0);        break;    default:        return DefWindowProc(hWnd, message, wParam, lParam);    }    return 0;} void createItem(HWND hc){    COMBOBOXEXITEM cbei;    int i, len;     cbei.mask=CBEIF_TEXT|CBEIF_IMAGE|CBEIF_SELECTEDIMAGE|CBEIF_INDENT;    cbei.pszText=TEXT("Image In Combo Box");    Shell_GetImageLists(NULL, &hImg);    SendMessage(hc, CBEM_SETIMAGELIST, 0, (LPARAM)hImg);//Add list image to comboboxex    len=ImageList_GetImageCount(hImg);    for(i=0; i<len; i++)    {        cbei.cchTextMax=(int)wcslen(cbei.pszText);        cbei.iItem=i;        cbei.iImage=i;        cbei.iSelectedImage=i;        cbei.iIndent=i%3;//Indent for each item        SendMessage(hc, CBEM_INSERTITEM, -1, (LPARAM)&cbei);//Add cbei to comboboxex    }}  void CreateComboBox(HWND hWnd){    INITCOMMONCONTROLSEX icex;      icex.dwSize=sizeof(INITCOMMONCONTROLSEX);    icex.dwICC=ICC_USEREX_CLASSES;    InitCommonControlsEx(&icex);     hCbx=CreateWindowEx(0        , WC_COMBOBOXEX        , NULL        ,  WS_BORDER | WS_VISIBLE |        WS_CHILD | CBS_DROPDOWN        , 0        , 0        , 400        , 200        , hWnd        , NULL        , hInst        , NULL);    createItem(hCbx);}
    File stdafx.h khi tạo project visual đã tạo sẵn cho bạn, nếu không có bạn có thể gõ thêm vào

    Mã:
    // stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently// #pragma once // Modify the following defines if you have to target a platform prior to the ones specified below.// Refer to MSDN for the latest info on corresponding values for different platforms.#ifndef WINVER              // Allow use of features specific to Windows XP or later.#define WINVER 0x0501       // Change this to the appropriate value to target other versions of Windows.#endif #ifndef _WIN32_WINNT        // Allow use of features specific to Windows XP or later.                   #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.#endif                       #ifndef _WIN32_WINDOWS      // Allow use of features specific to Windows 98 or later.#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.#endif #ifndef _WIN32_IE           // Allow use of features specific to IE 6.0 or later.#define _WIN32_IE 0x0600    // Change this to the appropriate value to target other versions of IE.#endif #define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers// Windows Header Files:#include <windows.h> // C RunTime Header Files#include <stdlib.h>#include <malloc.h>#include <memory.h>#include <tchar.h>  // TODO: reference additional headers your program requires here

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •