The Accel Application

zhaozj2021-02-17  46

The Accel Application

Let's put this newfound knowledge to work by writing an application that scrolls. Accel draws a window that resembles Microsoft Excel. (See Figure 2-13.) The spreadsheet depicted in the window is 26 columns wide and 99 rows high-much too large to be displayed all at once. However, scroll bars allow the user to view all parts of the spreadsheet. in addition to providing a hands-on look at the principles discussed in the preceding sections, Accel demonstrates another way that a program can scale its output . Rather than use a non-MM_TEXT mapping mode, it uses CDC :: GetDeviceCaps to query the display device for the number of pixels per inch displayed horizontally and vertically. Then it draws each spreadsheet cell so that it's 1 inch wide and ¼ inch tall Using Raw Pixel Count.

Figure 2-13. The accel window.

Figure 2-14. The Accel Application.

Accel.h # Define Linesize 8

Class CMYAPP: Public CWINAPP

{

PUBLIC:

Virtual Bool InitInstance ();

}

Class CMainWindow: Public CFrameWnd

{

protected:

INT M_NCellWidth; // Cell Width in Pixels

Int m_ncellheight; // Cell Height In Pixels

INT M_NRIBBONWIDTH; // Ribbon Width in Pixels

INT M_NVIEWIDTH; // Workspace Width in Pixels

INT m_nviewheight; // Workspace Height in Pixels

INT M_NHSCROLLPOS; // Horizontal Scroll Position

INT m_nvscrollpos; // Vertical Scroll Position

INT m_nhpagesize; // horizontal page size

INT M_NVPAGESIZE; // Vertical Page Size

PUBLIC:

CMAINWINDOW ();

protected:

AFX_MSG void onpaint ();

AFX_MSG Int Oncreate (LPCReatestruct LPCreateStruct);

AFX_MSG Void OnSize (Uint NTYPE, INT CX, INT CY);

AFX_MSG Void onhscroll (uint ncode, uint npos,

CScrollBar * pscrollbar;

AFX_MSG Void ONVScroll (uint ncode, uint npos, cscrollbar * pscrollbar);

Declare_message_map ()

}

Accel.cpp # include

#include "accel.h"

CMYAPP myApp;

/

// CMYAPP Member Functions

Bool CmyApp :: InitInstance ()

{

m_pmainwnd = new cmainwindow;

m_pmainwnd-> showwindow (m_ncmdshow);

m_pmainwnd-> UpdateWindow ();

Return True;

}

/

// CMainWindow Message Map and Member Functions

Begin_MESSAGE_MAP (CMainWindow, CFrameWnd)

ON_WM_CREATE ()

ON_WM_SIZE ()

ON_WM_PAINT ()

ON_WM_HSCROLL ()

ON_WM_VSCROLL ()

END_MESSAGE_MAP ()

CMAINWINDOW :: CMAINWINDOW ()

{

Create (NULL, _T ("accel"),

WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL);

}

INT CMAINWINDOW :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cframewnd :: oncreate (lpcreatestruct) == -1)

Return -1;

CClientDC DC (this);

m_ncellwidth = dc.getDeviceCaps (LogPixelsx);

M_ncellheight = dc.getDeviceCaps (LogPixelsy) / 4;

m_nribbonwidth = m_ncellwidth / 2;

M_nviewWidth = (26 * m_ncellwidth) m_nribbonwidth;

m_nviewheight = m_ncellheight * 100;

Return 0;

}

Void CMainWindow :: Onsize (uint ntype, int Cx, int CY)

{

CframeWnd :: Onsewnd :: OnSize (NTYPE, CX, CY);

//

// set the horizontal scrolling parameters.

//

INT NHScrollMax = 0;

m_nhscrollpos = m_nhpagesize = 0;

IF (cx

NHScrollmax = m_nviewwidth - 1;

m_nhpagesize = cx;

m_nhscrollpos = min (m_nhscrollpos, m_nviewwidth -

m_nhpagesize - 1);

}

ScrollInfo Si;

Si.fmask = SIF_PAGE | SIF_RANGE | SIF_POS;

Si.nmin = 0;

Si.nmax = NHScrollmax;

Si.npos = m_nhscrollpos;

Si.NPAGE = M_NHPAGESIZE;

Setscrollinfo (SB_HORZ, & SI, TRUE);

//

// set The Vertical Scrolling Parameters.

//

INT nVscrollmax = 0;

m_nvscrollpos = m_nvpagesize = 0;

IF (cy

NVScrollMax = m_nviewheight - 1;

M_nvpagesize = cy;

m_nvscrollpos = min (m_nvscrollpos, m_nviewheight -

M_nvpagesize - 1);

}

Si.fmask = SIF_PAGE | SIF_RANGE | SIF_POS;

Si.nmin = 0;

Si.nmax = nvscrollmax;

Si.npos = m_nvscrollpos;

Si.NPAGE = M_NVPAGESIZE;

SetscrollInfo (SB_VERT, & SI, TRUE);

}

