Computing.Net > Forums > Programming > MFC scroll bar message help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

MFC scroll bar message help

Reply to Message Icon

Name: gimmpy224
Date: October 31, 2004 at 12:29:14 Pacific
OS: windows XP pro
CPU/Ram: athlon 64 3000+, 2 gb
Comment:

I have spent about a week re-reading a section in my book on scroll bar messages ane i cant fr the life of me figure out how some of these functions work....

Ive been looking at the code for about 2 hours today and im still having trouble so i was wondering if anyone could make this more clear to me.

my .cpp code is :

#include <afxwin.h>
#include "edit.h"

CMyApp myApp;

// CMyApp member functions:

BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow; //makes m_pMainWnd point to CMainWindow
m_pMainWnd->ShowWindow (m_nCmdShow); //tells windows to show the window
m_pMainWnd->UpdateWindow (); //makes sure the window is updated?
return TRUE;
}


//************************************************************************
//************************************************************************
//************************************************************************
//CMainWindow member functions and message map.....


BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) // <<< begins the message loop.
ON_WM_CREATE ()
ON_WM_SIZE ()
ON_WM_PAINT ()
ON_WM_HSCROLL ()
ON_WM_VSCROLL () // this is the message that it will be looking for.
END_MESSAGE_MAP () //ends the message LOOP


//************************************************************************
//************************************************************************
//************************************************************************


CMainWindow::CMainWindow ()
{
Create (NULL, _T ("Test Drawing In Windows."), WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL);// this prints Test Drawing In Windows on the title bar
}


//************************************************************************
//************************************************************************
//************************************************************************


int CMainWindow::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate (lpCreateStruct) == -1)
return -1;

//
// Initialize internal width and height values based on screen metrics.
//
CClientDC dc (this);
m_nCellWidth = dc.GetDeviceCaps (LOGPIXELSX);
m_nCellHeight = dc.GetDeviceCaps (LOGPIXELSY);
m_nViewWidth = 1000 ;
m_nViewHeight = 1000 ;
return 0;
}
//************************************************************************
//************************************************************************
//************************************************************************


void CMainWindow::OnSize (UINT nType, int cx, int cy)
{
CFrameWnd::OnSize (nType, cx, cy);

//
// Set the horizontal scrolling parameters.
//
int nHScrollMax = 0;
m_nHScrollPos = m_nHPageSize = 0;

if (cx < m_nViewWidth) {
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 < m_nViewHeight) { //if the client area height is less than the workspace height then execute
nVScrollMax = m_nViewHeight - 1;
m_nVPageSize = cy; // sets VPageSize to the client areas height
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 () // this is the paint command.
{
CPaintDC dc (this); // creates a paint device context.

dc.SetWindowOrg (m_nHScrollPos, m_nVScrollPos);

CBrush brush (HS_DIAGCROSS, RGB (255,0,0)); // sets the criscrosses to bright red.
dc.SelectObject (&brush); //selects the brush into the DC (device context)
dc.SetBkColor (RGB (0,0,0)); // sets the background color to BLACK
dc.Rectangle (0,0,1000,1000);//Draws a rectangle that is 10000 by 10000


}


//************************************************************************
//************************************************************************
//************************************************************************


void CMainWindow::OnHScroll (UINT nCode, UINT nPos, CScrollBar* pScrollBar)
{
int nDelta;

//
// Compute the horizontal scroll distance, or "delta."
//
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;
}

//
// Adjust the delta if adding it to the current scroll position would
// cause an underrun or overrun.
//
int nScrollPos = m_nHScrollPos + nDelta;
int nMaxPos = m_nViewWidth - m_nHPageSize;

if (nScrollPos < 0)
nDelta = -m_nHScrollPos;
else if (nScrollPos > nMaxPos)
nDelta = nMaxPos - m_nHScrollPos;

//
// Update the scroll position and scroll the window.
//
if (nDelta != 0) {
m_nHScrollPos += nDelta;
SetScrollPos (SB_HORZ, m_nHScrollPos, TRUE);
ScrollWindow (-nDelta, 0);
}
}


//************************************************************************
//************************************************************************
//************************************************************************


void CMainWindow::OnVScroll (UINT nCode, UINT nPos, CScrollBar* pScrollBar)
{
int nDelta;

//
// Compute the vertical scroll distance, or "delta."
//
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;
}

//
// Adjust the delta if adding it to the current scroll position would
// cause an underrun or overrun.
//
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;

//
// Update the scroll position and scroll the window.
//
if (nDelta != 0) {
m_nVScrollPos += nDelta;
SetScrollPos (SB_VERT, m_nVScrollPos, TRUE);
ScrollWindow (0, -nDelta);
}
}


and my .h file code is :

#define LINESIZE 8

class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance(); // Need to declare this function so that the window itself can be printed.
};

class CMainWindow : public CFrameWnd
{
protected:
int m_nCellWidth;
int m_nCellHeight; // Cell height in pixels
int m_nRibbonWidth; // Ribbon width in pixels
int m_nViewWidth; // 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 (); // Gives the main window access to the OnPaint message it will need to output the painting to the screen.
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 () //Declares the messape map MFC gives you.
};

The OnSize Message confuses me the most... i dont see how any of it works or what it does....

And the If statements at the bottom of each Scroll message confuses me also...

I copied the code out of the books example.
If you need to books example instead of mine just say so.

TY :)


GIMPS



Sponsored Link
Ads by Google

Response Number 1
Name: gimmpy224
Date: October 31, 2004 at 19:16:16 Pacific
Reply:

Atualy im BEGGINING to understand it...... but yea.... any additional help is always welcome :-D

GIMPS


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: MFC scroll bar message help

Scrolling bars for html page. www.computing.net/answers/programming/scrolling-bars-for-html-page/7372.html

MFC :) mhm thats right www.computing.net/answers/programming/mfc-mhm-thats-right/11758.html

Client area and scroll bar www.computing.net/answers/programming/client-area-and-scroll-bar/11542.html