2013-05-20 1 views
2

J'utilise ce code pour créer un contrôle d'armature et introduire une bande avec barre d'outils dans la barre d'armature.
Mais lorsque la fenêtre est affichée, je ne peux pas voir la barre d'outils. et quand je vérifie la hauteur de la barre d'armature, dans cette ligne de code: int height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top; Je trouve que la hauteur des barres d'armature n'est que de 4 pixels.Création d'un contrôle d'armature et introduction d'une bande avec barre d'outils dans la barre d'armature

#include <windows.h> 
#include <stdlib.h> 
#include <CommCtrl.h> 
#pragma comment(lib, "comctl32.lib") 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
HINSTANCE instance; 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    instance = 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, MAKEINTRESOURCE(IDI_APPLICATION)); 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = L"Example"; 
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 

    RegisterClassEx(&wcex); 

    HWND hWnd = CreateWindow(L"Example", L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
     500, 500, NULL, NULL, hInstance, NULL); 

    // Initialize common controls. 
    INITCOMMONCONTROLSEX icex; 
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX); 
    icex.dwICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES; 
    InitCommonControlsEx(&icex); 

    HWND hwndRebar = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 
        0, 0, 100, 50, hWnd, NULL, instance, NULL); 

    // create toolbar 
    HWND hWndToolbar = CreateWindowEx(0 , TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_TOOLTIPS, 
      0, 0, 0, 0, hwndRebar, (HMENU)0, instance, NULL); 

    HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR16 | ILC_MASK, 3, 0); 

    SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList); 
    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); 

    TBBUTTON tbb[4] = 
    { 
     {0,0,TBSTATE_ENABLED,TBSTYLE_BUTTON}, 
     {1,1,TBSTATE_ENABLED,TBSTYLE_BUTTON}, 
     {2,2,TBSTATE_ENABLED,TBSTYLE_BUTTON}, 
    }; 

    SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&tbb); 

    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
    ShowWindow(hWndToolbar , SW_SHOW); 

    // Initialize band info. 
    REBARBANDINFO rbBand = { sizeof(REBARBANDINFO) }; 
    rbBand.fMask = RBBIM_STYLE | RBBIM_TEXT | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_COLORS; 

    rbBand.fStyle = RBBS_GRIPPERALWAYS; 

    // Get the height of the toolbar. 
    DWORD dwBtnSize = (DWORD)SendMessage(hWndToolbar, TB_GETBUTTONSIZE, 0,0); 

    // Set values unique to the band with the toolbar. 
    rbBand.clrFore = RGB(233, 233, 233); 
    rbBand.clrBack = RGB(200, 200, 200); 
    rbBand.lpText = TEXT(""); 
    rbBand.hwndChild = hWndToolbar; 
    rbBand.cyChild = LOWORD(dwBtnSize); 
    rbBand.cyMinChild = LOWORD(dwBtnSize); 
    rbBand.cxMinChild = 3 * HIWORD(dwBtnSize); 
    // The default width is the width of the buttons. 
    rbBand.cx = 0; 

    // Add the band 
    SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand); 

    // show the main window 
    ShowWindow(hWnd, nCmdShow); 

    // check the rebar size 
    WINDOWPLACEMENT wp; 
    GetWindowPlacement(hwndRebar, &wp); 
    int height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top; 

    MSG msg; 

    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 

    return (int) msg.wParam; 
} 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
     case WM_CREATE: 
      return 0; 

     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
} 

Répondre

2

Ainsi qu'il ressort des commentaires, la solution a été:

REBARBANDINFO rbBand; 
rbBand.cbSize = REBARBANDINFO_V3_SIZE; 
// initialize the rest here 

Cela semble affecter les anciennes versions de Windows (en particulier, XP), parce que le code d'origine compile et fonctionne bien sur Windows 7.

Cela a été mentionné dans les commentaires à une page MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/bb774393.aspx

1

Comme nous l'avons déjà dit, le membre cbSize du REBARBANDINFO struct nécessaire à régler.

Remarque concernant le code fourni ainsi:

  1. Les images sur les boutons de la barre d'outils ne se présentent pas. Juste après la création de la liste d'images que vous avez à faire l'appel suivant pour charger les images:

    SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL); 
    
  2. Le tableau de TBBUTTON est déclarée avec une taille de 4, mais il est seulement peuplé avec 3 articles. Techniquement, il devrait être déclaré comme suit:

    TBBUTTON tbb[3] 
    
Questions connexes