API |
MFC |
C++ |
C
Programming Windows with MFC
Scrollbar Window
#include <afxwin.h>
#define ID_SCROLLBAR 1000
#define ID_STATIC 1001
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
afx_msg void SetLabel(int );
afx_msg void OnHScroll(UINT , UINT nPos, CScrollBar* );
DECLARE_MESSAGE_MAP()
CScrollBar wScrollbar;//instantiate scrollbar
CStatic wStatic;
};
BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
CMainFrame::CMainFrame()
{
Create(NULL, _T("MFC scrollbar example"), WS_OVERLAPPEDWINDOW ,CRect(25,25,450,170));
wStatic.Create(_T(""),WS_CHILD | WS_VISIBLE | WS_BORDER , CRect(25,60,75,90), this, ID_STATIC);
wScrollbar.Create(WS_CHILD | WS_VISIBLE | SBS_HORZ, CRect(10, 10, 410, 50), this, ID_SCROLLBAR );
wScrollbar.SetScrollRange(1,100,true);//set scroll bar range
int p=wScrollbar.GetScrollPos();
SetLabel(p);
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_WM_HSCROLL()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;
//deals with scrollbar scrolling
afx_msg void CMainFrame::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int minpos;
int maxpos;
pScrollBar->GetScrollRange(&minpos, &maxpos);
int curpos = pScrollBar->GetScrollPos();
switch(nSBCode)
{
case SB_LEFT: //Scrolls to the lower right.
curpos = minpos;
break;
case SB_RIGHT: //Scrolls to the lower right.
curpos = maxpos;
break;
case SB_ENDSCROLL: //Ends scroll.
break;
case SB_LINEDOWN: //Scrolls one line down.
curpos++;
break;
case SB_LINEUP: //Scrolls one line up.
curpos--;
break;
case SB_PAGEDOWN: //Scrolls one page down.
curpos+=5;
break;
case SB_PAGEUP: //Scrolls one page up.
curpos-=5;
break;
case SB_THUMBPOSITION: //The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
curpos = nPos;
break;
case SB_THUMBTRACK: //The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to.
curpos = nPos;
break;
}
pScrollBar->SetScrollPos(curpos);
SetLabel(curpos);
}
//display value of scollbar in label
afx_msg void CMainFrame::SetLabel(int newvalue)
{
CString conv;
conv.Format(_T("%d"), newvalue);
wStatic.SetWindowText (_T (conv));
}
Creating a Simple Window |
Processing Messages |
Device Context |
Working with Graphics |
Mapping Modes |
Text Output |
Working with the Mouse |
Dealing with Keyboard Input |
Drawing Lines and Shapes |
Adding Menus |
Child Windows |
Dialog Windows |
Common Dialog Box |
Working with Bitmaps |
Common Controls |
Toolbars |
Document View Architecture |
Multi Document Interface |
Timers |
MFC Collections Classes