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

📄 触发器dlg.cpp

📁 数字电路学习:触发器原理演示
💻 CPP
字号:
#include "..\..\第五章\计数器\计数器dlg.h"
#include "..\..\第五章\计数器\计数器dlg.h"
#include "..\..\第五章\计数器\计数器dlg.h"
#include "..\..\第五章\计数器\计数器dlg.h"
#include "..\..\第五章\计数器\计数器dlg.h"
// 触发器Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "触发器.h"
#include "触发器Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMyDlg 对话框
#define ZOOMSIZE 0.3

CRect AllRect(0,0,746,450);


CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_QUERYDRAGICON()

	ON_WM_LBUTTONDOWN( )
	//}}AFX_MSG_MAP
//	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
//ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
//ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
//ON_BN_CLICKED(IDC_BUTTON6, OnBnClickedButton6)
END_MESSAGE_MAP()


// CMyDlg 消息处理程序

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

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:在此添加额外的初始化代码
	intJ = intK = intCP = intQN = intQN_1 = 2;//没有值
	trigging = false;
	pause = false;

	UINT resID[] = { IDB_BITMAP1, IDB_BITMAPH, IDB_BITMAPL };
	for( int i = 0; i < 3; i++ )
		res[i].LoadBitmap( resID[i] );

			
	intGates[5] = 1;
	intGates[6] = 0;
	//intGates[1] = 0;
	//intGates[2] = 1;

	return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CMyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		//CDialog::OnPaint();
		CPaintDC dc(this); // 用于绘制的设备上下文
		CDC mem_dc;

		mem_dc.CreateCompatibleDC( &dc );
		mem_dc.SelectObject( &res[0] );
		dc.BitBlt( 0,0,746,512,&mem_dc,0,0 ,SRCCOPY );

		DrawJRect( &dc );
		DrawKRect( &dc );
		DrawCPRect( &dc );
		DrawQRect( &dc );
		DrawOthersRect( &dc );

	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//在触发器在设初值时运行,否则返回
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	//触发器正在工作
	if( trigging ){
		if( pause ){
			m_nTimer = SetTimer(1, 10, 0);
			pause = false;
			return;
		}
		else{
			KillTimer( m_nTimer );
			pause = true;
			return;
		}
	}

	CRect rect;
	
	CRect rect_block[] = {
		CRect( 5,89,43,38 ),//j
		CRect( 9,230,43,38  ),//K
		CRect( 688,84,43,38   ),//Q
		CRect( 692,227,43,38   ),//Q
		CRect( 9,381,43,38  ),//CP
		CRect( 7,424,166,55   )//CP plot
	};
	int * value[] = { &intJ, &intK, &intQN, &intQN,&intCP };

	rect = rect_block[4];
	rect.right = rect.left + rect_block[4].right;
	rect.bottom = rect.top + rect_block[4].bottom;
	if( rect.PtInRect(point) ){
		//看是否有初值没有设置
		for( int i = 0 ;i < 3; i++ )
			if( *value[i] == 2 )
				return;
		trigging = !trigging;
		if( trigging ){//启动定时器
			m_nTimer = SetTimer(1, 10, 0);
			step = 0;
			trigging = false;
			pause = false;
			InvalidateRect( NULL );
			SendMessage( WM_PAINT );
			trigging = true;
			intCP = 2;
			return;
		}
	}

	if( trigging )
		return;


	//检测鼠标是否落在A1,A0,S1,S2内
	for( int i = 0 ;i < 4; i++ ){
		rect = rect_block[i];
		rect.right = rect.left + rect_block[i].right;
		rect.bottom = rect.top + rect_block[i].bottom;
		if( rect.PtInRect(point) ){
			(*value[i])++;
			if( *value[i] > 2 )
				*value[i] = 0;
			if( (i == 2) || (i == 3) ){
				intGates[5] = intQN;
				intGates[6] = !intQN;
			}

			//InvalidateRect( &rect );InvalidateRect( &AllRect );
			intCP = 2;
			InvalidateRect( NULL );
			return;
		}
	}


}


void CMyDlg::DrawJRect( CDC *dc )
{
	//显示低电平部分
	CRect rect[] ={ 						
		CRect( 5,89,43,38 ),//输入信号J
		CRect( 48,106,87,7 )
	};

	int rect_num = 2;
	CDC mem_dc;

	mem_dc.CreateCompatibleDC( dc );
	if( 2 != intJ ){
		mem_dc.SelectObject( &res[2-intJ] );
		for( int j = 0; j < rect_num; j++ )
			dc->BitBlt( 
			rect[j].left ,rect[j].top ,
			rect[j].right ,rect[j].bottom ,
			&mem_dc,rect[j].left ,rect[j].top ,SRCCOPY );
	}

}

void CMyDlg::DrawKRect(  CDC *dc )
{
	//显示低电平部分
	CRect rect[] ={ 						
		CRect( 9,230,43,38 ),//输入信号J
		CRect( 53,251,84,2 )
	};

	int rect_num = 2;
	CDC mem_dc;

	mem_dc.CreateCompatibleDC( dc );
	if( 2 != intK ){
		mem_dc.SelectObject( &res[2-intK] );
		for( int j = 0; j < rect_num; j++ )
			dc->BitBlt( 
			rect[j].left ,rect[j].top ,
			rect[j].right ,rect[j].bottom ,
			&mem_dc,rect[j].left ,rect[j].top ,SRCCOPY );
	}
}

void CMyDlg::DrawCPRect(  CDC * dc)
{
//显示低电平部分
	CRect rect[][10] ={ 						
		{
			CRect( 9,381,46,40 ),//输入信号J
			CRect( 52,339,218,73 ),
			CRect( 69,239,3,125 ),
			CRect( 61,121,76,120 )
		},
		{
			CRect( 309,329,67,84 ),
			CRect( 361,236,2,93 ),
			CRect( 355,122,51,113 )
		}

	};

	int rect_num[] = { 4, 3 };
	CDC mem_dc;

	mem_dc.CreateCompatibleDC( dc );
	if( 2 != intCP ){
		mem_dc.SelectObject( &res[2-intCP] );
		for( int j = 0; j < rect_num[0]; j++ )
			dc->BitBlt( 
			rect[0][j].left ,rect[0][j].top ,
			rect[0][j].right ,rect[0][j].bottom ,
			&mem_dc,rect[0][j].left ,rect[0][j].top ,SRCCOPY );
	}
	mem_dc.SelectObject( &res[1+intCP] );
	for( int j = 0; j < rect_num[1]; j++ ){
		dc->BitBlt( 
		rect[1][j].left ,rect[1][j].top ,
		rect[1][j].right ,rect[1][j].bottom ,
		&mem_dc,rect[1][j].left ,rect[1][j].top ,SRCCOPY );
	}

	if( trigging ){
		CRect rrect( 24,449, 456,61 );


		mem_dc.SelectObject( &res[2] );
		dc->BitBlt( 
		rrect.left ,rrect.top ,
		rrect.right ,rrect.bottom ,
		&mem_dc,rrect.left ,rrect.top ,SRCCOPY );
	}




}

void CMyDlg::DrawQRect(  CDC * dc )
{
	//显示低电平部分
	CRect rect[][10] ={ 						
		{
			CRect( 688,84,43,38 ),//输入信号J
			CRect( 566,95,65,87 ),
			CRect( 483,183,81,51 ),
			CRect( 633,97,52,18 ),
			CRect( 650,113,13,134 ),
			CRect( 648,249,26,84 ),
			CRect( 88,267,49,67 ),
			CRect( 131,312,230,41 ),
			CRect( 363,309,309,78 )
		},
		{
			CRect( 692,227,43,38 ),
			CRect( 658,226,33,42 ),
			CRect( 564,182,92,87 ),
			CRect( 485,119,80,64 ),
			CRect( 618,106,25,128 ),
			CRect( 616,16,33,88 ),
			CRect( 77,22,57,80 ),
			CRect( 79,18,592,35 )
		}

	};

	int rect_num[] = { 9, 8 };
	CDC mem_dc;

	mem_dc.CreateCompatibleDC( dc );
	if( 2 != intQN ){
		mem_dc.SelectObject( &res[2-intQN] );
		for( int j = 0; j < rect_num[0]; j++ )
			dc->BitBlt( 
			rect[0][j].left ,rect[0][j].top ,
			rect[0][j].right ,rect[0][j].bottom ,
			&mem_dc,rect[0][j].left ,rect[0][j].top ,SRCCOPY );
	}
		mem_dc.SelectObject( &res[1+intQN] );
		for( int j = 0; j < rect_num[1]; j++ ){
			dc->BitBlt( 
			rect[1][j].left ,rect[1][j].top ,
			rect[1][j].right ,rect[1][j].bottom ,
			&mem_dc,rect[1][j].left ,rect[1][j].top ,SRCCOPY );
	}
}

void CMyDlg::DrawOthersRect( CDC * dc )
{
	if( !trigging ) return;
	//只在触发器动作时绘制

	//显示低电平部分
	CRect rect[][6] ={ 						
		{
			CRect( 450,100,90,14 )
		},
		{
			CRect( 452,245,90,10 )
		},
		{
			CRect( 311,100,92,19 ),
			CRect( 291,106,54,78 ),
			CRect( 206,185,83,49 )
		},
		{
			CRect( 312,241,49,19 ),
			CRect( 363,245,42,11 ),
			CRect( 289,184,55,59 ),
			CRect( 217,122,74,63 )
		},
		{
			CRect( 181,105,83,6 )
		},
		{
			CRect( 183,248,83,8 )
		}
	};
	int rect_num[] = { 1,1,3,4,1,1 };
	int * val[] = { 
		&intGates[3],&intGates[4],&intGates[5],&intGates[6],&intGates[7],&intGates[8]
	};
	int intGates[10];//门的输出信号,可以计算出2-intJ

	CDC mem_dc;

	mem_dc.CreateCompatibleDC( dc );
	for(  int i = 0; i < 6; i++ ){
		if( 2 != *val[i] ){
			mem_dc.SelectObject( &res[ 2-*val[i] ] );
			for( int j = 0; j < rect_num[i]; j++ )
				dc->BitBlt( 
				rect[i][j].left ,rect[i][j].top ,
				rect[i][j].right ,rect[i][j].bottom ,
				&mem_dc,rect[i][j].left ,rect[i][j].top ,SRCCOPY );
		}
	}

}



void CMyDlg::OnTimer( UINT )
{
	//根据当前时刻,以及输入信号计算几个门的输出信号
	CRect rect( 24,449, 456,61 );


	rect.right = rect.left + rect.right*step/400;
	rect.bottom = rect.top + rect.bottom;
	step++;
	if( (step/100 == 1) || (step/100 == 2) ){
		if( intCP == 1 ){
			InvalidateRect( &rect );
			return;
		}
		else{
			intCP = 1;
			InvalidateRect( &AllRect );
		}
	}
	else if( (step/100 == 0) || (step/100 == 3) ){
		if( intCP == 0 ){
			InvalidateRect( &rect );
			return;
		}
		else{
			intCP = 0;
			InvalidateRect( &AllRect );
		}
	}
	else{
		KillTimer( m_nTimer );
		trigging = false;
	}

	intGates[7] = !(intCP & intJ & !intQN);
	intGates[8] = !(intCP & intK & intQN);
	if( intGates[7] && intGates[8] ){
	}
	else{
		intGates[5] = intGates[8];
		intGates[6] = !intGates[8];
	}
	intGates[3] = !(!intCP & intGates[5]);
	intGates[4] = !(!intCP & intGates[6]);
	if( intGates[3] && intGates[4] ){//如果3,4都是1,则不变
	}
	else{
		intGates[1] = intGates[4];
		intGates[2] = !intGates[4];
		intQN = intGates[1];
	}
}
void CMyDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}


⌨️ 快捷键说明

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