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

📄 paintdlg.cpp

📁 一个用GDI实现的滤波器的简单例子,实现了滚屏幕处理.
💻 CPP
字号:
// PaintDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Paint.h"
#include "PaintDlg.h"

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

#include "math.h"

#define WDL 50
#define WDR 30
#define HDT 50
#define HDB 90
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

static int m_x[2] = {0,0}, m_y[2] = {0,0};
static int nhigh[2] = {0,0};
static int startx=0, starty=0;
static int du=0;

static int dustep = 5, fstep = 5,fsys = 350,rangeMAX = 900,rangeMIN = -600,rangeh = 0,rangew = 0;
static CString CreateStyle = "Sin";
static int nzhou = 0;
static float usetime = 0;
static CString TipText="历史曲线示意图";

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPaintDlg dialog

CPaintDlg::CPaintDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPaintDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPaintDlg)
	m_Max = 0;
	m_F = 0;
	m_Step = 0;
	m_RangeMAX = 0;
	m_RangeMIN = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPaintDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPaintDlg)
	DDX_Control(pDX, IDC_COMBO_DATASTYLE, m_DataStyle);
	DDX_Control(pDX, IDC_BUTTON_UPDATE, m_Button);
	DDX_Control(pDX, IDC_PAINT, m_Paint);
	DDX_Text(pDX, IDC_EDIT_MAX, m_Max);
	DDV_MinMaxInt(pDX, m_Max, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_F, m_F);
	DDV_MinMaxInt(pDX, m_F, 0, 100);
	DDX_Text(pDX, IDC_EDIT_STEP, m_Step);
	DDV_MinMaxInt(pDX, m_Step, 0, 50);
	DDX_Text(pDX, IDC_EDIT_RANGEMAX, m_RangeMAX);
	DDV_MinMaxInt(pDX, m_RangeMAX, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_RANGEMIN, m_RangeMIN);
	DDV_MinMaxInt(pDX, m_RangeMIN, -1000, 0);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPaintDlg, CDialog)
	//{{AFX_MSG_MAP(CPaintDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_REFLESH, OnReflesh)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPaintDlg message handlers

BOOL CPaintDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_RangeMAX= rangeMAX;
	m_RangeMIN= rangeMIN;
	m_F    = fstep;
	m_Max  = fsys;
	m_Step = dustep;
	UpdateData(false);
	m_bUpData = false;
    m_DataStyle.SetWindowText(CreateStyle);

/*	ShowWindow(3);
	CRect rectw;
	GetClientRect(rectw);
	m_Paint.MoveWindow(rectw,true);*/

	SetTimer(0,fstep,NULL);

	CRect rect;
	m_Paint.GetClientRect(rect);

	int s1 = rangeMAX - rangeMIN;
	int nZero = (int)((rangeMAX*10/s1));

	startx = (int)(rect.left+3);
	starty = (int)(rect.top+HDT+(rect.bottom-rect.top-HDT-HDB)*nZero/10);	//(int)(rect.top+HDT+(rect.bottom-rect.top-HDT-HDB)/2);

	m_x[0]=m_x[1] = startx;
	m_y[0]=m_y[1] = starty;

	rangeh=rect.bottom-rect.top-HDT-HDB;
	rangew=rect.right-rect.left-WDL-WDR;


	//MemDC Init Part ----------------------------------------------------//
	CClientDC dc(&m_Paint);
	CRect rcClient;
	m_Paint.GetClientRect(rcClient);

	if (m_MemDC.GetSafeHdc() == NULL)
	{
		m_MemDC.CreateCompatibleDC(&dc);
		m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
		m_MemDC.SelectObject(m_Bitmap);

		m_MemDC.SetBkColor(RGB(0,0,0));

		CBrush bkBrush(RGB(0,0,0));
        m_MemDC.FillRect(rcClient,&bkBrush);
	}
	//--------------------------------------------------------------------//
	
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CPaintDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPaintDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		
		CRect rect,rectq;
    	CPaintDC  pdc(&m_Paint);
    	m_Paint.GetClientRect(rect);
	
	    rectq.SetRect(rect.left+WDL,rect.top+HDT,rect.right-WDR,rect.bottom-HDB);
	    pdc.FillSolidRect(rect,RGB(0,0,128));
	    //pdc.FillSolidRect(rectq,RGB(0,0,0));	
		DrawSence();		
		
		CDialog::OnPaint();		
	}	
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPaintDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPaintDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{

}

void CPaintDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);	
}


