#include "stdafx.h"
#include <commctrl.h>
BOOL CreateHeaderRowButtons(HWND hwndHeader, TBBUTTON* pLeft, TBBUTTON* pRight)
{
if (pLeft == NULL && pRight == NULL)
{
ASSERT(FALSE); // you must have either a left or a right button, you cannot have neither
return FALSE;
}
// tell the toolbar the size of the button structure
SendMessage(hwndHeader, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
// now create an empty button
TBBUTTON btnEmpty = { -1 /*bitmap*/,0 /*command*/,TBSTATE_INDETERMINATE, TBSTYLE_AUTOSIZE, {0,0} /*reserved*/, 0 /*data*/,-1 /*string*/};
ZeroMemory(&btnEmpty, sizeof(btnEmpty));
btnEmpty.iBitmap = -1;
btnEmpty.idCommand = 0;
btnEmpty.fsState = TBSTATE_INDETERMINATE;
btnEmpty.fsStyle = TBSTYLE_AUTOSIZE;
btnEmpty.dwData = 0;
btnEmpty.iString = -1;
// we will have most three buttons at least two buttons.
TBBUTTON buttons[3];
int iCurrentButton = 0;
// if there is a left button then copy it into the first button index
if (pLeft != NULL)
{
CopyMemory(&buttons[iCurrentButton++], pLeft, sizeof(TBBUTTON));
}
// now copy the empty button into either the first index if there is
// no left button or the middle index if thee is aleft button
CopyMemory(&buttons[iCurrentButton++], &btnEmpty, sizeof(TBBUTTON));
// finally copy the right button into the middle index if there
// is no left button or copy it into the third index if there
// is a left button.
if (pRight != NULL)
{
CopyMemory(&buttons[iCurrentButton++], pRight, sizeof(TBBUTTON));
}
// Now add the 2 or three buttons into the toolbar
VERIFY(SendMessage(hwndHeader, TB_ADDBUTTONS, iCurrentButton, (LPARAM)&buttons));
// the final thing left to do then is calulate the size of the
// middle button based on the sizes of the left and/or right buttons.
RECT rcLeft;
ZeroMemory(&rcLeft, sizeof(rcLeft));
RECT rcRight;
ZeroMemory(&rcRight, sizeof(rcRight));
// if there is a left button then get its dimensions.
// Note that we do not use TBN_GETBUTTONINFO becuase this does
// not return the correct size. Proberly need to find out why though!
if (pLeft != NULL)
{
VERIFY(SendMessage(hwndHeader, TB_GETITEMRECT, 0, (LPARAM)&rcLeft));
}
else
; // no left bitmap
// we do the same for the right button if there is one
if (pRight != NULL)
{
VERIFY(SendMessage(hwndHeader, TB_GETITEMRECT, pLeft == NULL ? 1 : 2, (LPARAM)&rcRight));
}
// Next we get the entire width of the header bar
RECT rc;
VERIFY(GetClientRect(hwndHeader, &rc));
// and calculate and set its size to be the width of the bar less the width of the left and right buttons
// if the buttons are present.
TBBUTTONINFO tbiMiddle;
tbiMiddle.cbSize = sizeof(TBBUTTONINFO);
tbiMiddle.dwMask = TBIF_SIZE;
tbiMiddle.cx = (WORD)((rc.right - rc.left) - (rcRight.right - rcRight.left) - (rcLeft.right - rcLeft.left));
VERIFY(SendMessage(hwndHeader, TB_SETBUTTONINFO, (WPARAM)0, (LPARAM)&tbiMiddle));
return TRUE;
}
HWND CreateStatusBar(HINSTANCE hInst, HWND hwnd)
{
RECT rc;
VERIFY(GetClientRect(hwnd, &rc));
HWND hwndStatus = CreateWindow(STATUSCLASSNAME, _T("status"), CCS_NOPARENTALIGN | WS_CHILD | WS_VISIBLE, rc.left, rc.bottom-26-30, rc.right - rc.left, 30, hwnd, NULL, hInst, NULL);
ASSERT(IsWindow(hwndStatus));
SendMessage(hwndStatus, SB_SETTEXT, SBT_NOBORDERS, (LPARAM)_T("Status bar text here"));
InvalidateRect(hwndStatus, NULL, TRUE);
return hwndStatus;
}
HWND CreateHeaderBar(HINSTANCE hInst, HWND hwnd)
{
RECT rc;
VERIFY(GetClientRect(hwnd, &rc));
HWND hwndHeaderControl = CreateWindow(TOOLBARCLASSNAME, _T("Headerbar control"), TBSTYLE_CUSTOMERASE | WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_LIST | WS_BORDER, rc.left, rc.top, rc.right - rc.left, 44, hwnd, NULL, hInst, NULL);
ASSERT(IsWindow(hwndHeaderControl));
return hwndHeaderControl;
}
void ShowItemSelected(HWND hwndTree, HTREEITEM hti)
{
if (hti == 0)
{
MessageBox(GetForegroundWindow(), _T("User cancelled selection"), _T("Caption"), MB_OK | MB_ICONINFORMATION);
}
else
{
TCHAR buffer[255];
LPTSTR lpszStart = _tcscpy(buffer, _T("User selected ")) + 14;
TV_ITEM tvi;
tvi.mask = TVIF_TEXT;
tvi.hItem = hti;
tvi.pszText = lpszStart;
tvi.cchTextMax = 255;
VERIFY(TreeView_GetItem(hwndTree, &tvi));
MessageBox(GetForegroundWindow(), buffer, _T("Caption"), MB_OK | MB_ICONINFORMATION);
}
}