§o lêng vµ tù ®éng ®iÒu khiÓn
//
#include "stdafx.h"
#include "Mo Phong.h"
#include "Mo PhongDlg.h"
#include"math.h"
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
};
5
§o lêng vµ tù ®éng ®iÒu khiÓn
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()
/////////////////////////////////////////////////////////////////////////////
// CMoPhongDlg dialog
CMoPhongDlg::CMoPhongDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMoPhongDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMoPhongDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in
Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMoPhongDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMoPhongDlg)
DDX_Control(pDX, IDC_LED1, m_led1);
DDX_Control(pDX, IDC_LED10, m_led10);
DDX_Control(pDX, IDC_LED2, m_led2);
6
§o lêng vµ tù ®éng ®iÒu khiÓn
DDX_Control(pDX, IDC_LED3, m_led3);
DDX_Control(pDX, IDC_LED4, m_led4);
DDX_Control(pDX, IDC_LED5, m_led5);
DDX_Control(pDX, IDC_LED6, m_led6);
DDX_Control(pDX, IDC_LED7, m_led7);
DDX_Control(pDX, IDC_LED8, m_led8);
DDX_Control(pDX, IDC_LED9, m_led9);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMoPhongDlg, CDialog)
//{{AFX_MSG_MAP(CMoPhongDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_HANGCHUC, OnHangchuc)
ON_BN_CLICKED(IDC_HANGDONVI, OnHangdonvi)
ON_BN_CLICKED(IDC_HANGNGHIN, OnHangnghin)
ON_BN_CLICKED(IDC_HANGTRAM, OnHangtram)
ON_BN_CLICKED(IDC_HANGVAN, OnHangvan)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_REVERSE, OnReverse)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_START, OnStart)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMoPhongDlg message handlers
BOOL CMoPhongDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About " menu item to system menu.
7
§o lêng vµ tù ®éng ®iÒu khiÓn
// 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
// TODO: Add extra initialization here
m_1=0;
m_2=m_3=m_4=m_5=m_6=m_7=m_8=m_9=m_10=0;
m_led1.SetWindowText("0");
m_led2.SetWindowText("0");
m_led3.SetWindowText("0");
m_led4.SetWindowText("0");
m_led5.SetWindowText("0");
m_led6.SetWindowText("0");
m_led7.SetWindowText("0");
m_led8.SetWindowText("0");
m_led9.SetWindowText("0");
m_led10.SetWindowText("0");
m_degree=0;
temp=TRUE;
t=0;
// m_vong=0;
return TRUE; // return TRUE unless you set the focus to a control
8
§o lêng vµ tù ®éng ®iÒu khiÓn
}
void CMoPhongDlg::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 CMoPhongDlg::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
9
§o lêng vµ tù ®éng ®iÒu khiÓn
{
CClientDC pDC(this);
OnDraw(&pDC);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMoPhongDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMoPhongDlg::OnHangchuc()
{
if(m_4==9) m_4=0;
else
m_4++;
CString s;
s.Format("%d",m_4);
m_led4.SetWindowText(s);
m_vong=m_1*10000+m_2*1000+m_3*100+m_4*10+m_5;
}
void CMoPhongDlg::OnHangdonvi()
{
if(m_5==9) m_5=0;
else
m_5++;
CString s;
s.Format("%d",m_5);
m_led5.SetWindowText(s);
m_vong=m_1*10000+m_2*1000+m_3*100+m_4*10+m_5;
}
void CMoPhongDlg::OnHangnghin()
{
10
§o lêng vµ tù ®éng ®iÒu khiÓn
if(m_2==9) m_2=0;
else
m_2++;
CString s;
s.Format("%d",m_2);
m_led2.SetWindowText(s);
m_vong=m_1*10000+m_2*1000+m_3*100+m_4*10+m_5;
}
void CMoPhongDlg::OnHangtram()
{
if(m_3==9) m_3=0;
else
m_3++;
CString s;
s.Format("%d",m_3);
m_led3.SetWindowText(s);
m_vong=m_1*10000+m_2*1000+m_3*100+m_4*10+m_5;
}
void CMoPhongDlg::OnHangvan()
{
if(m_1==9) m_1=0;
else
m_1++;
CString s;
s.Format("%d",m_1);
m_led1.SetWindowText(s);
m_vong=m_1*10000+m_2*1000+m_3*100+m_4*10+m_5;
}
void CMoPhongDlg::OnPause()
{
KillTimer(1);
}
11
§o lêng vµ tù ®éng ®iÒu khiÓn
void CMoPhongDlg::OnReset()
{
m_1=m_2=m_3=m_4=m_5=m_6=m_7=m_8=m_9=m_10=0;
// thiet lap ve khong
m_led1.SetWindowText("0");
m_led2.SetWindowText("0");
m_led3.SetWindowText("0");
m_led4.SetWindowText("0");
m_led5.SetWindowText("0");
m_led6.SetWindowText("0");
m_led7.SetWindowText("0");
m_led8.SetWindowText("0");
m_led9.SetWindowText("0");
m_led10.SetWindowText("0");
KillTimer(1);
}
void CMoPhongDlg::OnReverse()
{
if(temp==TRUE)
temp=FALSE;
else
{
temp=TRUE;
}
}
void CMoPhongDlg::OnDraw(CDC *pDC)
{
CRect rectWin;
GetWindowRect(rectWin);
rectWin.top+=32;
CWnd*pWnd=GetDlgItem(IDC_DISPLAY);
CRect rectD;
pWnd->GetWindowRect(rectD);
int cx,cy;
cx=rectD.CenterPoint().x;
cy=rectD.CenterPoint().y;
cx-=rectWin.left;
cy-=rectWin.top;
12
§o lêng vµ tù ®éng ®iÒu khiÓn
CBrush brush(RGB(0,255,0));
CBrush * oldBrush=pDC->SelectObject(&brush);
CPen *pen=new CPen(PS_SOLID,1,RGB(0,0,255));
CPen*oldPen =pDC->SelectObject(pen);
pDC->Ellipse(cx-50,cy-50,cx+50,cy+50);
CPen*pen1=new CPen(PS_SOLID,3,RGB(255,0,0));
CPen*oldPen1 =pDC->SelectObject(pen1);
double angle;
angle =m_degree*3*3.14/180;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
angle+=3.14/3;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
angle+=3.14/3;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
angle+=3.14/3;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
angle+=3.14/3;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
angle+=3.14/3;
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
pDC->MoveTo(cx,cy);
pDC->LineTo(cx+(int)(50*sin(angle)),cy-(int)(50*cos(angle)));
pDC->SelectObject(oldBrush);
pDC->SelectObject(oldPen);
pDC->SelectObject(oldPen1);
}
void CMoPhongDlg::OnTimer(UINT nIDEvent)
{
if(temp==TRUE)
{
m_degree++;
13
§o lêng vµ tù ®éng ®iÒu khiÓn
if(m_degree>=120) m_degree=0;
}
else
{
m_degree ;
if(m_degree<=0) m_degree=120;
}
if(m_10<9)
m_10++;
else
{
m_10=0;
if(m_9<9)
m_9++;
else
{
m_9=0;
if(m_8<9)
m_8++;
else
{
m_8=0;
if(m_7<9)
m_7++;
else
{
m_7=0;
if(m_6<9)
m_6++;
else
MessageBox("Tran
So","Warning",MB_OK);
}
}
}
}
t++;
if(t==100)
{
14
Không có nhận xét nào:
Đăng nhận xét