void CPaintDlg::DrawSence()
{
	CRect rect;
	CClientDC pDC(&m_Paint);
	pDC.SetBkMode(1);
	pDC.SetTextColor(RGB(0,255,0));

	CPen pen1;
	pen1.CreatePen(PS_SOLID,1,RGB(255,255,255));	
	pDC.SelectObject(&pen1);
	
	m_Paint.GetClientRect(rect);
	pDC.MoveTo(rect.left+WDL,rect.top+HDT);
	pDC.LineTo(rect.right-WDR,rect.top+HDT);
	pDC.LineTo(rect.right-WDR,rect.bottom-HDB+30);
	pDC.LineTo(rect.left+WDL,rect.bottom-HDB+30);
	pDC.LineTo(rect.left+WDL,rect.top+HDT);


	CString str;
	pDC.TextOut(rect.left+WDL+10,20,TipText);
	str.Format("每周期时间:              秒钟     采样时间长度:                   秒钟");   
		
	pDC.TextOut(rect.left+WDL+rangew-370,rect.bottom-HDB+45,str);
	pen1.DeleteObject();	

	CPen pen2;
	pen2.CreatePen(PS_SOLID,1,RGB(0,32,0));
	pDC.SelectObject(&pen2);

	pDC.MoveTo(rect.left+WDL+1,rect.top+HDT+(rect.bottom-rect.top-HDT-HDB)/2);
	pDC.LineTo(rect.right-WDR-1,rect.top+HDT+(rect.bottom-rect.top-HDT-HDB)/2);

	pen2.DeleteObject();
}


