MFC中ScrollWindow函数用法举例
1、类h文件
#pragma once
#ifndef __AFXWIN_H__
#error "include 'pch.h' before including this file for PCH"
#endif
#error "include 'pch.h' before including this file for PCH"
#endif
#include "resource.h" // 主符号
#include "School.h"
#include <string>
#include "StringUtil.h"
using std::wstring;
using std::string;
class CMyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow :public CFrameWnd
{
private:
School school;
RECT rectWindow;
char keyBoardCh;
int cIndex;
/*
每行高度
*/
const int hPerLine = 30;
/*行数*/
int rowCount;
/*客户区*/
CRect clientRect;
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnSize(UINT, int, int);
afx_msg void OnChar(UINT, UINT, UINT);
DECLARE_MESSAGE_MAP();
};
#include "School.h"
#include <string>
#include "StringUtil.h"
using std::wstring;
using std::string;
class CMyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow :public CFrameWnd
{
private:
School school;
RECT rectWindow;
char keyBoardCh;
int cIndex;
/*
每行高度
*/
const int hPerLine = 30;
/*行数*/
int rowCount;
/*客户区*/
CRect clientRect;
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnSize(UINT, int, int);
afx_msg void OnChar(UINT, UINT, UINT);
DECLARE_MESSAGE_MAP();
};
2、实现文件
#include "pch.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "MFCApplication7.h"
#include "MainFrm.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "MFCApplication7.h"
#include "MainFrm.h"
#include "MFCApplication7Doc.h"
#include "MFCApplication7View.h"
#include "MFCApplication7View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define new DEBUG_NEW
#endif
CMyApp myApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->MoveWindow(0, 0, 800, 600, 1);
m_pMainWnd->CenterWindow(m_pMainWnd);
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
ON_WM_CHAR()
ON_WM_SIZE()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
Create(NULL, TEXT("The Hello Application"), WS_OVERLAPPEDWINDOW,rectWindow);
keyBoardCh = 'a';
cIndex = 0;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
/*写文本的方框*/
CRect rectText;
TCHAR texts[1000];
if (keyBoardCh == 'c')
{
for (int i = 0;i < 2;i++)
{
rectText.left = 0;
rectText.right = clientRect.right;
rectText.top = (clientRect.Height()/ hPerLine -1)*hPerLine +(i*hPerLine);
rectText.bottom = rectText.top + hPerLine;
wsprintf(texts, TEXT("a%d %d"), (cIndex + 18 +i )*hPerLine, cIndex+17+i);
dc.DrawText(texts, -1, &rectText, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.MoveTo(0, rectText.bottom);
dc.LineTo(rectText.right, rectText.bottom);
}
return;
}
for (int i = 0;i < rowCount;i++)
{
rectText.left = 0;
rectText.right = clientRect.right;
rectText.top = i * hPerLine;
rectText.bottom = rectText.top + hPerLine;
wsprintf(texts, TEXT("a%d %d"), rectText.bottom,i);
dc.DrawText(texts, -1, &rectText, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.MoveTo(0, rectText.bottom);
dc.LineTo(rectText.right, rectText.bottom);
}
}
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->MoveWindow(0, 0, 800, 600, 1);
m_pMainWnd->CenterWindow(m_pMainWnd);
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
ON_WM_CHAR()
ON_WM_SIZE()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
Create(NULL, TEXT("The Hello Application"), WS_OVERLAPPEDWINDOW,rectWindow);
keyBoardCh = 'a';
cIndex = 0;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
/*写文本的方框*/
CRect rectText;
TCHAR texts[1000];
if (keyBoardCh == 'c')
{
for (int i = 0;i < 2;i++)
{
rectText.left = 0;
rectText.right = clientRect.right;
rectText.top = (clientRect.Height()/ hPerLine -1)*hPerLine +(i*hPerLine);
rectText.bottom = rectText.top + hPerLine;
wsprintf(texts, TEXT("a%d %d"), (cIndex + 18 +i )*hPerLine, cIndex+17+i);
dc.DrawText(texts, -1, &rectText, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.MoveTo(0, rectText.bottom);
dc.LineTo(rectText.right, rectText.bottom);
}
return;
}
for (int i = 0;i < rowCount;i++)
{
rectText.left = 0;
rectText.right = clientRect.right;
rectText.top = i * hPerLine;
rectText.bottom = rectText.top + hPerLine;
wsprintf(texts, TEXT("a%d %d"), rectText.bottom,i);
dc.DrawText(texts, -1, &rectText, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.MoveTo(0, rectText.bottom);
dc.LineTo(rectText.right, rectText.bottom);
}
}
void CMainWindow::OnSize(UINT, int cx, int cy)
{
rowCount = cy / hPerLine + 20;
clientRect.left = 0;
clientRect.top = 0;
clientRect.right = cx;
clientRect.bottom = cy;
}
{
rowCount = cy / hPerLine + 20;
clientRect.left = 0;
clientRect.top = 0;
clientRect.right = cx;
clientRect.bottom = cy;
}
void CMainWindow::OnChar(UINT ch, UINT, UINT)
{
keyBoardCh = ch;
if (ch == 'c')
{
CRect rect;
GetClientRect(&rect);
const int rowCount = rect.Height() / hPerLine;
RECT rt;
rt.left = 0;
rt.top = rect.Height() - 30-(rect.Height() - rowCount * hPerLine) + 2;
rt.right = rect.right;
rt.bottom = rect.Height();
/*向上滚动一行的高度*/
ScrollWindow(0, -30);
cIndex++;
/*因为初始化客户区时,客户区的高度不正好是高度的倍数,所以无效画客户区时把最后一整行和最后一个不完整的行无效化*/
InvalidateRect(&rt);
}
else
{
Invalidate();
}
}
{
keyBoardCh = ch;
if (ch == 'c')
{
CRect rect;
GetClientRect(&rect);
const int rowCount = rect.Height() / hPerLine;
RECT rt;
rt.left = 0;
rt.top = rect.Height() - 30-(rect.Height() - rowCount * hPerLine) + 2;
rt.right = rect.right;
rt.bottom = rect.Height();
/*向上滚动一行的高度*/
ScrollWindow(0, -30);
cIndex++;
/*因为初始化客户区时,客户区的高度不正好是高度的倍数,所以无效画客户区时把最后一整行和最后一个不完整的行无效化*/
InvalidateRect(&rt);
}
else
{
Invalidate();
}
}