Void cmainwindow :: onpaint ()

{

CPAINTDC DC (this);

//

// set the window Origin to Reflect The Current Scroll Positions.

//

Dc.SetWindoworg (m_nhscrollpos, m_nvscrollpos);

//

// Draw the grid lines.

//

CPEN PEN (PS_SOLID, 0, RGB (192, 192, 192);

CPEN * POLDPEN = DC.SelectObject (& Pen);

For (int i = 0; i <99; i ) {

INT Y = (i * m_ncellheight) m_ncellheight;

Dc.moveto (0, y);

Dc.LineTo (M_NViewWidth, Y);

}

For (int J = 0; j <26; j ) {

INT x = (j * m_ncellwidth) m_nribbonwidth;

Dc.moveto (x, 0);

DC.LineTo (x, m_nviewheight);

}

Dc.selectObject (Poldpen);

//

// Draw the Bodies of the rows and the column headers.

//

CBRUSH brush;

Brush.createstockOckObject (LTGRAY_BRUSH);

CRECT RCTOP (0, 0, M_NViewWidth, M_ncellHeight);

DC.FillRect (Rctop, & Brush);

CRECT RCLEFT (0, 0, M_NRibbonWidth, M_nViewHeight);

Dc.FillRect (RCLEFT, & Brush);

Dc.moveto (0, M_NCellHeight);

Dc.LineTo (M_nViewWidth, M_ncellHeight);

Dc.moveto (m_nribbonwidth, 0);

Dc.LineTo (M_nribbonwidth, M_NViewHeight);

Dc.setbkmode (transparent);

//

// Add number and button outlines to the row headers.

//

For (i = 0; i <99; i ) {

INT Y = (i * m_ncellheight) m_ncellheight;

Dc.moveto (0, y);

Dc.LineTo (M_nribbonwidth, Y); CString String;

String.Format (_t ("% d"), i 1);

CRECT RECT (0, Y, M_NRibbonWidth, Y M_NCellHeight);

Dc.drawtext (String, & Rect, Dt_SingLine |

DT_Center | DT_VCENTER);

RECT.TOP ;

Dc.draw3dRect (Rect, RGB (255, 255, 255),

RGB (128, 128, 128);

}

//

// Add letters and button outlines to the column headers.

//

For (j = 0; j <26; j ) {

INT x = (j * m_ncellwidth) m_nribbonwidth;

Dc.moveto (x, 0);

Dc.LineTo (x, m_ncellheight);

CString string;

String.Format (_t ("% c"), J `a ');

CRECT RECT (x, 0, x m_ncellwidth, m_ncellheight);

Dc.drawtext (String, & Rect, Dt_SingLine |

DT_Center | DT_VCENTER);

Rect.Left ;

Dc.draw3dRect (Rect, RGB (255, 255, 255),

RGB (128, 128, 128);

}

}

Void CMainWindow :: OnhScroll (uint ncode, uint npos, cscrollbar * pscrollbar)

{

Int ndelta;

Switch (ncode) {

Case SB_LINELEFT:

NDELTA = -Linesize;

Break;

Case SB_PageLeft:

NDELTA = -m_nhpagesize;

Break;

Case Sb_thumbTrack:

NDELTA = (int) npos - m_nhscrollpos;

Break;

Case SB_PAgeright:

NDELTA = m_nhpagesize;

Break;

Case SB_Lineright:

NDELTA = linesize;

Break;

Default: // ignore Other Scroll Bar Messages

Return;

}

INT nscrollpos = m_nhscrollpos nDELTA;

INT nmaxpos = m_nviewwidth - m_nhpagesize;

IF (nscrollpos <0)

NDELTA = -m_nhscrollpos;

Else IF (nscrollpos> nmaxpos)

NDELTA = NMAXPOS - M_NHSCROLLLPOS;

IF (ndelta! = 0) {

m_nhscrollpos = ndelta;

Setscrollpos (SB_HORZ, M_NHSCROLLLPOS, TRUE);

ScrollWindow (-ndelta, 0);

}

}

Void CMainWindow :: ONVSCROLL (uint ncode, uint npos, cscrollbar * pscrollbar)

{

Int ndelta;

Switch (ncode) {CASE SB_LINEUP:

NDELTA = -Linesize;

Break;

Case SB_PAGEUP:

NDELTA = -M_nvpagesize;

Break;

Case Sb_thumbTrack:

NDELTA = (int) npos - m_nvscrollpos;

Break;

Case SB_PAGEDOWN:

NDELTA = M_NVPAGESIZE;

Break;

Case SB_LINEDown:

NDELTA = linesize;

Break;

Default: // ignore Other Scroll Bar Messages

Return;

}

INT nscrollpos = m_nvscrollpos ndelta;

INT NMAXPOS = M_NViewHeight - M_nvpagesize;

IF (nscrollpos <0)

NDELTA = -m_nvscrollpos;

Else IF (nscrollpos> nmaxpos)

NDELTA = nmaxpos - m_nvscrollpos;

IF (ndelta! = 0) {

M_nvscrollpos = ndelta;

SetscrollPos (SB_VERT, M_NVSCROLLLPOS, TRUE);

ScrollWindow (0, -NDelta);

}

}

GetDeviceCaps is called from CMainWindow's OnCreate handler, which is called upon receipt of a WM_CREATE message. WM_CREATE is the first message a window receives. It is sent just once, and it arrives very early in the window's lifetime-before the window is even visible on .. the screen An ON_WM_CREATE entry in the window's message map connects WM_CREATE messages to the member function named OnCreate OnCreate is the ideal place to initialize member variables whose values ​​can only be determined at run time It is prototyped as follows.:

AFX_MSG Int Oncreate (LPCReatestruct LPCreatestruct)

lpCreateStruct is a pointer to a structure of type CREATESTRUCT, which contains useful information about a window such as its initial size and location on the screen. The value returned by OnCreate determines what Windows does next. If all goes as planned, OnCreate returns 0, signaling to Windows that the window was properly initialized If OnCreate returns -1, Windows fails the attempt to create the window A prototype OnCreate handler looks like this:.. int CMainWindow :: OnCreate (lPCREATESTRUCT lpCreateStruct)

{

IF (cframewnd :: oncreate (lpcreatestruct) == -1)

Return -1;

Return 0;

}

OnCreate should always call the base class's OnCreate handler to give the framework the opportunity to execute its own window-creation code. This is especially important when you write document / view applications, because it is a function called by CFrameWnd :: OnCreate that creates the View That Goes Inside a frame window.

You'll find the code that does the scrolling in the window's OnHScroll and OnVScroll handlers. Switch-case logic converts the notification code passed in nCode into a signed nDelta value that represents the number of pixels the window should be scrolled. Once nDelta is computed , the scroll position is adjusted by ndelta pixels and the window is scrolled with the statements

M_nvscrollpos = ndelta;

SetscrollPos (SB_VERT, M_NVSCROLLLPOS, TRUE);

ScrollWindow (0, -NDelta);

For the Vertical Scroll Bar and

m_nhscrollpos = ndelta;

Setscrollpos (SB_HORZ, M_NHSCROLLLPOS, TRUE);

ScrollWindow (-ndelta, 0);

For the Horizontal Scroll Bar.

How are the scroll positions stored in m_nHScrollPos and m_nVScrollPos factored into the program's output? When OnPaint is called to paint the part of the workspace that was exposed by the scrolling operation, it repositions the window origin with the statementdc.SetWindowOrg (m_nHScrollPos, m_nVScrollPos) ;

Recall that CDC :: SetWindowOrg tells Windows to map the logical point (x, y) to the device point (0,0), which, for a client-area device context, corresponds to the upper left corner of the window's client area. The statement above moves the origin of the coordinate system left m_nHScrollPos pixels and upward m_nVScrollPos pixels. If OnPaint tries to paint the pixel at (0,0), the coordinate pair is transparently transformed by the GDI into (_m_nHScrollPos, _m_nVScrollPos). If the scroll position is (0,100), the first 100 rows of pixels are clipped from the program's output and the real output-the output the user can see-begins with the 101st row. Repositioning the origin in this manner is a simple and effective way to Move A Scrollable Window over a Virtual Display Surface.

If you could enlarge the window enough to see the entire spreadsheet, you would see the scroll bars disappear. That's because CMainWindow :: OnSize sets the scroll bar range to 0 if the window size equals or exceeds the size of the virtual workspace. The OnSize Of the window...................

And with that, all the pieces are in place The user clicks a scroll bar or drags a scroll bar thumb;. OnHScroll or OnVScroll receives the message and responds by updating the scroll position and scrolling the window; and OnPaint redraws the window, using SetWindowOrg to move the drawing origin an amount that equals the current scroll position. The program's entire workspace is now accessible, despite the physical limitations that the window size imposes on the output. And all for less than 100 additional lines of code. How could it be ? any easier Funny you should ask Because that's exactly what MFC's CScrollView class is for:. to make scrolling easier CScrollView is an MFC class that encapsulates the behavior of a scrolling window you tell CScrollView how large a landscape you wish to view, and.. IT Handles Everything else. Among Other Things, CScrollView Processes WM_VScroll and WM_HSCROLL MESSAGES for You, Scrolls The Window In Response To Scroll Bar Events, and Updates the thumb size When the window size change.

While it's perfectly possible to wire a CScrollView into an application like Accel, CScrollView was designed primarily for document / view applications. Chapter 10 examines CScrollView more closely and also introduces some of the other view classes that MFC provides.

转载请注明原文地址:https://www.9cbs.com/read-30681.html

New Post(0)