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

📄 dlgview.cpp

📁 代码检查工具 只要简单一些switch case的配对 以及类里面memset的使用
💻 CPP
字号:
// DlgView.cpp : implementation file
//

#include "stdafx.h"
#include "check.h"
#include "DlgView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define TIMER_VIEW_FILE			(1)
#define TIMER_DRAW_LINE_NUM		(2)
/////////////////////////////////////////////////////////////////////////////
// CDlgView dialog


CDlgView::CDlgView(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgView::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_nSelLine = 0;
	memset(m_szFileName, 0, sizeof(m_szFileName));
}


void CDlgView::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgView)
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgView, CDialog)
	//{{AFX_MSG_MAP(CDlgView)
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgView message handlers
BOOL CDlgView::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	Init();
	SetTimer(2, 100, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgView::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if (m_edit.GetSafeHwnd())
		this->AdjustCtrl();
}

void CDlgView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == TIMER_VIEW_FILE)
	{
		KillTimer(1);
		this->ViewFile();
	}
	else if (nIDEvent == TIMER_DRAW_LINE_NUM)
	{
		this->DrawLineNum();
	}
	CDialog::OnTimer(nIDEvent);
}

void CDlgView::Init()
{
	//empty.
}

void CDlgView::AdjustCtrl()
{
	RECT rc;
	GetClientRect(&rc);
	int dlg_w = rc.right - rc.left;
	int dlg_h = rc.bottom - rc.top;
	
	const int pad = 6;
	rc.left		+= pad*6+3;
	rc.right	-= pad;
	rc.top		+= pad;
	rc.bottom	-= pad;
	m_edit.MoveWindow(&rc, TRUE);
}

void CDlgView::ViewFile(const char* pszFile, int nSelLine)
{
	if (pszFile == 0 || pszFile[0] == 0)
		return;
	KillTimer(1);
	m_edit.SetWindowText("");
	strcpy(m_szFileName, pszFile);
	m_nSelLine = nSelLine;
	SetTimer(1, 200, NULL);
}

void CDlgView::ViewFile()
{
	const char* pszFile = m_szFileName;
	if (pszFile == 0 || pszFile[0] == 0)
		return;

	if (!pszFile || pszFile[0] == 0)
		return;
	FILE* fp = fopen(pszFile, "rb");
	if (fp == 0)
		return;
	
	fseek(fp, 0L, SEEK_END);
	int len = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	if (len == 0)
	{
		fclose(fp);
		return;
	}
	
	char* buf = new char[len+1];
	memset(buf, 0, len+1);
	fread(buf, 1, len, fp);
	if (fp)
	{
		fclose(fp);
	}
	m_edit.SetWindowText(buf);
	if (buf)
	{
		delete[] buf;
	}

	//set title
	char str[200] = "";
	if (m_nSelLine != -1)
		sprintf(str, "查看文件 [%s] 第%d行case可能没有break", pszFile, m_nSelLine + 1);
	else
		sprintf(str, "查看文件 [%s]", pszFile);
	SetWindowText(str);

	//scroll to dest line
	if (m_nSelLine != -1)
	{
		if (m_nSelLine < 1)
			m_nSelLine = 1;
		int start	= m_edit.LineIndex(m_nSelLine);
		int end		= m_edit.LineIndex(m_nSelLine + 1);
		m_edit.SetSel(start, end, false);
	}
	m_edit.SetFocus();
}

void CDlgView::DrawLineNum()
{
	if (m_szFileName == 0 || m_szFileName[0] == 0)
		return;
	if (m_edit.GetWindowTextLength() == 0)
		return;
	int nCurLine	= m_edit.LineFromChar();
	int nFirstLine	= m_edit.GetFirstVisibleLine();
	int nLineCount	= m_edit.GetLineCount();
	if (nLineCount == 0)
		return;
	
	const int pad = 12;
	RECT rc;
	m_edit.GetRect(&rc);
	int h = rc.bottom - rc.top;
	int nVisibleCount = h / pad;
	if (nVisibleCount > nLineCount)
		nVisibleCount = nLineCount;
	
	CDC* pDC = this->GetDC();
	if (pDC == NULL)
		return;
	COLORREF nOldCr = pDC->SetBkColor(RGB(212,208,200));
	CFont* pOldFont = pDC->SelectObject(m_edit.GetFont());
	if (pOldFont == NULL)
		return;

	int t = 3;
	for (int i = 0; i < nVisibleCount; i++)
	{
		RECT rc = {10, t, 40, t + 20}; t += pad;
		char str[100] = "";
		sprintf(str, "%-4d", nFirstLine + i + 1);

		if (nFirstLine + i == nCurLine)
		{
			pDC->SetTextColor(RGB(255,255,255));
			pDC->SetBkColor(RGB(0,128,0));
		}
		else
		{
			pDC->SetTextColor(RGB(0,0,0));
			pDC->SetBkColor(RGB(212,208,200));
		}
		pDC->DrawText(str, &rc, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	}
	pDC->SetBkColor(nOldCr);
	pDC->SelectObject(pOldFont);
	this->ReleaseDC(pDC);
}

⌨️ 快捷键说明

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