?? circuedlg.cpp
字號:
// circueDlg.cpp : implementation file
//
#include "stdafx.h"
#include "circue.h"
#include "circueDlg.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()
};
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()
/////////////////////////////////////////////////////////////////////////////
// CCircueDlg dialog
CCircueDlg::CCircueDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCircueDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCircueDlg)
m_avNum = 0;
m_circum = 0.0;
m_doNum = 0;
m_err = 0.0;
m_number = 2000;
//m_stat = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCircueDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCircueDlg)
DDX_Control(pDX, IDC_PRO, m_pro);
DDX_Control(pDX, IDC_PICTURE, m_picture);
//DDX_Text(pDX, IDC_STAT, m_stat);
DDX_Text(pDX, IDC_AVNUM, m_avNum);
DDX_Text(pDX, IDC_CirCUM, m_circum);
DDX_Text(pDX, IDC_DONUM, m_doNum);
DDX_Text(pDX, IDC_ERR, m_err);
DDX_Text(pDX, IDC_NUMBER, m_number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCircueDlg, CDialog)
//{{AFX_MSG_MAP(CCircueDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_AgAIN, OnAgAIN)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCircueDlg message handlers
BOOL CCircueDlg::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
//get the max_rand/2
max = RAND_MAX/2.0;
//m_number = 200000;
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCircueDlg::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 CCircueDlg::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
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCircueDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCircueDlg::Count()
{
double a; //臨時變量,用來計算隨機數的半徑
this->DrawPicture ( TRUE, TRUE ); //顯示圖象
this->UpdateData ( TRUE ); //得到數據,用當前框中的數據,來計算概率
m_pro.SetRange32 (0,m_number); //設置進度條的范圍
m_avNum = 0; //將命中的次數設置為0
for ( int i=1; i<=m_number; i++ ) //開始循環
{
randy = ( max - rand() )/max; //得到隨機數,具體的方法,是利用計算機中存在的rand()
randx = ( max - rand() )/max; //得到隨機數
a = randy*randy+randx*randx; //得到隨機數的半徑
if ( a<=1 ) //判斷
{
m_avNum = m_avNum++; //符合條件 命中點+1
this->DrawPicture ( TRUE, FALSE ); //顯示圖象
}
else
this->DrawPicture ( FALSE, FALSE ); //不符合條件,顯示圖象
m_pro.SetPos ( i ); //設置進度條當前位置
}
m_doNum = m_number; //運行次數為設置的次數
m_circum = (double)(m_avNum); //改變整數為小數型
m_circum = 4*m_circum / m_doNum; //計算圓周率
m_err = 3.1415926 - m_circum; //圓周率與現在的圓周率的誤差
this->UpdateData ( FALSE ); //顯示運算的結果
}
void CCircueDlg::DrawPicture( BOOL av, BOOL first )
{
if ( first ) //如果是第一次,做圖象數據處理的初始工作
{
hdc = ::GetDC ( m_picture.m_hWnd ); //得到句柄
::GetClientRect ( m_picture.m_hWnd, &rect ); //得到圖象區的大小
rect.bottom = rect.bottom -20;
rect.top = rect.top +20;
rect.left = rect.left + 20;
rect.right = rect.right - 20; //以上為不讓圖象區走初邊界
c_x = ( rect.left + rect.right )/2;
c_y = ( rect.bottom + rect.top )/2; //以上為設置圖象區的中點
if ( (rect.bottom-rect.top)<(rect.right-rect.left) )
{
dir = rect.bottom - rect.top;
rect.left = c_x - dir/2;
rect.right = c_x + dir/2;
}
else
{
dir = rect.right - rect.left;
rect.bottom = c_y + dir/2;
rect.top = c_y - dir/2;
}//設置顯示四方區的邊界
::Rectangle ( hdc, rect.left, rect.top, rect.right, rect.bottom );//畫出圖象顯示區
return;//返回
}
if ( av )
::SetPixel ( hdc, c_x + randx*dir/2, c_y + randy*dir/2, RGB ( 0, 0, 255 ) );//如果在圓半徑內,顯示為蘭色點
else
::SetPixel ( hdc, c_x + randx*dir/2, c_y + randy*dir/2, RGB ( 255, 0, 0 ) );//如果不在圓的半徑內,顯示為紅色點
}
void CCircueDlg::OnAgAIN()
{
// TODO: Add your control notification handler code here
this->Count();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -