home *** CD-ROM | disk | FTP | other *** search
- #define STRICT
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <commctrl.h>
- #include "tbdemo2.h"
- #include <winver.h>
-
- /*****************************************
- Toolbar Demo Program by Eric Heimburg
- To compile, create a project, and place
- TBDEMO2.C (this file) and TBDEMO2.RC
- in the project's file list. If compiling
- with Watcom C/C++, also include the file
- \watcom\lib386\nt\comctl32.lib.
-
- Key to abbreviations:
- TB = Tool Bar
- CB = Combo Box
- EB = Edit Box
- IDX = Index
- CMD = Command ID
- *****************************************/
-
- /* global variables */
-
- HINSTANCE hInstance;
- HWND hMainWnd, hTB, hCB, hEB;
- LONG OldProc = 0;
- HBITMAP hPicBmp;
-
- /* toolbar definitions */
-
- #define CBWIDTH 125 /* width of combobox */
- #define CBEXTRAHEIGHT 200 /* amount CB should hang
- down below TB*/
- #define EBWIDTH 75 /* width of edit box */
- #define EBEXTRAHEIGHT 0 /* amount EB should hang
- down below TB*/
- #define ID_TB 10 /* Cmd. ID of toolbar */
- #define ID_CB 110 /* Cmd. ID of combobox */
-
- /* These specify the registry location (inside
- HKEY_CURRENT_USER) to which the toolbar configuration
- is to be saved and restored. */
-
- #define SAVERESTORE_SUBKEY "TBDEMO"
- #define SAVERESTORE_ENTRY "TBDEMO Configuration"
-
-
- /* Btns[] is the master TBBUTTON list. It contains info
- about every button that can appear on the toolbar. */
-
- TBBUTTON Btns[] =
- {
- {IDX_BOMB, CMD_BOMB, TBSTATE_ENABLED,
- TBSTYLE_CHECK, "", 0, 0},
- {IDX_GREENFLAG, CMD_GREENFLAG, TBSTATE_ENABLED,
- TBSTYLE_CHECK, "", 0, 0},
- {IDX_YELLOWFLAG,CMD_YELLOWFLAG,TBSTATE_ENABLED,
- TBSTYLE_CHECK, "", 0, 0},
- {IDX_PHONE, CMD_PHONE, TBSTATE_ENABLED,
- TBSTYLE_BUTTON, "", 0, 0},
- {IDX_FLOPPYDISK,CMD_FLOPPYDISK,TBSTATE_ENABLED,
- TBSTYLE_BUTTON, "", 0, 0},
- {IDX_FOLDER, CMD_FOLDER, TBSTATE_ENABLED,
- TBSTYLE_BUTTON, "", 0, 0},
- {IDX_OPENFOLDER,CMD_OPENFOLDER,TBSTATE_ENABLED,
- TBSTYLE_BUTTON, "", 0, 0},
- {IDX_DOCUMENTS, CMD_DOCUMENTS, TBSTATE_ENABLED,
- TBSTYLE_BUTTON, "", 0, 0},
- {EBWIDTH, CMD_EB, TBSTATE_ENABLED,
- TBSTYLE_SEP, "", 0, 0},
- {CBWIDTH, CMD_CB, TBSTATE_ENABLED,
- TBSTYLE_SEP, "", 0, 0}
- };
- #define NUMBTNS (sizeof(Btns) / sizeof(TBBUTTON))
-
-
- /* IdxStartBtns[] contains indices into the master TBBUTTON
- list. These are the controls that appear on the toolbar
- at startup, or when the toolbar is told to reset itself.*/
-
- #define NUMSTARTBTNS 7
- int IdxStartBtns[NUMSTARTBTNS] = { IDX_CB, IDX_BOMB, IDX_EB,
- IDX_FOLDER, IDX_GREENFLAG, IDX_YELLOWFLAG, IDX_DOCUMENTS};
-
-
-
- /**********************************************************
- RestoreToolbar - loads a saved toolbar configuration from
- the registry. Replaces TB_SAVERESTORE's
- restoration capability, which is flawed (see article).
-
- Parameters:
- hTB = handle of toolbar being restored
- List = pointer to program's master TBBUTTON list
- nList = number of entries in List
- hKey = handle of key to get value from
- (typically HKEY_CURRENT_USER)
- SubKey = text name of subkey to get value from
- (or NULL to use hKey)
- Value = text name of value
-
- Note that the last three parameters directly correspond
- to the three fields of the TBSAVEPARAMS structure, which
- is passed to TB_SAVERESTORE.
- **********************************************************/
-
- void RestoreToolbar(HWND hTB, TBBUTTON *List, int nList,
- HKEY hKey, LPCSTR SubKey, LPCSTR ValueName)
- {
- TBBUTTON Btn, DefBtn;
- HKEY hOpenKey = 0;
- DWORD BufSize, dwType;
- DWORD *Buffer;
- int loop, loop2, temp;
-
- if (RegOpenKeyEx(hKey, SubKey, 0, KEY_ALL_ACCESS,
- &hOpenKey) != ERROR_SUCCESS)
- return; /* key doesn't exist */
-
- memset(&DefBtn, 0, sizeof(DefBtn));
- DefBtn.fsState = TBSTATE_ENABLED;
- DefBtn.fsStyle = TBSTYLE_SEP;
-
- /*call RegQueryValueEx once to get size of stored value*/
- if ((RegQueryValueEx(hOpenKey, ValueName, 0, 0, 0,
- &BufSize) == ERROR_SUCCESS) && (BufSize != 0))
- {
- Buffer = (DWORD*)malloc(BufSize);
- RegQueryValueEx(hOpenKey, ValueName, 0, &dwType,
- (BYTE*)Buffer, &BufSize);
-
- /* Ok, the entry exists. Clear off the toolbar... */
- temp = SendMessage(hTB, TB_BUTTONCOUNT, 0, 0);
- for (loop = 0; loop < temp; loop++)
- SendMessage(hTB, TB_DELETEBUTTON, 0, 0);
-
- /* ... and add the new buttons. */
- for (loop = 0; loop < (BufSize / sizeof(DWORD)); loop++)
- {
- Btn = DefBtn;
- for (loop2 = 0; loop2 < nList; loop2++)
- if ((List[loop2].idCommand) == Buffer[loop])
- {
- Btn = Btns[loop2];
- break;
- }
- SendMessage(hTB, TB_ADDBUTTONS, 1, (LPARAM)&Btn);
- }
-
- /* deallocate the buffer */
- free((void*)Buffer);
- }
- RegCloseKey(hOpenKey);
- }
-
-
- /**********************************************************
- bVersionCorrect - function to check the version of the
- common control DLL. Returns TRUE if it is the correct
- version. This function only checks the DLL the first
- time it is called; it uses a static variable to store
- that result, and immediately return it on subsequent
- calls of the function.
- **********************************************************/
-
- BOOL bVersionCorrect(void)
- {
- DWORD dwDummy, dwSize;
- char *ptr;
- VS_FIXEDFILEINFO *p;
- static int bVersionOk = -1;
-
- if (bVersionOk == -1) {
- dwSize = GetFileVersionInfoSize("comctl32.dll", &dwDummy);
- if (!dwSize) return FALSE; /* couldn't find file! */
- ptr = (char*)malloc(dwSize);
- GetFileVersionInfo("comctl32.dll", 0, dwSize, ptr);
- VerQueryValue(ptr, "\\", (void**)&p, (UINT*)&dwDummy);
- bVersionOk = ((p->dwFileVersionMS == 0x40000) &&
- (p->dwFileVersionLS == 950) &&
- (p->dwProductVersionMS == 0x40000) &&
- (p->dwProductVersionLS == 950));
- free((void*)ptr);
- }
- return (BOOL)bVersionOk;
- }
-
-
- /**********************************************************
- FixTBCntrlPos - function to position a control in a
- toolbar over the control's "host" separator.
-
- Parameters:
- hTB = handle of the toolbar
- hCntrl = handle of the control to be positioned
- HostCmd = command ID of the control's host separator
- YExtra = amount, in pixels, that the control
- (indicated by hCntrl) should hang extend down
- below the toolbar. This is primarily useful
- for comboboxes, so that when they are clicked
- on, they have a larger area in which to
- display their list.
- **********************************************************/
-
- void FixTBCntrlPos(HWND hTB, HWND hCntrl, int HostCmd,
- int YExtra)
- {
- RECT r;
- int n = SendMessage(hTB, TB_COMMANDTOINDEX, HostCmd, 0);
- if (n == -1)
- ShowWindow(hCntrl, SW_HIDE);
- else {
- SendMessage(hTB, TB_GETITEMRECT, n, (LPARAM)&r);
- SetWindowPos(hCntrl, NULL, r.left, r.top,
- r.right - r.left, r.bottom - r.top + YExtra,
- SWP_NOZORDER | SWP_SHOWWINDOW);
- }
- }
-
-
- /**********************************************************
- CustomizeProc - the window procedure used when
- subclassing the "Customize Toolbar" dialog. See article
- for more information.
- **********************************************************/
-
- LRESULT CALLBACK CustomizeProc(HWND hwnd, UINT Msg,
- WPARAM WParam, LPARAM LParam)
- {
- DWORD result, itemID;
- DRAWITEMSTRUCT *pDraw;
-
- if (Msg == WM_DRAWITEM) {
- pDraw = (DRAWITEMSTRUCT*)LParam;
- if (LOWORD(pDraw->itemData) == 0xFFFF) {
- itemID = HIWORD(pDraw->itemData);
- if ((itemID==IDX_EB) || (itemID==IDX_CB)) {
- SendMessage(pDraw->hwndItem,
- LB_SETITEMDATA, pDraw->itemID,
- MAKELONG(itemID, HIWORD(pDraw->itemData)));
- InvalidateRect(pDraw->hwndItem, NULL, FALSE);
- }
- }
- }
- result = CallWindowProc((WNDPROC)OldProc, hwnd,
- Msg, WParam, LParam);
- if (Msg == WM_DESTROY) {
- SetWindowLong(hwnd, GWL_WNDPROC, OldProc);
- OldProc = (DWORD)NULL;
- }
- return result;
- }
-
-
- /**********************************************************
- DoToolbarNotify - procedure to process notifications
- from the toolbar.
-
- Parameters:
- pHdr = pointer to TBNOTIFY structure (sent as the
- LPARAM of the WM_NOTIFY message).
- **********************************************************/
-
- LRESULT DoToolbarNotify(TBNOTIFY *pHdr)
- {
- static char buf[128]; /* used in TBN_GETBUTTONINFO */
- HWND htemp; /* used when finding HWND of customize dlg. */
- UINT dwtemp;
- TOOLTIPTEXT *pTT;
-
- TBBUTTON StartBtns[NUMSTARTBTNS];
- int loop, count;
-
- if ((pHdr->hdr.hwndFrom==hTB) || (pHdr->hdr.code==TTN_NEEDTEXT)) {
- switch (pHdr->hdr.code) {
- case TBN_QUERYDELETE:
- return TRUE;
-
- case TBN_QUERYINSERT:
- if ((OldProc == 0) && (bVersionCorrect())) {
- htemp = FindWindow("#32770", "Customize Toolbar");
- if (htemp)
- OldProc = SetWindowLong(htemp, GWL_WNDPROC,
- (LONG)CustomizeProc);
- }
- return TRUE;
-
- case TBN_GETBUTTONINFO:
- if (pHdr->iItem < NUMBTNS) {
- pHdr->tbButton = Btns[pHdr->iItem];
- dwtemp = SendMessage(pHdr->hdr.hwndFrom,
- TB_GETSTATE, Btns[pHdr->iItem].idCommand, 0);
- if (dwtemp != -1)
- pHdr->tbButton.fsState = (BYTE)dwtemp;
- if (pHdr->pszText == NULL) {
- pHdr->pszText = buf;
- pHdr->cchText = sizeof(buf);
- }
- if (LoadString(hInstance, Btns[pHdr->iItem].idCommand,
- pHdr->pszText, pHdr->cchText) == 0)
- strcpy(pHdr->pszText, "Unknown");
- return TRUE;
- }
- else
- return FALSE;
-
- case TBN_RESET:
- /* reset toolbar to startup defaults. remove btns
- from toolbar, then put startup buttons back on */
- count = SendMessage(hTB, TB_BUTTONCOUNT, 0, 0);
- for (loop = 0; loop < count; loop++)
- SendMessage(hTB, TB_DELETEBUTTON, 0, 0);
-
- for (loop = 0; loop < NUMSTARTBTNS; loop++)
- StartBtns[loop] = Btns[IdxStartBtns[loop]];
- SendMessage(hTB, TB_ADDBUTTONS, NUMSTARTBTNS, (LPARAM)StartBtns);
-
- /* "break" statement purposefully missing */
- case TBN_TOOLBARCHANGE:
- FixTBCntrlPos(hTB, hCB, CMD_CB,CBEXTRAHEIGHT);
- FixTBCntrlPos(hTB, hEB, CMD_EB, EBEXTRAHEIGHT);
- break;
-
- case TTN_NEEDTEXT:
- pTT = (TOOLTIPTEXT*)pHdr;
- pTT->lpszText = pTT->szText;
- pTT->hinst = NULL;
- if (LoadString(hInstance, pTT->hdr.idFrom,
- pTT->szText, sizeof(pTT->szText)) == 0)
- strcpy(pTT->szText, "Unknown");
- break;
- }
- }
- return 0;
- }
-
-
- /**********************************************************
- MainWndProc - main window procedure
- **********************************************************/
-
- LRESULT CALLBACK MainWndProc( HWND hwnd, UINT Msg,
- WPARAM WParam, LPARAM LParam )
- {
- TBBUTTON StartBtns[NUMSTARTBTNS];
- int loop;
- TBSAVEPARAMS sp;
- sp.hkr = HKEY_CURRENT_USER;
- sp.pszSubKey = SAVERESTORE_SUBKEY;
- sp.pszValueName = SAVERESTORE_ENTRY;
-
- switch (Msg)
- {
- case WM_CREATE:
- /* fill startup button array, create toolbar */
- for (loop = 0; loop < NUMSTARTBTNS; loop++)
- StartBtns[loop] = Btns[IdxStartBtns[loop]];
- hTB = CreateToolbarEx(hwnd,
- WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|CCS_ADJUSTABLE,
- ID_TB, NUM_PICTURES, hInstance, RES_PICTURES,
- StartBtns, NUMSTARTBTNS, 0, 0,
- PICTURE_WIDTH, PICTURE_HEIGHT, sizeof(TBBUTTON));
-
- /* create combo box, fill it, attach it to toolbar */
- hCB = CreateWindow("COMBOBOX", "",
- WS_CHILD | WS_VSCROLL | WS_BORDER | CBS_DROPDOWNLIST,
- 0, 0, 0, 0, hwnd, (HMENU)IDX_CB, hInstance, NULL);
- SendMessage(hCB, CB_DIR, 0, (LPARAM)"*.*");
- SetParent(hCB, hTB);
-
- /* position combobox over host separator */
- FixTBCntrlPos(hTB, hCB, CMD_CB, CBEXTRAHEIGHT);
-
- /* create edit box, attach it to toolbar */
- hEB = CreateWindow("EDIT", "Edit Box",
- WS_CHILD | WS_BORDER, 0, 0, 0, 0,
- hwnd, (HMENU)IDX_EB, hInstance, NULL);
- SetParent(hEB, hTB);
-
- /* position edit box over host separator */
- FixTBCntrlPos(hTB, hEB, CMD_EB, EBEXTRAHEIGHT);
- break;
-
- case WM_DESTROY:
- PostQuitMessage( 0 );
- break;
-
- case WM_SIZE:
- SendMessage(hTB, WM_SIZE, 0, 0);
- break;
-
- case WM_COMMAND:
- switch (LOWORD(WParam))
- {
- case CMD_SAVE:
- SendMessage(hTB, TB_SAVERESTORE, 1, (LPARAM)&sp);
- break;
-
- case CMD_RESTORE:
- RestoreToolbar(hTB, Btns, NUM_PICTURES,
- HKEY_CURRENT_USER, SAVERESTORE_SUBKEY,
- SAVERESTORE_ENTRY);
- FixTBCntrlPos(hTB, hCB, CMD_CB, CBEXTRAHEIGHT);
- FixTBCntrlPos(hTB, hEB, CMD_EB, EBEXTRAHEIGHT);
- break;
-
- case CMD_CUSTOMIZE:
- SendMessage(hTB, TB_CUSTOMIZE, 0, 0);
- break;
-
- case CMD_EXIT:
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- break;
- }
- break;
-
- case WM_NOTIFY:
- return DoToolbarNotify((TBNOTIFY*)LParam);
-
- default:
- return DefWindowProc( hwnd, Msg, WParam, LParam );
- }
- return 0;
- }
-
-
- /**********************************************************
- WinMain - program's entry point
- **********************************************************/
-
- #pragma argsused
- int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst,
- LPSTR CmdLine, int nCmdShow )
- {
- WNDCLASS wndclass;
- MSG msg;
-
- hInstance = hInst;
- if (!hPrevInst) {
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = MainWndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon( NULL, IDI_WINLOGO );
- wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
- wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
- wndclass.lpszMenuName = "MainMenu";
- wndclass.lpszClassName = "TBDEMO";
- if ( !RegisterClass( &wndclass ) )
- return 0;
- }
-
- hMainWnd = CreateWindowEx( 0, "TBDEMO", "Toolbar Demo",
- WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT,
- 0, NULL, NULL, hInstance, NULL );
-
- ShowWindow(hMainWnd, nCmdShow);
- UpdateWindow(hMainWnd);
-
- while( GetMessage( &msg, NULL, 0, 0 ) ) {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- return msg.wParam;
- }
-
-