⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spbufferwnd.h

📁 窗口类CBufferWnd:能够作为子窗口或者时弹出窗口支持滚动
💻 H
字号:
#ifndef __BUFFER_WND_H_
#define __BUFFER_WND_H_

//#pragma once

/*
CBufferWnd:完成屏幕输出类

功能:类似与Console屏幕的输出方式
1 管理光标,光标定义在输入后的待输入位置
2 向屏幕输出字符,通过在光标后面添加字符串,或者通过指定行列设置字符
3 管理滚动

功能限制:
1 不会对字符进行处理,只会直接将字符进行显示
*/
namespace spBase
{

// CBufferWnd
#define WYYBUFFER_WND			"WYY_BUFFER_WND"
//定义滚动时每页的尺寸
#define HSCROLL_PAGE_SIZE			5
#define VSCROLL_PAGE_SIZE			5

#define WM_BW_NOTIFY				WM_USER + 188 // add by wenyy 2003/08/20
/*
WPARAM = 0 时表示 ScrollTxtUp ,LPARAM为参数
HIWORD(LPARAM) = 滚动行数
LOBYTE(LOWORD(LPARAM)) = 填充字符

WPARAM = 1 时表示 AppendTxtAtCaret
LPARAM = 指向结构 struct CBufferWnd::strAppendTxt 的指针

*/

class CConsoler;
class CBufferWnd : public CWnd
{
	DECLARE_DYNAMIC(CBufferWnd)
	friend class CConsoler;
public:
	CBufferWnd();
	virtual ~CBufferWnd();
	BOOL RegisterWindowClass(void);
public:
	//	ASSERT(iWidthBuf>20 && iWidthBuf<=512 && iHeightBuf>4 && iHeightBuf<512 );
	//  dwFlagsAdd 附加的窗口风格,window style attributes,默认的风格:WS_CHILD | WS_VSCROLL | WS_HSCROLL;
	//创建子窗口
	BOOL CreateChildWnd(DWORD dwFlagsAdd, CWnd * parent, LPCSTR pszTitle,const CRect & rect,int nID, 
						int iTabSize, int iFontSize, int iWidthBuf, int iHeightBuf);
	//创建弹出窗口
	// 默认的风格: WS_OVERLAPPED | WS_VSCROLL | WS_HSCROLL;
	BOOL CreateAppWnd(DWORD dwFlagsAdd,LPCSTR pszTitle,const CRect & rect, 
						int iTabSize, int iFontSize, int iWidthBuf, int iHeightBuf);
	//设置退出标记
	void SetCanExitFlag(BOOL fCan){m_fCanExit = fCan;};
public://滚动功能
	void ReCalcScrollSize( void); 
	void ScrollTo(int iX,int iY);
	//检查指定行列是否能够显示,如果不能显示,则进行滚动
	BOOL ScrollIfPointNotShown(int iX,int iY);
public://光标功能
	BOOL OpenCaretAsNecessary(void);
	void CloseCaret(void);
	void GetCaretPoint(CPoint& ptCaret){ptCaret = m_ptCaret;};
	//将光标移动到指定行列
	void SetCaretPoint(int iX,int iY){m_ptCaret=CPoint(iX,iY);SetCaretPoint();};
	void SetCaretPoint(CPoint ptCaret){m_ptCaret = ptCaret;SetCaretPoint();};
	void SetCaretPoint(void);
public://绘图功能
	void PaintWnd(CDC* pDC);
public://文字功能
	struct strAppendTxt
	{
		BOOL fShowY;
		BOOL fShowX;
		int iTxtLen;
		LPCSTR pszTxt;
	};
	//在光标处添加文字,同时改变光标位置
	//BOOL fShowY=TRUE,BOOL fShowX=FALSE
	//在添加文字后是否强制滚动到文字的最后位置
	void AppendTxtAtCaret(LPCSTR pszText,BOOL fShowY=TRUE,BOOL fShowX=FALSE){ASSERT(pszText);AppendTxtAtCaret((int)strlen(pszText),pszText,fShowY,fShowX);};
	//在光标处添加文字
	void AppendTxtAtCaret(int iTxtLen,LPCSTR pszText,BOOL fShowY=TRUE,BOOL fShowX=FALSE);
	//滚动文字,并在后面填入空行,不改变光标位置
	void ScrollTxtUp(int iLines,char cFill=0x20);
public://属性
	//检查某行列在窗口内是否可见
	BOOL IsPosVisible(int iX,int iY);
	//得到经过计算的显示全部缓冲区内容需要的显示区域大小
	CSize GetNeedSize(void){return m_sizeNeed;};
	//得到指定位置的值
	BYTE& GetXY(int iX,int iY){ASSERT(m_pbBuffer && iX<m_iWidthBuf && iY<m_iHeightBuf);return m_pbBuffer[iX +iY*m_iWidthBuf];};
	BYTE& operator [] (int iIndex){ASSERT(m_pbBuffer && iIndex<m_iWidthBuf *m_iHeightBuf);return m_pbBuffer[iIndex];};
	//得到缓冲区指针
	const BYTE* GetBuffer(void){return m_pbBuffer;};
	//得到缓冲区大小
	int GetBufferLen(void){ASSERT(m_pbBuffer);return m_iLenBuffer;};
	//得到文字窗口尺寸
	void GetBufferWindowSize(int &iWidth,int &iHeight){iWidth = m_iWidthBuf;iHeight= m_iHeightBuf;};
	//得到光标位置
	CPoint GetCaretPoint(void){return m_ptCaret;};

protected:
	DECLARE_MESSAGE_MAP()
    
protected:
	
