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

📄 linenumberedit.cpp

📁 MFC窗口程序设计源代码。相信大家看得懂。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 
	Return :		void
	Parameters :	none

	Usage :			Must be called to (re)establish the edit 
					rect, must also be called as soon as the 
					control changes size.

   ============================================================*/
{

	// Calc sizes
	int width = CalcLineNumberWidth();
	CRect rect;
	GetClientRect( &rect );
	CRect rectEdit( rect );
	rect.right = width;
	rectEdit.left = rect.right + 1;

	// Setting the edit rect and 
	// creating or moving child control
	SetRect( &rectEdit );
	if( m_line.m_hWnd )
		m_line.MoveWindow( 0, 0, width, rect.Height() );
	else
		m_line.Create(NULL,WS_CHILD | WS_VISIBLE | SS_NOTIFY, rect, this, 1 );

	GetRect( &rectEdit );

	// Update line number control data
	m_line.SetTopMargin( rectEdit.top );
	UpdateTopAndBottom();

}

int CLineNumberEdit::CalcLineNumberWidth()
/* ============================================================
	Function :		CLineNumberEdit::CalcLineNumberWidth
	Description :	Calculates the desired width of the line 
					number control, using the current format 
					string and the max number of chars allowed 
					(pessimistic - assumes one character per 
					line).
 
	Return :		int - The width in pixels
	Parameters :	none

	Usage :			Called as soon as the format string is 
					changed.

   ============================================================*/
{

	CClientDC dc( this );

	// If a new font is set during runtime,
	// we must explicitly select the font into
	// the CClientDC to measure it.
	CFont* font = GetFont();
	CFont* oldFont = dc.SelectObject( font );

	m_zero=dc.GetTextExtent( _T( "0" ) );
	CString format;

    // GetLimitText returns the number of bytes the edit box may contain,
    // not the max number of lines...
	//... which is the max number of lines, given one character per d:o :-)
	int maxval = GetLimitText();
    if (m_maxval > 0)
        maxval = m_maxval + m_LineDelta;

	format.Format( m_format, maxval );
	CSize fmt = dc.GetTextExtent( format );
	dc.SelectObject( oldFont );

	// Calculate the size of the line-
	// number field. We add a 5 pixel margin
	// to the max size of the format string
	return fmt.cx + 5;

}

void CLineNumberEdit::UpdateTopAndBottom() 
/* ============================================================
	Function :		CLineNumberEdit::UpdateTopAndBottom
	Description :	Updates the top- and bottom line number 
					for the line number control.
 
	Return :		void
	Parameters :	none
	Usage :			Should be called as soon as the contents of 
					the control is changed.

   ============================================================*/
{

	CRect rect;
	GetClientRect( &rect );
	int maxline = GetLineCount() + m_LineDelta;

	// Height for individual lines
	int lineheight = m_zero.cy;

	// Calculate the number of lines to draw
	int topline = GetFirstVisibleLine() + m_LineDelta;
	if( ( topline + ( rect.Height() / lineheight ) ) < maxline )
		maxline = topline + ( rect.Height() / lineheight );

    if ( m_maxval > 0 && maxline > m_maxval + m_LineDelta )
        maxline = m_maxval + m_LineDelta;

	m_line.SetTopAndBottom( topline, maxline );

}

/////////////////////////////////////////////////////////////////////////////
// CLineNumberEdit public implementation

void CLineNumberEdit::SetMarginForegroundColor( COLORREF col, BOOL redraw, BOOL bEnabled /*= TRUE*/ )
/* ============================================================
	Function :		CLineNumberEdit::SetMarginForegroundColor
	Description :	Sets the text color for the number 
					margin.
 
	Return :		void
	Parameters :	COLORREF col	-	The new text color 
					BOOL redraw		-	TRUE if the control 
										should be redrawn 
										(default)

	Usage :			Call to set a new text color for the line
					number margin. The control will be redrawn
					if it exists.

   ============================================================*/
{

	m_line.SetFgColor( col, redraw );
    if (bEnabled)
    {
        m_bUseEnabledSystemColours = FALSE;
        m_EnabledFgCol = col;
    } else {
        m_bUseDisabledSystemColours = FALSE;
        m_DisabledFgCol = col;
    }

}

void CLineNumberEdit::SetMarginBackgroundColor( COLORREF col, BOOL redraw, BOOL bEnabled /*= TRUE*/ )
/* ============================================================
	Function :		CLineNumberEdit::SetMarginBackgroundColor
	Description :	Sets the background color for the number 
					margin.
 
	Return :		void
	Parameters :	COLORREF col	-	The new background color 
					BOOL redraw		-	TRUE if the control 
										should be redrawn 
										(default)

	Usage :			Call to set a new background color for the 
					line number margin. The control will be 
					redrawn if it exists.

   ============================================================*/
{

	m_line.SetBgColor( col, redraw );
    if (bEnabled)
    {
        m_bUseEnabledSystemColours = FALSE;
        m_EnabledBgCol = col;
    } else {
        m_bUseDisabledSystemColours = FALSE;
        m_DisabledBgCol = col;
    }

}

void CLineNumberEdit::SetLineNumberFormat( CString format )
/* ============================================================
	Function :		CLineNumberEdit::SetLineNumberFormat
	Description :	Changes the way line numbers are presented 
					on screen. 
 
	Return :		void
	Parameters :	CString format - The new format string

	Usage :			Call with a format string using the same 
					format as CString::Format. It should contain 
					one and only one numeric type.

   ============================================================*/
{

	m_format = format;
	m_line.SetLineNumberFormat( format );
	if( m_hWnd )
		Prepare();

}

void CLineNumberEdit::SetLineNumberRange( UINT nMin, UINT nMax /*= 0*/ )
/* ============================================================
	Function :		CLineNumberEdit::SetLineNumberRange
	Description :	Changes the default min and max line numbers. 
 
	Return :		void
	Parameters :	int nMin - changes the line offset
					int nMax - changes the max line number

	Usage :			Call to set up the min and max line numbers.

   ============================================================*/
{

    m_LineDelta = ( int ) nMin;
    m_maxval = ( int ) nMax;

}

/////////////////////////////////////////////////////////////////////////////
// CLineNumberStatic

CLineNumberStatic::CLineNumberStatic()
/* ============================================================
	Function :		CLineNumberStatic::CLineNumberStatic
	Description :	constructor
 
	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{

	m_bgcol = RGB( 255, 255, 248 );
	m_fgcol = RGB( 0, 0, 0 );
    m_format = _T( "%05i" );
    m_topline = 0;
    m_bottomline = 0;
}

CLineNumberStatic::~CLineNumberStatic()
/* ============================================================
	Function :		CLineNumberStatic::~CLineNumberStatic
	Description :	destructor
 
	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{
}

BEGIN_MESSAGE_MAP(CLineNumberStatic, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLineNumberStatic message handlers

void CLineNumberStatic::OnPaint() 
/* ============================================================
	Function :		CLineNumberStatic::OnPaint
	Description :	Handler for WM_PAINT. 
 
	Return :		void
	Parameters :	none

	Usage :			Called from Windows.

   ============================================================*/
{

	CPaintDC dcPaint( this );

	CRect rect;
	GetClientRect( &rect );

	// We double buffer the drawing - 
	// preparing the memory CDC
	CDC dc;
	dc.CreateCompatibleDC( &dcPaint );
	int saved = dc.SaveDC();

	// Create GDI and select objects
	CBitmap bmp;
	CPen pen;
	bmp.CreateCompatibleBitmap( &dcPaint, rect.Width(), rect.Height() );
	pen.CreatePen( PS_SOLID, 1, m_fgcol );
	dc.SelectObject( &bmp );
	dc.SelectObject( &pen );

	// Painting the background
	dc.FillSolidRect( &rect, m_bgcol );
//	dc.MoveTo( rect.right - 1, 0 );
//	dc.LineTo( rect.right - 1, rect.bottom );

	// Setting other attributes
	dc.SetTextColor( m_fgcol );
	dc.SetBkColor( m_bgcol );
	dc.SelectObject( GetParent()->GetFont() );

	// Output the line numbers
	if( m_bottomline )
	{
		int lineheight = dc.GetTextExtent( _T( "0" ) ).cy;
		for( int t = m_topline ; t < m_bottomline ; t++ )
		{
			CString output;
			output.Format( m_format, t );
			int topposition = m_topmargin + lineheight * ( t - m_topline );
			dc.TextOut( 2, topposition, output );
		}
	}

	dcPaint.BitBlt( 0, 0, rect. right, rect.bottom, &dc, 0, 0, SRCCOPY );
	dc.RestoreDC( saved );

}

BOOL CLineNumberStatic::OnEraseBkgnd( CDC* ) 
/* ============================================================
	Function :		CLineNumberStatic::OnEraseBkgnd
	Description :	Mapped to WM_ERASEBKGND. Handled to avoid
					flicker, as we redraw the complete control 
					in OnPaint
 
	Return :		BOOL  -	Always TRUE
	Parameters :	CDC*  -	From Windows

	Usage :			Called from Windows.

   ============================================================*/
{

	return TRUE;

}

void CLineNumberStatic::OnLButtonDown( UINT nFlags, CPoint point )
/* ============================================================
	Function :		CLineNumberStatic::OnLButtonDown
	Description :	Called when the control is clicked. Will
					send the urm_SELECTLINE registered message 
					to the parent to select the line clicked on.
					
	Return :		void
	Parameters :	UINT nFlags		-	Not used
					CPoint point	-	Position of cursor
					
	Usage :			Called from Windows.

   ============================================================*/
{

	// Find the line clicked on
	CClientDC	dc( this );
	dc.SelectObject( GetParent()->GetFont() );
	int lineheight = dc.GetTextExtent( _T( "0" ) ).cy;
	int lineno = ( int ) ( ( double ) point.y / ( double ) lineheight );

	// Select this line in the edit control
	GetParent()->SendMessage( urm_SELECTLINE, lineno );

	CStatic::OnLButtonDown( nFlags, point );

}

/////////////////////////////////////////////////////////////////////////////
// CLineNumberStatic public implementation

void CLineNumberStatic::SetBgColor( COLORREF col, BOOL redraw )
/* ============================================================
	Function :		CLineNumberStatic::SetBgColor
	Description :	This function sets the panel background 
					color
 
	Return :		void
	Parameters :	COLORREF col -	New background color
					BOOL redraw  -	TRUE if the control 
									should be redrawn 
									(default)

	Usage :			Called from the parent.

   ============================================================*/
{

	m_bgcol = col;
	if( m_hWnd && redraw )
		RedrawWindow();

}

void CLineNumberStatic::SetFgColor( COLORREF col, BOOL redraw )
/* ============================================================
	Function :		CLineNumberStatic::SetFgColor
	Description :	This function sets the panel foreground 
					color
 
	Return :		void
	Parameters :	COLORREF col -	New text color
					BOOL redraw  -	TRUE if the control 
									should be redrawn 
									(default)
 
	Usage :			Called from the parent.

   ============================================================*/
{

	m_fgcol = col;
	if( m_hWnd && redraw )
		RedrawWindow();

}

void CLineNumberStatic::SetTopAndBottom( int topline, int bottomline )
/* ============================================================
	Function :		CLineNumberStatic::SetTopAndBottom
	Description :	Sets the top- and bottom line and redraw 
					the control (if it exists)
 
	Return :		void
	Parameters :	int topline		-	The top line number
					int bottomline	-	The bottom line number
 
	Usage :			Called when the top and bottom line is 
					changed in the parent.

   ============================================================*/
{

	m_topline = topline;
	m_bottomline = bottomline;
	if( m_hWnd )
		RedrawWindow();

}

void CLineNumberStatic::SetTopMargin( int topmargin )
/* ============================================================
	Function :		CLineNumberStatic::SetTopMargin
	Description :	Sets the top margin for painting.
 
	Return :		void
	Parameters :	int topmargin -	The top margin to set
 
	Usage :			Will be called with the value of GetRect 
					from the parent.

   ============================================================*/
{

	m_topmargin = topmargin;

}

void CLineNumberStatic::SetLineNumberFormat( CString format )
/* ============================================================
	Function :		CLineNumberStatic::SetLineNumberFormat
	Description :	Sets the format string of the control
 
	Return :		void
	Parameters :	CString format -	Format string to use 
 
	Usage :			Called from the parent when the format 
					string is changed.

   ============================================================*/
{

	m_format = format;
	if( m_hWnd )
		RedrawWindow();

}

⌨️ 快捷键说明

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