11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6. Dialog Box<br />

}<br />

TOOLTIPTEXT *pTTT=(TOOLTIPTEXT *)pNMHDR;<br />

UINT nID=pNMHDR->idFrom;<br />

if(pTTT->uFlags & TTF_IDISHWND)<br />

{<br />

nID=::GetDlgCtrlID((HWND)nID);<br />

if(nID)<br />

{<br />

pTTT->lpszText=MAKEINTRESOURCE(nID);<br />

pTTT->hinst=AfxGetResourceHandle();<br />

return TRUE;<br />

}<br />

}<br />

return FALSE;<br />

First the ID of the control is obtained. If the control is a window, this ID will be a valid handle. We can<br />

retrieve the control’s resource ID by calling fucntion ::GetDlgCtrlID(…). Next, the resource ID is<br />

assigned to member lpszText, and the application’s instance handle is assigned to member hinst.<br />

With this method, we can only implement a tool tip which contains maximum of 80 characters. To<br />

implement longer tool tips, we need to provide our own buffer and assign its address to member lpszText.<br />

In this case, we do not need to assign the application’s instance handle to member hinst.<br />

After adding the above implementation, we can just add string resources whose IDs are the same with<br />

the resource IDs of the controls. By doing this, the tool tip will automatically implemented for them.<br />

6.8 Tool Bar and Status Bar in Dialog Box<br />

By default, the dialog box does not support tool bar and status bar implementation. Because a dialog<br />

box can contain various intuitive controls, it is often not necessary to implement extra tool bar and status<br />

bar. But sometimes the tool bar and status bar are helpful, especially when we want to implement a row of<br />

buttons with the same size. In this case, we can also easily implement the tool tips and flybys on the status<br />

bar.<br />

Frame Window<br />

In a standard SDI or MDI application, tool bar and status bar can be implemented by declaring<br />

CToolBar and CStatusBar type variables in class CMainFrame (They will be created in function<br />

CMainFrame::OnCreate(…)). In a dialog-based application, the frame window is the dialog box itself, so we<br />

need to embed CToolBar and CStatusBar type variables in the CDialog derived class and create them in<br />

function CDialog::OnInitDialog().<br />

However, unlike CFrameWnd, class CDialog is not specially designed to work together with status bar<br />

and tool bar, so it lacks some features that are owned by class CFrameWnd: first, it does not support<br />

automatic tool tip implementation, so we have to write TTN_NEEDTEXT message handler for displaying tool<br />

tips; second, it does not support flyby implementation, so we also need to add other message handlers in<br />

order to enable flybys.<br />

Flyby Related Messages<br />

In <strong>MFC</strong>, there are two un-documented messages that are used for flyby implementation. When a flyby<br />

text for certain control needs to be displayed, the frame window will receive message<br />

WM_SETMESSAGESTRING. Also, when a flyby needs to be removed, the frame window will receive another<br />

message: WM_POPMESSAGESTRING.<br />

Tool Bar Resource<br />

Sample 6.8-1\DB demonstrates how to implement tool bar and status bar in a dialog based application.<br />

The sample is generated by Application Wizard, with a tool bar resource IDD_DB_DIALOG added later on.<br />

This ID is the same with the dialog template ID, which is convenient for tool bar implementation.<br />

159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!