	BOOL CreateEx(DWORD dwFlagsEx,DWORD dwFlags,CWnd * parent,LPCSTR pszTitle, const CRect & rect,int nID, 
               int iTabSize, int iFontSize, int iWidthBuf, int iHeightBuf);

protected://参数设置功能
	void SetBaseParameter(int iTabSize,int iFontSize, int iWidthBuf, int iHeightBuf);
	//消息通知
	void NotifyParent(UINT msg);
	//判断指定位置字符是否为中文字符的第二个字节
	BOOL IsSecondByteOfDBCS(int iX,int iY);
protected:
	CWnd *m_pwndParent;
	BYTE *m_pbBuffer; //缓冲区
	int m_iTabSize;
	int m_iWidthBuf,m_iHeightBuf,m_iLenBuffer;
	CFont *m_ftDraw; //屏幕字体 宋体
	int m_iFontWidth,m_iFontHeight; //字体大小
	int m_iFontMarginX,m_iFontMarginY;//每行,每列需要留出的空间
	COLORREF m_crFont,m_crBackground;

	CSize m_sizeNeed;//显示全部缓冲区内容需要的显示区域大小
	CSize m_sizeIndeed;//当前窗口实际可以显示的窗口大小
	CSize m_sizeIndeedDraw;

	CPoint m_ptCaret;//记录光标位置
	BOOL m_fCaret; //光标是否显示
	int m_nWndID;//窗口ID
	BOOL m_fPopupWnd,m_fCanExit;
public:
	afx_msg LRESULT OnBWndNotifyMsg(WPARAM wP, LPARAM lP);
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnSetFocus(CWnd* pOldWnd);
	afx_msg void OnKillFocus(CWnd* pNewWnd);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnDestroy();
	afx_msg void OnClose();
};
/*
CConsoler : 完成各种类似与控制台的输出功能:
1 通过printf方式输出
2 指定位置输出字符

*/
class CConsoler
{
public:
	CConsoler();
	~CConsoler();
	void SetBufferWnd(CBufferWnd* pWnd);
public:
	void printfX(const char *pszFormat,...);
	BYTE& GetXY(int iX,int iY){ASSERT(m_pBufWnd); return m_pBufWnd->GetXY(iX,iY);};
	CBufferWnd* GetWindow(void){return m_pBufWnd;};
protected:
	//将字符串格式化成具体数据
	//将Tab键进行转换,将\n , \r进行转换,处理中文作为最后一个字符时换行,
	//返回实际使用的字符数量
	int FormatCharsAndOutput(LPCSTR pszSrc,int iLen);
	//
	BOOL IsCtrlChar(char c){return (c=='\t' || c=='\n' || c=='\r');};
protected:
	CBufferWnd* m_pBufWnd;
};

class CMemDC : public CDC
{
public:
	
	// constructor sets up the memory DC
	CMemDC(CDC* pDC) : CDC()
    {
		ASSERT(pDC != NULL);
		
		m_pDC = pDC;
		m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
		
        if (m_bMemDC)	// Create a Memory DC
		{
            pDC->GetClipBox(&m_rect);
            CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
			m_pOldBitmap = SelectObject(&m_bitmap);
            SetWindowOrg(m_rect.left, m_rect.top);
        }
		else		// Make a copy of the relevent parts of the current DC for printing
		{
            m_bPrinting = pDC->m_bPrinting;
            m_hDC		= pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
	}
	
	// Destructor copies the contents of the mem DC to the original DC
	~CMemDC()
    {
		if (m_bMemDC) {	
			// Copy the offscreen bitmap onto the screen.
			m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
				this, m_rect.left, m_rect.top, SRCCOPY);
			
            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
		} else {
			// All we need to do is replace the DC with an illegal value,
			// this keeps us from accidently deleting the handles associated with
			// the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
		}
	}
	
	// Allow usage as a pointer
    CMemDC* operator->() {return this;}
	
    // Allow usage as a pointer
    operator CMemDC*() {return this;}

private:
	CBitmap  m_bitmap;		// Offscreen bitmap
    CBitmap* m_pOldBitmap;	// bitmap originally found in CMemDC
    CDC*     m_pDC;			// Saves CDC passed in constructor
    CRect    m_rect;		// Rectangle of drawing area.
    BOOL     m_bMemDC;		// TRUE if CDC really is a Memory DC.
};


};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -