?? wtl for mfc programmers, part iii.mht
字號:
<P>On the next page, uncheck <I>Rebar</I> so the wizard creates a =
normal=20
toolbar:</P>
<P><IMG height=3D387 alt=3D" [AppWizard pg 2 - 21K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/appwiz2.png" =
width=3D477=20
align=3Dbottom border=3D0></P>
<P>After copying the clock code from the app in Part II, the new =
app looks=20
like this:</P>
<P><IMG height=3D187 alt=3D" [default toobar - 5K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/defaultapp.png" =
width=3D267=20
align=3Dbottom border=3D0></P>
<H3><A name=3Dcreatebars></A>How CMainFrame creates the bars</H3>
<P>The AppWizard puts more code in =
<CODE>CMainFrame::OnCreate()</CODE> in=20
this project. Its job is to create the bars and tell=20
<CODE>CUpdateUI</CODE> to update the toolbar =
buttons.</P><PRE>LRESULT CMainFrame::OnCreate(UINT <SPAN =
class=3Dcpp-comment>/*uMsg*/</SPAN>, WPARAM <SPAN =
class=3Dcpp-comment>/*wParam*/</SPAN>,=20
LPARAM <SPAN =
class=3Dcpp-comment>/*lParam*/</SPAN>, BOOL& <SPAN =
class=3Dcpp-comment>/*bHandled*/</SPAN>)
{
CreateSimpleToolBar();
CreateSimpleStatusBar();
m_hWndClient =3D m_view.Create(...);
<SPAN class=3Dcpp-comment>// ...</SPAN>
<SPAN class=3Dcpp-comment>// register object for message filtering =
and idle updates</SPAN>
CMessageLoop* pLoop =3D _Module.GetMessageLoop();
ATLASSERT(pLoop !=3D NULL);
pLoop->AddMessageFilter(<SPAN class=3Dcpp-keyword>this</SPAN>);
pLoop->AddIdleHandler(<SPAN class=3Dcpp-keyword>this</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}</PRE>
<P>The new code here is at the beginning.=20
<CODE>CFrameWindowImpl::CreateSimpleToolBar()</CODE> creates a new =
toolbar=20
using the toolbar resource IDR_MAINFRAME and stores its handle in=20
<CODE>m_hWndToolBar</CODE>. Here is the code for=20
<CODE>CreateSimpleToolBar()</CODE>:</P><PRE>BOOL =
CFrameWindowImpl::CreateSimpleToolBar(
UINT nResourceID =3D <SPAN class=3Dcpp-literal>0</SPAN>,=20
DWORD dwStyle =3D ATL_SIMPLE_TOOLBAR_STYLE,=20
UINT nID =3D ATL_IDW_TOOLBAR)
{
ATLASSERT(!::IsWindow(m_hWndToolBar));
=20
<SPAN class=3Dcpp-keyword>if</SPAN>(nResourceID =3D=3D <SPAN =
class=3Dcpp-literal>0</SPAN>)
nResourceID =3D T::GetWndClassInfo().m_uCommonResourceID;
=20
m_hWndToolBar =3D T::CreateSimpleToolBarCtrl(m_hWnd, nResourceID, =
TRUE, <BR> dwStyle, nID);
<SPAN class=3Dcpp-keyword>return</SPAN> (m_hWndToolBar !=3D NULL);
}</PRE>
<P>The parameters are:</P>
<DL>
<DT><CODE>nResourceID</CODE>=20
<DD>ID of the toolbar resource to use. The default of 0 means to =
use the=20
ID specified in the <CODE>DECLARE_FRAME_WND_CLASS</CODE> macro. =
This is=20
IDR_MAINFRAME in the wizard-generated code.=20
<DT><CODE>dwStyle</CODE>=20
<DD>Styles for the toolbar. The default value=20
<CODE>ATL_SIMPLE_TOOLBAR_STYLE</CODE> is defined as=20
<CODE>TBSTYLE_TOOLTIPS</CODE> plus the usual child and visible =
styles.=20
This makes the toolbar create a tooltip control for use when the =
cursor=20
hovers over a button.=20
<DT><CODE>nID</CODE>=20
<DD>Window ID for the toolbar, you will usually use the default =
value.=20
</DD></DL>
<P><CODE>CreateSimpleToolBar()</CODE> checks that a toolbar hasn't =
been=20
created yet, then calls <CODE>CreateSimpleToolBarCtrl()</CODE> to =
actually=20
create the control. The handle returned by=20
<CODE>CreateSimpleToolBarCtrl()</CODE> is saved in=20
<CODE>m_hWndToolBar</CODE>. <CODE>CreateSimpleToolBarCtrl()</CODE> =
reads=20
the resource and creates toolbar buttons accordingly, then returns =
the=20
toolbar's window handle. The code to do that is rather long, so I =
won't=20
cover it here. You can find it in atlframe.h if you're =
interested.</P>
<P>The next call in <CODE>OnCreate()</CODE> is=20
<CODE>CFrameWindowImpl::CreateSimpleStatusBar()</CODE>. This =
creates a=20
status bar and stores its handle in <CODE>m_hWndStatusBar</CODE>. =
Here is=20
the code:</P><PRE>BOOL CFrameWindowImpl::CreateSimpleStatusBar(
UINT nTextID =3D ATL_IDS_IDLEMESSAGE,=20
DWORD dwStyle =3D ... SBARS_SIZEGRIP,=20
UINT nID =3D ATL_IDW_STATUS_BAR)
{
TCHAR szText[<SPAN class=3Dcpp-literal>128</SPAN>]; <SPAN =
class=3Dcpp-comment>// max text lentgth is 127 for status bars</SPAN>
szText[<SPAN class=3Dcpp-literal>0</SPAN>] =3D <SPAN =
class=3Dcpp-literal>0</SPAN>;
::LoadString(_Module.GetResourceInstance(), nTextID, szText, <SPAN =
class=3Dcpp-literal>128</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> =
CreateSimpleStatusBar(szText, dwStyle, nID);
}</PRE>
<P>This loads a string from the string table, which will be shown =
in the=20
status bar. The parameters are:</P>
<DL>
<DT><CODE>nTextID</CODE>=20
<DD>Resource ID of the string to be initially shown in the =
status bar.=20
The AppWizard generates the string "Ready" with ID =
ATL_IDS_IDLEMESSAGE.=20
<DT><CODE>dwStyle</CODE>=20
<DD>Styles for the status bar. The default includes=20
<CODE>SBARS_SIZEGRIP</CODE> to have a resizing gripper added to =
the=20
bottom-right corner.=20
<DT><CODE>nID</CODE>=20
<DD>Window ID for the status bar, you will usually use the =
default=20
value. </DD></DL>
<P><CODE>CreateSimpleStatusBar()</CODE> calls the other overload =
to do the=20
work:</P><PRE>BOOL CFrameWindowImpl::CreateSimpleStatusBar(
LPCTSTR lpstrText,
DWORD dwStyle =3D ... SBARS_SIZEGRIP,
UINT nID =3D ATL_IDW_STATUS_BAR)
{
ATLASSERT(!::IsWindow(m_hWndStatusBar));
m_hWndStatusBar =3D ::CreateStatusWindow(dwStyle, lpstrText, m_hWnd, =
nID);
<SPAN class=3Dcpp-keyword>return</SPAN> (m_hWndStatusBar !=3D NULL);
}</PRE>
<P>This version checks that a status bar has not been created yet, =
then=20
calls <CODE>CreateStatusWindow()</CODE> to create the status bar. =
The=20
status bar's handle is then stored in =
<CODE>m_hWndStatusBar</CODE>.</P>
<H3><A name=3Dbarsshowhide></A>Showing and hiding the bars</H3>
<P><CODE>CMainFrame</CODE> also has a <I>View</I> menu with two =
commands=20
to show/hide the toolbar and status bar. These commands have IDs=20
<CODE>ID_VIEW_TOOLBAR</CODE> and <CODE>ID_VIEW_STATUS_BAR</CODE>.=20
<CODE>CMainFrame</CODE> has handlers for both commands that show =
or hide=20
the corresponding bar. Here is the <CODE>OnViewToolBar()</CODE>=20
handler:</P><PRE>LRESULT CMainFrame::OnViewToolBar(WORD <SPAN =
class=3Dcpp-comment>/*wNotifyCode*/</SPAN>, WORD <SPAN =
class=3Dcpp-comment>/*wID*/</SPAN>,=20
HWND <SPAN =
class=3Dcpp-comment>/*hWndCtl*/</SPAN>, BOOL& <SPAN =
class=3Dcpp-comment>/*bHandled*/</SPAN>)
{
BOOL bVisible =3D !::IsWindowVisible(m_hWndToolBar);
::ShowWindow(m_hWndToolBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
UISetCheck(ID_VIEW_TOOLBAR, bVisible);
UpdateLayout();
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}</PRE>
<P>This toggles the visible state of the bar, toggles the check =
mark next=20
to the <I>View|Toolbar</I> menu item, then calls=20
<CODE>UpdateLayout()</CODE> to position the bar (if it is becoming =
visible) and resize the view window.</P>
<H3><A name=3Dbuiltins></A>Built-in features of the bars</H3>
<P>MFC's framework provides some nice features in its toolbars and =
status=20
bars, such as tooltips for toolbar buttons and flyby help for menu =
items.=20
WTL has comparable features implemented in =
<CODE>CFrameWindowImpl</CODE>.=20
Below are screen shots showing both the tooltip and flyby =
help.</P>
<P><IMG height=3D233 alt=3D" [Toolbar button tooltip - 4K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/tbtooltip.png" =
width=3D291=20
align=3Dbottom border=3D0> <IMG height=3D233=20
alt=3D" [Status bar flyby help - 5K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/tbflyby.png" =
width=3D291=20
align=3Dbottom border=3D0></P>
<P><CODE>CFrameWindowImplBase</CODE> has two message handlers that =
are=20
used for these features. <CODE>OnMenuSelect()</CODE> handles=20
<CODE>WM_MENUSELECT</CODE>, and it finds the flyby help string =
just like=20
MFC does - it loads a string resource with the same ID as the=20
currently-selected menu item, looks for a <CODE>\n</CODE> =
character in the=20
string, and uses the text preceding the <CODE>\n</CODE> as the =
flyby help.=20
<CODE>OnToolTipTextA()</CODE> and <CODE>OnToolTipTextW()</CODE> =
handle=20
<CODE>TTN_GETDISPINFOA</CODE> and <CODE>TTN_GETDISPINFOW</CODE>=20
respectively to provide tooltip text for toolbar buttons. Those =
handlers=20
load the same string as <CODE>OnMenuSelect()</CODE>, but use the =
text=20
<I>following</I> the <CODE>\n</CODE>. (As a side note,=20
<CODE>OnMenuSelect()</CODE> and <CODE>OnToolTipTextA()</CODE> are =
not=20
DBCS-safe, as they do not check for DBCS lead/trail bytes when =
looking for=20
the <CODE>\n</CODE>.) Here's an example of a toolbar button and =
its=20
associated help string:</P>
<P><IMG height=3D320 alt=3D" [Toolbar button and help string - 9K] =
"=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/tbbtntext.png" =
width=3D502=20
align=3Dbottom border=3D0></P>
<H3><A name=3Ddiffstyles></A>Creating a toolbar with a different =
style</H3>
<P>If you don't like to have 3D buttons on your toolbar (even =
though flat=20
UI elements are worse from a usability perspective), then you can =
alter=20
the toolbar style by passing the style to=20
<CODE>CreateSimpleToolBar()</CODE>. For example, to create a =
toolbar that=20
resembles the ones in Internet Explorer, use this code in=20
<CODE>CMainFrame::OnCreate()</CODE>:</P><PRE> =
CreateSimpleToolBar ( <SPAN class=3Dcpp-literal>0</SPAN>, =
ATL_SIMPLE_TOOLBAR_STYLE |=20
TBSTYLE_FLAT | TBSTYLE_LIST );</PRE>
<P>Note that if you have the AppWizard add a manifest to your app =
so it=20
uses common controls version 6 on XP, you don't have a choice with =
the=20
buttons - toolbars always use flat buttons even if you don't =
specify the=20
<CODE>TBSTYLE_FLAT</CODE> style when the toolbar is created.</P>
<H2><A name=3Dtbeditor></A>The Toolbar Editor</H2>
<P>The AppWizard creates several default buttons as we saw =
earlier. Only=20
the <I>About</I> button is hooked up, however. You can edit the =
toolbar=20
just as you do with an MFC project; the editor modifies the =
toolbar=20
resource that <CODE>CreateSimpleToolBarCtrl()</CODE> uses to build =
the=20
bar. Here's how the AppWizard-generated toolbar looks in the =
editor:</P>
<P><IMG height=3D211 alt=3D" [default toolbar in the editor - 7K] =
"=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/tbeditor.png" =
width=3D490=20
align=3Dbottom border=3D0></P>
<P>For our clock app, we'll add two buttons to change the colors =
in the=20
view window, and two buttons that show/hide the toolbar and status =
bar.=20
Here's our new toolbar:</P>
<P><IMG height=3D175 alt=3D" [New toolbar buttons - 5K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC3/newtb.png" =
width=3D438=20
align=3Dbottom border=3D0></P>
<P>The buttons are:</P>
<UL>
<LI><CODE>IDC_CP_COLORS</CODE>: Change the view to CodeProject =
colors=20
<LI><CODE>IDC_BW_COLORS</CODE>: Change the view to black and =
white=20
<LI><CODE>ID_VIEW_STATUS_BAR</CODE>: Show or hide the status bar =
<LI><CODE>ID_VIEW_TOOLBAR</CODE>: Show or hide the toolbar =
</LI></UL>
<P>The first two buttons also have items on the <I>View</I> menu. =
They=20
call a new function in the view class, <CODE>SetColors()</CODE>. =
Each one=20
passes a foreground and background color that the view will use =
for its=20
clock display. Handling these two buttons is no different from =
handling=20
menu items with the <CODE>COMMAND_ID_HANDLER_EX</CODE> macro; =
check out=20
the sample project if you want to see the details of the message =
handlers.=20
In the next section, I'll cover UI updating the View Status Bar =
and View=20
Toolbar buttons so they reflect the current state of the bars.</P>
<H2><A name=3Duiudpatebtns></A>UI Updating Toolbar Buttons</H2>
<P>The AppWizard-generated <CODE>CMainFrame</CODE> already has UI =
update=20
handlers that check and uncheck the <I>View|Toolbar</I> and =
<I>View|Status=20
Bar</I> menu items. This is done just as the app in Part II - =
there are UI=20
update macros for each command in =
<CODE>CMainFrame</CODE>:</P><PRE>BEGIN_UPDATE_UI_MAP(CMainFrame)
UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
END_UPDATE_UI_MAP()</PRE>
<P>Our clock app has toolbar buttons with those same IDs, so the =
first=20
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -