📄 vcg11.htm
字号:
<P>The CStatusBarCtrl.Create() function requires four parameters. The first parameter controls the style of the status bar. You can specify the style SBARS_SIZEGRIP to add a sizing grip to the right end of the status bar.
<BR>
<P>The rect parameter specifies the size of the Status Bar control, and the this keyword tells the status bar what the parent window is. The final parameter, 0, is the control identifier (which isn't used in the sample program).
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Microsoft documentation states that the SBARS_SIZEGRIP sizing grip isn't functional when the status bar is aligned with the top of the window. Experiments I've performed have shown that this isn't true: Regardless of the placement of the status bar, the sizing grip is always active. However, it's difficult to use the sizing grip when the status bar isn't attached to the bottom of the parent window.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E178"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The RichText Control</B></FONT></CENTER></H4>
<BR>
<P>The RichText control is based on the Microsoft rich text specification. Rich text could easily be used to store formatted text in a database. (All rich text documents use only the standard text character sets.)
<BR>
<P>Usually, your programs will use the CRichEditCtrl as an application's main window, probably using Visual C++ 4's AppWizard to create their applications. There are situations in which you might very well want to create an application that has a dialog with a rich text edit control that is located in a dialog box.
<BR>
<P>In the rich edit control section of the sample program, I've created the most basic implementation of a CRichEditCtrl: I create the control and fill it from a file that the user selects.
<BR>
<P>The RichText control offers a vast array of functionalities:
<BR>
<UL>
<LI>Character formatting: The RichText control allows full character formatting, including typeface, size, color, and effects such as bold, italic, and protected.
<BR>
<BR>
<LI>Paragraph formatting: The RichText control allows formatting of paragraphs, such as alignment, tabs, indentation, and numbering.
<BR>
<BR>
<LI>Current selection: The RichText control allows for selection processing.
<BR>
<BR>
<LI>Word breaks: The RichText control allows processing of word breaks to determine where it can optimally break a line.
<BR>
<BR>
<LI>Clipboard operations: The RichText control supports both the clipboard and OLE.
<BR>
<BR>
<LI>Stream operations: The RichText control can process its own input, both to files and to application buffers. This process uses callback functions that you, the programmer, write.
<BR>
<BR>
<LI>Printing: The RichText control comes with an implementable printer interface.
<BR>
<BR>
<LI>Bottomless rich edit controls: The RichText control can request that it be resized based on its contents. This resizing can be dynamic so that the control is resized as objects are added to the control.
<BR>
<BR>
<LI>The RichText control supports notifications for a number of different events.
<BR>
<BR>
</UL>
<P>The example of a RichText control shown in Figure 11.3 creates a RichText control and serializes (from an existing .RTF file) some text into the control. You create this control by first providing a dummy frame control (so that you know the location and size when you create the RichText control) and then making a few modifications to the source files as created by ClassWizard.
<BR>
<P>Figure 11.3 shows the dialog box with its frame control to provide a place (both location and size) for the RichText control to be located. The attributes of this frame control aren't critical, because it will be completely covered with the RichText control when the dialog box is created.
<BR>
<P><B><A HREF="11vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg03.gif">Figure 11.3. The dialog box that will have the RichEdit control example.</A></B>
<BR>
<P>After the program executes (and a file has been loaded), your RichText control might look like that shown in Figure 11.4.
<BR>
<P><B><A HREF="11vcg04.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg04.gif">Figure 11.4. The RichEdit control example in a dialog box.</A></B>
<BR>
<P>Listing 11.3 shows the header file RICHTEXT.H. In this file, a CRichEditCtrl member and prototypes for some functions that will be called have been added manually (and appear in bold). Remember that callback functions must be external to C++ classes, as Listing 11.3 shows.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.3. The RICHTEXT.H file.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// RichText.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CRichEdit dialog
#define ID_RTFCTRL 12345
class CRichEdit : public CDialog
{
// Construction
public:
CRichEdit(CWnd* pParent = NULL); // Standard constructor
// Dialog data
//{{AFX_DATA(CRichEdit)
enum { IDD = IDD_RICHTEXT };
CStatic m_RichFrame;
CRichEditCtrl m_RichText;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRichEdit)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
<B> BOOL GetFileName(LPSTR szFileName, BOOL bOpen);</B>
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CRichEdit)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
<B>// Callbacks must be external to the class!</B>
<B> DWORD CALLBACK OpenCallback</B>
<B> (DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);</B></FONT></PRE>
<P>In the source file, RICHTEXT.CPP, functions have been added to get the filename of the .RTF file and to serialize this file into the control. The WM_INITDIALOG handler has also been implemented where the CRichEditCtrl control has been created. Listing 11.4 shows the RICHTEXT.CPP file with edits marked in bold.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.4. The RichText control implementation file, RICHTEXT.CPP.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// RichText.cpp : implementation file
//
#include "stdafx.h"
#include "win32cc.h"
#include "RichText.h"
<B>#include "cderr.h" // For the common dialog error codes!</B>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRichEdit dialog
CRichEdit::CRichEdit(CWnd* pParent /*=NULL*/)
: CDialog(CRichEdit::IDD, pParent)
{
//{{AFX_DATA_INIT(CRichEdit)
//}}AFX_DATA_INIT
}
void CRichEdit::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRichEdit)
DDX_Control(pDX, IDC_RICHFRAME, m_RichFrame);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRichEdit, CDialog)
//{{AFX_MSG_MAP(CRichEdit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRichEdit message handlers
BOOL CRichEdit::OnInitDialog()
{
CDialog::OnInitDialog();
<B> WINDOWPLACEMENT lpwndpl;</B>
<B>// We use the frame to locate our RTF control:</B>
<B> m_RichFrame.GetWindowPlacement(&lpwndpl);</B>
<B> m_RichText.Create(</B>
<B> WS_CHILD | WS_VISIBLE | WS_BORDER |</B>
<B> ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL |</B>
<B> ES_MULTILINE | ES_WANTRETURN,</B>
<B> lpwndpl.rcNormalPosition, this, ID_RTFCTRL);</B>
<B> HFILE hFile; // File handle</B>
<B> OFSTRUCT OpenFileName; // Open file strucuture</B>
<B> EDITSTREAM es; // The EDITSTREAM structure</B>
<B> char szFileName[512]; // Filename buffer</B>
<B> szFileName[0] = '\0';</B>
<B> if(GetFileName(szFileName, TRUE))</B>
<B> {</B>
<B> // Open the file, read mode:</B>
<B> hFile = OpenFile(szFileName, &OpenFileName, OF_READ);</B>
<B> es.dwCookie = 0;</B>
<B> es.dwError = 0;</B>
<B> es.pfnCallback = (EDITSTREAMCALLBACK)NULL;</B>
<B> if (hFile)</B>
<B> {</B>
<B> // Set up the EDITSTREAM structure</B>
<B> es.dwCookie = (DWORD)hFile; // Our file handle</B>
<B> es.dwError = 0; // No errors</B>
<B> es.pfnCallback = OpenCallback; // Use callback</B>
<B> // Get the file using the callback:</B>
<B> m_RichText.StreamIn(SF_RTF, es);</B>
<B> // Close the file when done.</B>
<B> _lclose(hFile);</B>
<B> }</B>
<B> }</B>
return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
<B>BOOL CRichEdit::GetFileName(LPSTR szFileName, BOOL bOpen)</B>
<B>{</B>
<B> BOOL nReturn = FALSE;</B>
<B> OPENFILENAME OpenFileName;</B>
<B> OpenFileName.lStructSize = sizeof(OPENFILENAME);</B>
<B> OpenFileName.hwndOwner = (HWND)m_hWnd;</B>
<B> OpenFileName.hInstance = NULL;</B>
<B> OpenFileName.lpstrFilter = "RTF Files (*.RTF)\0*.RTF\0";</B>
<B> OpenFileName.lpstrCustomFilter = (LPSTR)NULL;</B>
<B> OpenFileName.nMaxCustFilter = 0L;</B>
<B> OpenFileName.nFilterIndex = 1L;</B>
<B> OpenFileName.lpstrFile = szFileName;</B>
<B> OpenFileName.nMaxFile = 512;</B>
<B> OpenFileName.lpstrFileTitle = NULL;</B>
<B> OpenFileName.nMaxFileTitle = 0;</B>
<B> OpenFileName.lpstrInitialDir = NULL;</B>
<B> OpenFileName.lpstrTitle = (LPSTR)NULL;</B>
<B> OpenFileName.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;</B>
<B> OpenFileName.nFileOffset = 0;</B>
<B> OpenFileName.nFileExtension = 0;</B>
<B> OpenFileName.lpstrDefExt = (LPSTR)"RTF";</B>
<B> OpenFileName.lCustData = 0L;</B>
<B> OpenFileName.lpfnHook = 0L; // eliminate compiler warning</B>
<B> OpenFileName.lpTemplateName = (LPSTR)NULL;</B>
<B> if (bOpen)</B>
<B> {</B>
<B> nReturn = GetOpenFileName (&OpenFileName);</B>
<B> }</B>
<B> else</B>
<B> {</B>
<B> nReturn = GetSaveFileName(&OpenFileName);</B>
<B> }</B>
<B> if (!nReturn)</B>
<B> {</B>
<B> // Process any errors here if desired!</B>
<B> switch(CommDlgExtendedError())</B>
<B> {// Generic common dialog box error handler:</B>
<B> case CDERR_DIALOGFAILURE :</B>
<B> AfxMessageBox("The common dialog box procedure's call to "</B>
<B> "the DialogBox function failed. ");</B>
<B> break;</B>
<B> case CDERR_FINDRESFAILURE :</B>
<B> AfxMessageBox("The common dialog box procedure failed to "</B>
<B> "find a specified resource.");</B>
<B> break;</B>
<B> case CDERR_GENERALCODES :</B>
<B> AfxMessageBox("General error codes for common dialog "</B>
<B> "boxes.");</B>
<B> break;</B>
<B> case CDERR_INITIALIZATION :</B>
<B> AfxMessageBox("The common dialog box procedure failed "</B>
<B> "during initialization.");</B>
<B> break;</B>
<B> case CDERR_LOADRESFAILURE :</B>
<B> AfxMessageBox("The common dialog box procedure failed to "</B>
<B> "load a specified resource.");</B>
<B> break;</B>
<B> case CDERR_LOADSTRFAILURE :</B>
<B> AfxMessageBox("The common dialog box procedure failed to "</B>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -