📄 vcg11.htm
字号:
<HTML>
<HEAD>
<TITLE>vcg11.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">
<LINK REL="Next" HREF="vcg12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg12.htm">
<LINK REL="Previous" HREF="vcg10.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg10.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcg10.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg10.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>
<A HREF="vcg12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg12.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E65" >The Win32 Common Controls</A>
<UL>
<LI>
<A HREF="#E69E177" >The Status Bar Control</A>
<LI>
<A HREF="#E69E178" >The RichText Control</A>
<LI>
<A HREF="#E69E179" >The Toolbar Control</A>
<LI>
<A HREF="#E69E180" >The Up-Down (Spinbox) Control</A>
<LI>
<A HREF="#E69E181" >The Progress Indicator Control</A>
<LI>
<A HREF="#E69E182" >The Trackbar Control</A>
<LI>
<A HREF="#E69E183" >The Header Control</A>
<LI>
<A HREF="#E69E184" >The ListView Control</A>
<LI>
<A HREF="#E69E185" >The TreeView Control</A>
<LI>
<A HREF="#E69E186" >The Tab Control</A>
<LI>
<A HREF="#E69E187" >Property Sheets</A>
<LI>
<A HREF="#E69E188" >The Animation Control</A>
<UL>
<LI>
<A HREF="#E70E59" >Implementing a CAnimateCtrl Animation Control</A>
<LI>
<A HREF="#E70E60" >Using the CAnimateCtrl Animate Object</A></UL></UL>
<LI>
<A HREF="#E68E66" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E29"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>- 11 -</B>
<BR><B>Using the New Win32 Common Controls</B></FONT></CENTER></H1>
<BR>
<P>Windows 95 offers the programmer a new set of dialog controls that are available in Windows NT as of version 3.51. Throughout this chapter, these controls are referred to as the Win32 Common Controls. The sample program was created under Windows 95, but the details of an implementation under Windows NT are identical.
<BR>
<P>The Win32 Common Controls are supported with COMCTL32.DLL. When you use the Common Controls with Visual C++ 4, you need to include the AFXCMN.H header file. With Visual C++-created projects, Visual C++ should automatically include AFXCMN.H in the STDAFX.H file.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Don't confuse COMCTL32.DLL, which supports Common Controls, with COMDLG32.DLL, which supports the common dialog boxes. The Common Controls are separate from the common dialog boxes.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Although you can write applications that are non-MFC-based that use the Common Controls, using MFC makes interacting with the Common Controls easier and cleaner. Microsoft has provided a sample program in the January 1996 release of the Microsoft Developer Library CD. Search for the keyword LISTVIEW and select ListView: Implements the List View Common Control in Windows 95. The program displays an application that shows the ListView Common Control without an MFC interface. You can compare the ListView example with the sample program in this chapter to see why you might want to use MFC to manage your ListView and other Common Control objects.
<BR>
<P>There are a number of Win32 Common Controls, which are documented in the next part of this chapter. In the final part of this chapter, you will develop a simple MFC application that demonstrates how to implement the ListView and TreeView controls. The other Win32 Common Controls are easy to use and should present no difficulties for the typical Visual C++ programmer.
<BR>
<P>Another source of information about the Win32 Common Controls is <I>Windows </I><I>95 Programming Unleashed</I> (Sams Publishing, 1995). This book covers all the controls and shows a non-MFC implementation of the RichText control.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>CAUTION</B>
<BR>
<BR>Be careful, because the Win32 Common Controls will never be available for native Windows 3.x 16-bit programs. If backward compatibility is important to you, you might want to limit your usage of the Win32 Common Controls.
<BR>
<BR>When you're creating new applications, you probably won't want backward compatibility with Windows 3.x, because there is a wide backward-compatibility gap between both Windows 3.x (16-bit) and the 32-bit versions of Windows—and also between Visual C++ 1.5x and Visual C++ 4.0.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E65"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Win32 Common Controls</B></FONT></CENTER></H3>
<BR>
<P>Ten Win32 Common Controls are available to the programmer. One of these controls—the Header control—is normally used by the ListView control and is seldom used by programmers directly.
<BR>
<P>Of the 10 controls, two (TreeView and ListView) could be very useful to database applications developers. These two controls let you display information in a logical manner. Of course, the other controls can also prove to be most valuable to database programmers.
<BR>
<P>In a moment, you'll take a quick look at the new Win32 Common Controls. The next part of this chapter offers a sample application that uses both the TreeView control and the ListView control to display data to the user.
<BR>
<P>This section also documents property sheets. The property sheet extensions to the Win32 interface are available to developers of 16-bit applications. Other (nonWin32 Common Control) MFC classes that provide simple toolbars and status bars also are available to developers of 16-bit applications.
<BR>
<P>All the examples in this chapter are implemented in a single source project called WIN32CC. The complete source for this project is found on the sample source CD that is supplied with this book.
<BR>
<BR>
<A NAME="E69E177"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The Status Bar Control</B></FONT></CENTER></H4>
<BR>
<P>The Status Bar control lets an application provide visual and verbal feedback to the user. When AppWizard creates an application that includes status bar support, the CStatusBar class is used to manage the status bar. However, CStatusBar objects can be used only with frame windows, not with dialog windows.
<BR>
<P>In the sample program, a status bar has been added to a dialog box. This status bar could show virtually anything, but it's useful for extended help, debugging, and other messages that need to be communicated to the user unobtrusively. Both CStatusBar and CStatusBarCtrl controls include the capability to assign the status window's position to either the top or bottom of the application's window.
<BR>
<P>Most applications that are created by using AppWizard and that have a CStatusBar-implemented status bar will have a default set of attributes. These attributes include the following:
<BR>
<UL>
<LI>A status message area, which is the first area on the status bar's left. This output area is stretched to fill in the area that remains after the other status areas are created and sized.
<BR>
<BR>
<LI>A Caps Lock indicator, whose default text is CAP. This text is a string resource and may be changed by the developer.
<BR>
<BR>
<LI>A Num Lock indicator, whose default text is NUM. This text is a string resource and may be changed by the developer.
<BR>
<BR>
<LI>A Scroll Lock indicator, whose default text is SCRL. This text is a string resource and may be changed by the developer.
<BR>
<BR>
</UL>
<P>AppWizard also includes strings for the following indicators:
<BR>
<UL>
<LI>A recording indicator, normally implemented when the user is interactively recording a macro. Another use for this indicator would be to indicate that some form of multimedia recording is being performed. The default string for this indicator is REC.
<BR>
<BR>
<LI>The embedded selection indicator, whose default text is EXT.
<BR>
<BR>
<LI>The overwrite indicator, whose default text is OVR.
<BR>
<BR>
</UL>
<P>The MFC doesn't supply a default handler for these three indicators (REC, EXT, and OVR). If your application is going to use these three handlers, it must code its own handlers for each of them.
<BR>
<P>Figure 11.1 shows the main status bar in the sample application. This status bar has had all six of its panes activated, but no handler has been written for the three unsupported panes.
<BR>
<P><B><A HREF="11vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg01.gif">Figure 11.1. The standard CStatusBar in an MFC application.</A></B>
<BR>
<P>With the sample application, I've added a simple status bar to a dialog box. I therefore needed to leave space for the status bar in the dialog box template (I could have resized the dialog box to make space if I wanted to). The status bar in the dialog box was implemented by using CStatusBarCtrl and has only one pane: an output area for simple status messages. Figure 11.2 shows the implementation of CStatusBarCtrl.
<BR>
<P><B><A HREF="11vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg02.gif">Figure 11.2. A dialog box with a CStatusBarCtrl status bar.</A></B>
<BR>
<P>The code to implement the CStatusBarCtrl object is rather simple, as shown in Listing 11.1. In the header file for the dialog box (or in any other window), you must define a member variable of type CStatusBarCtrl. The line that shows this definition appears in bold.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.1. Creating a CStatusBarCtrl object.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// statusba.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// StatusBar dialog
class StatusBar : public CDialog
{
// Construction
public:
StatusBar(CWnd* pParent = NULL); // Standard constructor
<B> CStatusBarCtrl m_StatusBar;</B>
// Dialog data
//{{AFX_DATA(StatusBar)
enum { IDD = IDD_STATUSBAR };
// NOTE: ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard-generated virtual function overrides
//{{AFX_VIRTUAL(StatusBar)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(StatusBar)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};</FONT></PRE>
<P>Listing 11.2 is the actual code that displays the status bar and places some text in the status bar's output area. Code that has been added to the AppWizard-generated application appears in bold. Most applications will place text in the status bar using a message handler.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.2. The CStatusBarCtrl initialization code.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// statusba.cpp : implementation file
//
#include "stdafx.h"
#include "Win32CC.h"
#include "statusba.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// StatusBar dialog
StatusBar::StatusBar(CWnd* pParent /*=NULL*/)
: CDialog(StatusBar::IDD, pParent)
{
//{{AFX_DATA_INIT(StatusBar)
// NOTE: ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void StatusBar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(StatusBar)
// NOTE: ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(StatusBar, CDialog)
//{{AFX_MSG_MAP(StatusBar)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// StatusBar message handlers
BOOL StatusBar::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
<B> RECT rect; // Never really used! Default sizes used.</B>
<B> rect.top = 0;</B>
<B> rect.left = 0;</B>
<B> rect.right = 100;</B>
<B> rect.bottom = 20;</B>
<B> m_StatusBar.Create(WS_CHILD | WS_VISIBLE | CCS_BOTTOM, rect, this, 0);</B>
<B> m_StatusBar.SetWindowText("Hello, CStatusBarCtrl here!");</B>
return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}</FONT></PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -