源程式:
// zouxinyu_MechanismView.cpp : implementation of the CZouxinyu_MechanismView class
//
#include "stdafx.h"
#include "zouxinyu_Mechanism.h"
#include "math.h"
#include "zouxinyu_MechanismDoc.h"
#include "zouxinyu_MechanismView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView
IMPLEMENT_DYNCREATE(CZouxinyu_MechanismView, CView)
BEGIN_MESSAGE_MAP(CZouxinyu_MechanismView, CView)
//{{AFX_MSG_MAP(CZouxinyu_MechanismView)
ON_COMMAND(ID_INPUT_SIZE, OnInputSize)
ON_COMMAND(ID_SIMU_START, OnSimuStart)
ON_COMMAND(ID_SIMU_STOP, OnSimuStop)
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView construction/destruction
CZouxinyu_MechanismView::CZouxinyu_MechanismView()
{
// TODO: add construction code here
// TODO: add construction code here
ab=300;
bc=600;
cd=600;
ad=500;
sita4=0;
angle1=45;
angle2=0;
angle3=0;
}
CZouxinyu_MechanismView::~CZouxinyu_MechanismView()
{
}
BOOL CZouxinyu_MechanismView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView drawing
void CZouxinyu_MechanismView::OnDraw(CDC* pDC)
{
CZouxinyu_MechanismDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//設定顯示區域,及原點位置
CRect rectClient;
GetClientRect(rectClient); // 獲得當前視窗的客戶區大小
pDC->SetMapMode(MM_ISOTROPIC); // 設定MM_ANISOTROPIC映射模式
pDC->SetWindowExt(1500,1500); // 設定視窗范圍
pDC->SetViewportExt(rectClient.right,-rectClient.bottom);// 設定視口范圍
pDC->SetViewportOrg(rectClient.right/2,rectClient.bottom/2); // 設定視口原點
double pi=3.1415926; // 定義圓周率π
double bx,by,cx,cy; //鉸鏈B、C點的坐標
double L,fai,aa,bb; //定義中間變數
angle1=angle1*pi/180; //將角度值轉化為弧度值
sita4=sita4*pi/180;
L=sqrt(ab*ab+ad*ad-2*ab*ad*cos(angle1-sita4*pi/180));
fai = atan((ad * sin(sita4) - ab * sin(angle1)) / (ad * cos(sita4) - ab * cos(angle1)));
aa = (bc * bc - L * L - cd * cd) / (2 * L * cd);
angle3 = acos(aa) + fai;
bb = (L * sin(fai) + cd * sin(angle3)) / (L * cos(fai) + cd * cos(angle3));
angle2 = atan(bb);
if (bb < 0)
{
angle2 = angle2 + pi;
}
bx = ab * cos(angle1);
by = ab * sin(angle1);
cx = bx + bc * cos(angle2);
cy = by + bc * sin(angle2);
//設定畫圖需要的引數
CPen NewPen1,NewPen2;//宣告畫筆物件
CPen* pOldPen; //保存原先畫筆的指標
//初始化實線、8像素寬的紅色畫筆
NewPen1.CreatePen(PS_SOLID,8,RGB(255,0,0));
//將畫筆選入設備物件
pOldPen=pDC->SelectObject(&NewPen1);
//畫AB桿
pDC->MoveTo(0,0);
pDC->LineTo(bx,by);
//畫BC桿
pDC->MoveTo(bx,by);
pDC->LineTo(cx,cy);
pDC->Ellipse(bx-20,by-20,bx+20,by+20); //畫轉動副B
//畫CD桿
pDC->MoveTo(cx,cy);
pDC->LineTo(ad,0);
pDC->Ellipse(cx-20,cy-20,cx+20,cy+20); //畫轉動副C
//畫AD桿
pDC->MoveTo(0,0);
pDC->LineTo(ad,0);
pDC->Ellipse(-20,-20,20,20); //畫轉動副A
pDC->Ellipse(ad-20,-20,ad+20,20); //畫轉動副D
//畫機架
for (int i=1;i<=20;i++)
{
pDC->MoveTo(20*i,0);
pDC->LineTo(20*i-20,-20);
}
pDC->SelectObject(pOldPen);
angle1=angle1*180/pi;
}
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView printing
BOOL CZouxinyu_MechanismView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CZouxinyu_MechanismView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CZouxinyu_MechanismView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView diagnostics
#ifdef _DEBUG
void CZouxinyu_MechanismView::AssertValid() const
{
CView::AssertValid();
}
void CZouxinyu_MechanismView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CZouxinyu_MechanismDoc* CZouxinyu_MechanismView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZouxinyu_MechanismDoc)));
return (CZouxinyu_MechanismDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZouxinyu_MechanismView message handlers
void CZouxinyu_MechanismView::OnInputSize()
{
// TODO: Add your command handler code here
//顯示四桿機構輸入對話框
CParameterDlg Zouxinyu_MechanismDlg; //定義CParameterDlg類的一個物件
//初始化四桿機構引數輸入對話框
Zouxinyu_MechanismDlg.m_ab=ab;
Zouxinyu_MechanismDlg.m_bc=bc;
Zouxinyu_MechanismDlg.m_cd=cd;
Zouxinyu_MechanismDlg.m_ad=ad;
//從四桿機構引數輸入對話框中得到用戶輸入的各桿長尺寸
if(Zouxinyu_MechanismDlg.DoModal()==IDOK)
{
ab=Zouxinyu_MechanismDlg.m_ab;
bc=Zouxinyu_MechanismDlg.m_bc;
cd=Zouxinyu_MechanismDlg.m_cd;
ad=Zouxinyu_MechanismDlg.m_ad;
}
//呼叫OnDraw函式按照用戶輸入的引數重新繪制四桿機構
Invalidate(TRUE);
}
void CZouxinyu_MechanismView::OnSimuStart()
{
// TODO: Add your command handler code here
SetTimer(1,100,NULL);
}
void CZouxinyu_MechanismView::OnSimuStop()
{
// TODO: Add your command handler code here
KillTimer(1);
}
void CZouxinyu_MechanismView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (angle1>360)
angle1=angle1-360; //當曲柄轉動角度大于360度時,重新從0度開始旋轉。
angle1=angle1+2; // 曲柄每次轉動2度
Invalidate(TRUE); // 呼叫OnDraw函式,強制視窗重繪。
CView::OnTimer(nIDEvent);
}


出現的錯
uj5u.com熱心網友回復:
CParameterDlg Zouxinyu_MechanismDlg;包含 ParameterDlg。h
uj5u.com熱心網友回復:
那應該怎么改???uj5u.com熱心網友回復:
在你的源檔案里加上:
#include "ParameterDlg.h"
重新編譯
uj5u.com熱心網友回復:
多謝大神,十分感謝!!!!轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/113983.html
標籤:基礎類
上一篇:error C2106:'=' : left operand must be l-value是什么錯誤,請幫忙
下一篇:分類回歸樹