void CPaintDlg::OnTimer(UINT nIDEvent) 
{

	//模拟数据产生-------------------------------------------------------------//
	float num;
	int curh;
	du+=dustep;

 	if(CreateStyle=="Sin")	num=-sin(du*3.14159265/180)*fsys;
	else if(CreateStyle=="Cos")	 num=-cos(du*3.14159265/180)*fsys;
	else if(CreateStyle=="Tan")	 num=-tan(du*3.14159265/180)*fsys;
	else if(CreateStyle=="Atan") num=-atan(du*3.14159265/180)*fsys;

	m_x[0]=m_x[1]+2;
	m_y[0]=m_y[1];

	int s1 = rangeMAX - rangeMIN;
	int nZero = (int)((rangeMAX*10/s1));

	curh = (int)((num/(rangeMAX-rangeMIN))*rangeh);

	if(curh<-(nZero*rangeh/10)) curh = -(nZero*rangeh/10)+1;
	if(curh>(10-nZero)*rangeh/10) curh = (10-nZero)*rangeh/10-1;
	
	m_x[1]=startx;
	m_y[1]=starty+curh;

	//====================================================================================//

/*	//创建部分-------------------------------------------------------------------//
	CClientDC dc(&m_Paint);
	CRect rcClient;
	m_Paint.GetClientRect(rcClient);

	if (m_MemDC.GetSafeHdc() == NULL)
	{
		m_MemDC.CreateCompatibleDC(&dc);
		m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
		m_MemDC.SelectObject(m_Bitmap);

		m_MemDC.SetBkColor(RGB(0,0,0));
		
		
		CBrush bkBrush(RGB(0,0,0));
        m_MemDC.FillRect(rcClient,&bkBrush);
	}
*/	
	//预处理部分------------------------------------------------------------------//
	CRect rcClient;
	m_Paint.GetClientRect(rcClient);
	CClientDC dc(&m_Paint);

	if (m_MemDC.GetSafeHdc() != NULL)
	{
		m_MemDC.BitBlt(2, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);
	
		CPen pen;
	    pen.CreatePen(PS_SOLID,1,RGB(255,255,255));
		m_MemDC.SelectObject(&pen);

		m_MemDC.MoveTo(rcClient.left+WDL-3,rcClient.bottom-HDB);
		m_MemDC.LineTo(rcClient.right-WDR+3,rcClient.bottom-HDB);
		
	    if(du%360==0) {
			m_MemDC.MoveTo(m_x[1],rcClient.bottom-HDB);
			m_MemDC.LineTo(m_x[1],rcClient.bottom-HDB+5);
		}
		pen.DeleteObject();
		
		

		usetime+=(dustep*fstep/dustep)/1000.0;
		static int oldzhou = 0 ;
		
		m_MemDC.SetTextColor(RGB(255,255,255));

		if((du-dustep)<dustep && oldzhou !=nzhou) {
						
			CString str;
			if(usetime>=60)  str.Format("%.2f m",usetime/60.0);
			else str.Format("%.2f s",usetime);
			m_MemDC.TextOut(m_x[1],rcClient.bottom-HDB+10,str);		
			oldzhou = nzhou;
		}
		
	    pen.CreatePen(PS_SOLID,1,RGB(32,32,0));
		m_MemDC.SelectObject(&pen);
        //创建一个与位图相对应的 CDC 对象 m_MemDC 作为画图的后备缓冲区,画完后即可调用 CClientDC 的 BitBlt(..)方法显示
		m_MemDC.MoveTo(startx,starty);
     	m_MemDC.LineTo(startx+2,starty);

	    if((du-dustep)%360 == 0) {
			m_MemDC.MoveTo(m_x[0],rcClient.top+HDT);
			m_MemDC.LineTo(m_x[0],rcClient.bottom-HDB);
		}

		nhigh[1] = (int)(((float)fsys/(rangeMAX-rangeMIN))*rangeh);
		if(nhigh[0]!=nhigh[1]) {
		    m_MemDC.MoveTo(startx+2,starty-nhigh[0]);
		    m_MemDC.LineTo(startx+2,starty-nhigh[1]);
		    m_MemDC.MoveTo(startx+2,starty+nhigh[0]);
		    m_MemDC.LineTo(startx+2,starty+nhigh[1]);
			nhigh[0] = nhigh[1];
		}		
		
		m_MemDC.MoveTo(startx,starty-nhigh[0]);
		m_MemDC.LineTo(startx+2,starty-nhigh[1]);
		m_MemDC.MoveTo(startx,starty+nhigh[0]);
		m_MemDC.LineTo(startx+2,starty+nhigh[1]);

		pen.DeleteObject();
	    pen.CreatePen(PS_SOLID,1,RGB(0,255,0));
		m_MemDC.SelectObject(&pen);		
		m_MemDC.MoveTo(m_x[0],m_y[0]);
		m_MemDC.LineTo(m_x[1],m_y[1]);
		pen.DeleteObject();
	}

	dc.SetBkColor(RGB(0,0,128));
	dc.SetTextColor(RGB(255,255,0));

	CString str;
	str.Format("%3.2f",(float)((360/dustep)*fstep/1000.0));
	dc.TextOut(rcClient.left+WDL+rangew-290,rcClient.bottom-HDB+45,str);
	
	str.Format("%5.2f",usetime);
	dc.TextOut(rcClient.left+WDL+rangew-100,rcClient.bottom-HDB+45,str);

	dc.SetTextColor(RGB(0,255,0));

	float step = (float)(rcClient.bottom-rcClient.top-HDT-HDB)/10;
	CPen pen;
   	pen.CreatePen(PS_SOLID,1,RGB(255,255,255));	
   	dc.SelectObject(&pen);	

	for(int i=0;i<=10;i++) {
		dc.TextOut(WDL-40,rcClient.bottom-HDB-6-i*step,"          ");
	    str.Format("%d",(i-(10-nZero))*s1/10);
		dc.TextOut(WDL-40,rcClient.bottom-HDB-6-i*step,str);
	
		if(i!=10 && i!=0) {
		dc.MoveTo(WDL-5,rcClient.bottom-HDB-i*step);
		dc.LineTo(WDL,rcClient.bottom-HDB-i*step);		
		}		
	}
	pen.DeleteObject();

	//显示部分-------------------------------------------------------------------//
	CPaintDC dc1(&m_Paint); 
	//CRect rcClient;
	//GetClientRect(rcClient);
	if (m_MemDC.GetSafeHdc() != NULL)
	{
		dc.BitBlt(WDL+1, HDT+1, rcClient.Width()-WDR-WDL-1, rcClient.Height()-HDT-HDB+30-1, &m_MemDC, WDL+1+2, HDT+1, SRCCOPY);
	}	

	//====================================================================================//

	if(du>=360) {
		du = 0;
		nzhou++;
	}
	startx = (int)(rcClient.left+3);
	starty = (int)(rcClient.top+HDT+(rcClient.bottom-rcClient.top-HDT-HDB)*nZero/10);
}


void CPaintDlg::OnReflesh() 
{
	// TODO: Add your control notification handler code here
	InvalidateRect(NULL,false);	
}

void CPaintDlg::OnButtonUpdate() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	//m_bUpData = true;
	rangeMAX = m_RangeMAX;
	rangeMIN = m_RangeMIN;
	fstep = m_F;
	fsys  = m_Max;
	dustep= m_Step;	
	
	m_DataStyle.GetWindowText(CreateStyle);	
	
	SetTimer(0,fstep,NULL);
}

⌨️ 快捷键说明

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