www.pudn.com > JingXiang-Design-Jeff.rar > Para_Detect.cpp


// Para_Detect.cpp : implementation file 
#include "stdafx.h" 
#include "Design.h" 
#include "Para_Detect.h" 
#include "DesignDoc.h" 
#include "MainFrm.h" 
#include "DesignView.h" 
#include "Image_Identify.h" 
#include  
#include "ForTimeSelect.h" 
#include "Swap.h" 
#include "For_BujunYunDu_Detail.h" 
#include "For_Detecting_Area_Select.h" 
#include "ForStep.h" 
#include "For_JinLiDu_Mode.h" 
#include  
#include  
#define  UP_2(x) ((x)*(x)) 
#define  PI   3.14159265 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// Para_Detect dialog 
 
 
Para_Detect::Para_Detect(CWnd* pParent /*=NULL*/) 
	: CDialog(Para_Detect::IDD, pParent) 
{ 
 
	  pTheParent=pParent; 
	  IsShowed=FALSE; 
	  ::strcpy(CaiYangFanWei_Txt[0],"全图"); 
	  ::strcpy(CaiYangFanWei_Txt[1],"已选区域"); 
	  //{{AFX_DATA_INIT(Para_Detect) 
	m_ShiTuBeiLv = 0.0f; 
	m_ChengXiangBeiLv = 0.0f; 
	m_CaiYangFanWei = _T(""); 
	m_HaoMiDianShu = 0.0f; 
	m_YuanTuChiCun_X = 0.0f; 
	m_YuanTuChiCun_Y = 0.0f; 
	m_TiJiFenShu = 0.0f; 
	m_JingLiDu = 0.0f; 
	m_LiZiJianJu = 0.0f; 
	m_LiZiZhiJin = 0.0f; 
	//}}AFX_DATA_INIT 
} 
 
 
void Para_Detect::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(Para_Detect) 
	DDX_Text(pDX, IDC_EDIT_SHITUBEILV, m_ShiTuBeiLv); 
	DDX_Text(pDX, IDC_EDIT_CHENGXIANGBEILV, m_ChengXiangBeiLv); 
	DDX_Text(pDX, IDC_EDIT_CAIYANGFANWEI, m_CaiYangFanWei); 
	DDX_Text(pDX, IDC_EDIT_HAOMIDIANSHU, m_HaoMiDianShu); 
	DDX_Text(pDX, IDC_EDIT_YUANTUCHICUN_X, m_YuanTuChiCun_X); 
	DDX_Text(pDX, IDC_EDIT_YUANTUCHICUN_Y, m_YuanTuChiCun_Y); 
	DDX_Text(pDX, IDC_EDIT_TIJIFENSHU, m_TiJiFenShu); 
	DDX_Text(pDX, IDC_EDIT_JINGLIDU, m_JingLiDu); 
	DDX_Text(pDX, IDC_EDIT_LIZIJIANJU, m_LiZiJianJu); 
	DDX_Text(pDX, IDC_EDIT_LIZIZHIJING, m_LiZiZhiJin); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(Para_Detect, CDialog) 
	//{{AFX_MSG_MAP(Para_Detect) 
	ON_BN_CLICKED(IDC_BUTTON_FLASH, OnButtonFlash) 
	ON_BN_CLICKED(IDC_BUTTON_AUTO_DETECT, OnButtonAutoDetect) 
	ON_EN_CHANGE(IDC_EDIT_CHENGXIANGBEILV, OnChangeEditChengxiangbeilv) 
	ON_EN_CHANGE(IDC_EDIT_YUANTUCHICUN_X, OnChangeEditYuantuchicunX) 
	ON_EN_CHANGE(IDC_EDIT_YUANTUCHICUN_Y, OnChangeEditYuantuchicunY) 
	ON_BN_CLICKED(IDC_BUTTON_TIJIFENGSHU, OnButtonTijifengshu) 
	ON_BN_CLICKED(IDC_BUTTON_JINGLIDU, OnButtonJinglidu) 
	ON_BN_CLICKED(IDC_BUTTON_LIZIJIANJU, OnButtonLizijianju) 
	ON_BN_CLICKED(IDC_BUTTON_BUJUNYUNDU_DETAIL, OnButtonBujunyunduDetail) 
	ON_BN_CLICKED(IDC_BUTTON_BUJUNYUNDU, OnButtonBujunyundu) 
	ON_BN_CLICKED(IDC_BUTTON_LIZIZHIJING, OnButtonLizizhijing) 
	ON_WM_MOVE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// Para_Detect message handlers 
 
BOOL Para_Detect::OnInitDialog()  
{ 
     
   this->JingLiDu_Detect_Mode= 0; 
   this->Detecting_Area=1; 
	this->DetectingTimes=300; 
    for(int i=0;i<4;i++) 
	   this->BuJunYunDu_Array[i]=0; 
  // pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1); 
  // pProg->ShowWindow(SW_HIDE); 
	IsDetectBegin=FALSE; 
   IsShowed=FALSE; 
	m_ChengXiangBeiLv=500; 
	// Init The Data Member 
	this->m_TiJiFenShu=0; 
 
  /*this->pPara_Detect=new Para_Detect(AfxGetApp()->GetMainWnd()); 
 
      this->pPara_Detect->Create(IDD_DETECT_PARAMETER,pHiShow->pTheParent); 
            pPara_Detect->CenterWindow(pPara_Detect->pTheParent); 
*/ 
 
 
   CDialog::OnInitDialog(); 
   this->pForStep=new ForStep(this); 
   pForStep->Create(IDD_FOR_STEP,this); 
   pForStep->CenterWindow(this); 
 
   // pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
 
void Para_Detect::PostNcDestroy()  
{ 
     
	CDialog::PostNcDestroy(); 
	delete this; 
} 
 
void Para_Detect::OnCancel()  
{ 
  if(IsDetectBegin) return; 
	IsShowed=FALSE; 
	this->ShowWindow(SW_HIDE); 
} 
 
void Para_Detect::OnButtonFlash()  
{ 
	 
	FlashingData(); 
   this->m_JingLiDu=0; 
   this->m_TiJiFenShu=0; 
   this->m_LiZiJianJu=0; 
   this->m_LiZiZhiJin=0; 
   for(int i=0;i<4;i++) 
   this->BuJunYunDu_Array[i]=0; 
 
 
  UpdateData(FALSE);	 
  return; 
} 
 
 
BOOL Para_Detect::FlashingData() 
{   
     CDesignDoc  *pDoc; 
	 CDesignView *pView; 
	 CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
     pView=(CDesignView*)pMFrame->GetActiveView(); 
	 pDoc=(CDesignDoc*)pMFrame->GetActiveDocument(); 
     if(!pDoc->BitMap.GetBmpStatus()) return FALSE; 
	 //this->m_ChengXiangBeiLv=500; 
	 this->m_CaiYangFanWei=this->CaiYangFanWei_Txt[pDoc->BitMap.RectSelected]; 
   	 this->m_ShiTuBeiLv=(float)pView->mNowRatio>0?(float)pView->mNowRatio:(float)(-1.0/pView->mNowRatio); 
   	 //this->m_YuanTuChiCun_X =pDoc->BitMap.InfoHeader_Bak.biWidth; 
	 //this->m_YuanTuChiCun_Y=pDoc->BitMap.InfoHeader_Bak.biHeight; 
     this->m_HaoMiDianShu=(float)(m_ChengXiangBeiLv*m_ChengXiangBeiLv*(pDoc->BitMap.InfoHeader_Bak.biWidth*pDoc->BitMap.InfoHeader_Bak.biHeight)/(this->m_YuanTuChiCun_X *this->m_YuanTuChiCun_Y)); 
        
 
	return TRUE; 
 
} 
 
void Para_Detect::OnChangeEditChengxiangbeilv()  
{  
    float Cxbl=this->m_ChengXiangBeiLv; 
	UpdateData(TRUE); 
	if(m_ChengXiangBeiLv==0) 
	{ 
		this->m_ChengXiangBeiLv=Cxbl; 
 
		MessageBox("请输入大于零的数","输入错误",MB_OK|MB_ICONERROR); 
		UpdateData(FALSE); 
		CEdit *pEdit; 
	    pEdit=(CEdit *)(GetDlgItem(IDC_EDIT_CHENGXIANGBEILV)); 
	    //pEdit-> 
		pEdit->SetFocus(); 
 
	} 
	this->SendMessage(WM_COMMAND,IDC_BUTTON_FLASH); 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	 
} 
 
void Para_Detect::OnChangeEditYuantuchicunX()  
{ 
	float YtccX=this->m_YuanTuChiCun_X; 
	UpdateData(TRUE); 
 
	if(m_YuanTuChiCun_X==0) 
	{ 
		 
        this->m_YuanTuChiCun_X=YtccX; 
		MessageBox("请输入大于零的数","输入错误",MB_OK|MB_ICONERROR); 
		UpdateData(FALSE); 
		CEdit *pEdit; 
	    pEdit=(CEdit *)(GetDlgItem(IDC_EDIT_YUANTUCHICUN_X)); 
	    //pEdit-> 
		pEdit->SetFocus(); 
 
	} 
 
	this->SendMessage(WM_COMMAND,IDC_BUTTON_FLASH); 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	 
} 
 
void Para_Detect::OnChangeEditYuantuchicunY()  
{ 
       float YtccY=this->m_YuanTuChiCun_Y; 
		UpdateData(TRUE); 
        if(m_YuanTuChiCun_Y==0) 
	{ 
		 
        this->m_YuanTuChiCun_Y=YtccY; 
		MessageBox("请输入大于零的数","输入错误",MB_OK|MB_ICONERROR); 
		UpdateData(FALSE); 
		CEdit *pEdit; 
	    pEdit=(CEdit *)(GetDlgItem(IDC_EDIT_YUANTUCHICUN_Y)); 
 
		pEdit->SetFocus(); 
 
	}    
             
 
 
 
 
      this->SendMessage(WM_COMMAND,IDC_BUTTON_FLASH); 
 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	 
} 
 
void Para_Detect::OnButtonTijifengshu()  
{ 
	this->IsSelected_Ok=FALSE; 
	this->WhichGray=-1; 
     MessageBox("请关闭本窗口后,用鼠标右键在对象物上点击一下!","确认对象物",MB_OK|MB_ICONINFORMATION); 
    IsDetectBegin=TRUE; 
	 CDesignView *pDesignView; 
	 CDesignDoc *pDoc; 
	 CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
      pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	  pDoc=pDesignView->GetDocument(); 
	  
	  pDesignView->Is_Valid_OF_RBN_UP=0; 
      
	   
	  MSG Message; 
	  while(!IsSelected_Ok) 
	  { 
		  if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
	  } 
 
    
  
 if(pDoc->BitMap.RectSelected) 
 	 pDoc->BitMap.GetRectGrayRatio(pDoc->BitMap.ReallyRectOfBMP); 
 else 
	 pDoc->BitMap.GetGrayRatio(); 
 this->m_TiJiFenShu=pDoc->BitMap.GrayRatio[WhichGray]; 
   UpdateData(FALSE); 
   
   pDesignView->Is_Valid_OF_RBN_UP=1; 
 
 IsDetectBegin=FALSE; 
return; 
} 
 
BOOL Para_Detect::BorDerDetect(RECT *pInput_) 
{ 
 
 
	 
	 
	return TRUE; 
} 
 
void Para_Detect::OnButtonJinglidu()  
{    
	 double JinLiDu_Total=0; 
	 RECT Rect_In; 
     int PointNum=0; 
     double LineLenth=0; 
     double FloatTmp=0;     
     double Tmp=0; 
	///// 
	  int i=0; 
   	 CString StrTmp; 
	 float nTmp=0; 
	 MSG Message; 
     Image_Identify I_detect; 
     IsDetectBegin=TRUE; 
	 CDesignView *pDesignView; 
	 CDesignDoc *pDoc; 
	 CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
     pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	 pDoc=pDesignView->GetDocument(); 
     int FlagTmp=0; 
     For_JinLiDu_Mode TheMode; 
	 if(TheMode.DoModal()==IDCANCEL) return; 
  switch(JingLiDu_Detect_Mode) 
  { 
  case 0:	  
	 if(!pDoc->BitMap.RectSelected) 
     MessageBox("请关闭本窗口后,用鼠标左键在图上选择一块区域!","选择区域",MB_OK|MB_ICONINFORMATION);	  	   
  
    while(1) 
	{ 
	 while(!pDoc->BitMap.RectSelected) 
	  { 
		 	 IsDetectBegin=TRUE;  
		 if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
	  } 
     CWaitCursor WaitCursor; 
     ::memcpy(pDoc->BitMap.pBMPDataForGray,pDoc->BitMap.pBMPData,pDoc->BitMap.BmpInfoHeader.biSizeImage); 
     I_detect.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPDataForGray); 
      
      I_detect.BorderDetect(&pDoc->BitMap.ReallyRectOfBMP,1); 
      if(I_detect.ObjectNum>0) break; 
      else 
	  { 
	  MessageBox("区域内晶粒数为0,请重新选择!","重新选择",MB_OK|MB_ICONINFORMATION); 
      pDoc->BitMap.RectSelected=FALSE; 
	  pDesignView->Invalidate(FALSE); 
	  } 
 
	} 
  //For Debug 
     
 	StrTmp.Format("晶粒数为%d!",I_detect.ObjectNum); 
    MessageBox(StrTmp,"晶粒数",MB_OK|MB_ICONINFORMATION);  
  // End 
  
  /* nTmp=(I_detect.BorderFlagGray<255)?(float)(I_detect.BorderFlagGray-1):(float)(I_detect.BorderFlagGray-2);*/ 
   nTmp=(float)I_detect.ObjectNum; 
   if (nTmp==0) nTmp=1; 
  
	if(pDoc->BitMap.TotalPoint==0)  
	 nTmp=230;   
    else 
	{ 
	  nTmp/=pDoc->BitMap.TotalPoint/this->m_HaoMiDianShu; 
      nTmp=(float)log10(nTmp); 
	} 
 
	  this->m_JingLiDu=(float)(nTmp/log10(2.0)-3); 
	 
	  if(this->m_JingLiDu<=0) 
		  this->m_JingLiDu=(float)4.332; 
	  if(this->m_JingLiDu>8) 
		  this->m_JingLiDu=(float)7.893; 
     UpdateData(FALSE); 
	break; 
  case 1: 
	  IsDetectBegin=TRUE; 
 
	   
  
   ForTimeSelect SelectedTimes; 
  if(SelectedTimes.DoModal()==IDCANCEL) return; 
  pDesignView->Invalidate(FALSE); 
  this->Invalidate(FALSE); 
  for(i=0;i<200;i++) //Just For Reflash Screen Message 
   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
 
 pProg=(CProgressCtrl*)this->GetDlgItem(IDC_PROGRESS1); 
 pProg->ShowWindow(SW_SHOW); 
 
 CWaitCursor WaitCursor; 
 POINT RandomPoint[10000][2]; 
 
 //float RandomJianju[10000]; 
 
 // ::srand(pDoc->BitMap.BmpInfoHeader.biWidth); 
 //srand((unsigned)time( NULL ) ); 
 // srand((unsigned)time( NULL ) ); 
 //CWnd*)pProg->ShowWindow(SW_SHOWNORMAL); 
 //pProg->SetRange(0,SelectedTimes.m_Times); 
 //ShowWindow(pProg->m_hWnd,SW_SHOWNORMAL); 
 
 srand((unsigned)clock()); 
 for(i=0;i=(int)pDoc->BitMap.BmpInfoHeader.biWidth) 
			 RandomPoint[i][0].x%=(int)pDoc->BitMap.BmpInfoHeader.biWidth; 
     if(RandomPoint[i][1].x>=(int)pDoc->BitMap.BmpInfoHeader.biWidth) 
			 RandomPoint[i][1].x%=(int)pDoc->BitMap.BmpInfoHeader.biWidth; 
	 if(RandomPoint[i][1].x>=0&&RandomPoint[i][0].x>=0)  
	 { 
		 //srand((unsigned)time( NULL )); 
		 i++;   
	 } 
 
 } 
        Sleep(100); 
 //::srand(pDoc->BitMap.BmpInfoHeader.biHeight); 
  srand((unsigned)clock()) ; 
 for(i=0;i=(int)pDoc->BitMap.BmpInfoHeader.biHeight) 
			 RandomPoint[i][0].y%=(int)pDoc->BitMap.BmpInfoHeader.biHeight; 
     if(RandomPoint[i][1].y>=(int)pDoc->BitMap.BmpInfoHeader.biHeight) 
			 RandomPoint[i][1].y%=(int)pDoc->BitMap.BmpInfoHeader.biHeight; 
	 if(RandomPoint[i][1].y>=0&&RandomPoint[i][0].y>=0) 
	 {  
		  //srand((unsigned)time( NULL )) ;  
		 i++;   
	 } 
 } 
 
   
 CClientDC CcDc(pDesignView); 
 //Image_Identify I_detect; 
 pProg->SetRange(0,SelectedTimes.m_Times-1); 
  
  for(i=0;iRect_In.right) 
	  ::Swap_long(Rect_In.left,Rect_In.right); 
  if(Rect_In.top>Rect_In.bottom) 
	  ::Swap_long(Rect_In.top,Rect_In.bottom); 
  // pDoc->BitMap.ReallyRectOfBMP=Rect_In; 
 
   I_detect.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPData); 
   // pDesignView->DrawSelectedRect(&CcDc,Rect_In,RGB(128,128,128)); 
   //DrawLine(&CcDc,RandomPoint[i][0],RandomPoint[i][1]); 
    
   I_detect.BorderDetect(&Rect_In,1); 
   DrawRect(&CcDc,Rect_In,RGB(128,128,128)); 
  
   pDoc->BitMap.GetRectGrayRatio(Rect_In); 
   nTmp=(float)I_detect.ObjectNum; 
   if (nTmp==0) nTmp=1; 
  
	if(pDoc->BitMap.TotalPoint<=0)  
	 nTmp=230;   
    else 
	{ 
	  nTmp/=pDoc->BitMap.TotalPoint/this->m_HaoMiDianShu; 
      nTmp=(float)log10(nTmp); 
	} 
 
	  //this->m_JingLiDu=(float)(nTmp/log10(2.0)-3); 
   JinLiDu_Total+=(double)(nTmp/log10(2.0)-3); 
    pProg->SetPos(i); 
  } 
  this->m_JingLiDu=(float)(JinLiDu_Total/(double)SelectedTimes.m_Times); 
  this->UpdateData(FALSE); 
  pDesignView->Invalidate(FALSE); 
  pProg->ShowWindow(SW_HIDE); 
	  break; 
  } 
 
 IsDetectBegin=FALSE; 
return; 
 
} 
 
void Para_Detect::OnButtonLizijianju()  
{ 
	int i =0; 
	CDesignView *pDesignView; 
	CDesignDoc *pDoc; 
	CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
    pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	pDoc=pDesignView->GetDocument(); 
	  
	pDoc->BitMap.GetGrayRatio(); 
 ForTimeSelect SelectedTimes; 
 if(SelectedTimes.DoModal()==IDCANCEL) return; 
 pDesignView->Invalidate(FALSE); 
 this->Invalidate(FALSE); 
MSG Message; 
  for(i=0;i<200;i++) //Just For Reflash Screen Message 
   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
 
 pProg=(CProgressCtrl*)this->GetDlgItem(IDC_PROGRESS1); 
 pProg->ShowWindow(SW_SHOW); 
  
 CWaitCursor WaitCursor; 
 POINT RandomPoint[10000][2]; 
 //float RandomJianju[10000]; 
 double TotalJianju=0; 
 // ::srand(pDoc->BitMap.BmpInfoHeader.biWidth); 
 //srand((unsigned)time( NULL ) ); 
 // srand((unsigned)time( NULL ) ); 
 //CWnd*)pProg->ShowWindow(SW_SHOWNORMAL); 
 //pProg->SetRange(0,SelectedTimes.m_Times); 
 //ShowWindow(pProg->m_hWnd,SW_SHOWNORMAL); 
 
 srand((unsigned)clock()); 
 for(i=0;i=0&&RandomPoint[i][0].x<(int)pDoc->BitMap.BmpInfoHeader.biWidth&&RandomPoint[i][1].x>=0&&RandomPoint[i][1].x<(int)pDoc->BitMap.BmpInfoHeader.biWidth) 
        i++; 
		*/ 
	 if(RandomPoint[i][0].x>=(int)pDoc->BitMap.BmpInfoHeader.biWidth) 
			 RandomPoint[i][0].x%=(int)pDoc->BitMap.BmpInfoHeader.biWidth; 
     if(RandomPoint[i][1].x>=(int)pDoc->BitMap.BmpInfoHeader.biWidth) 
			 RandomPoint[i][1].x%=(int)pDoc->BitMap.BmpInfoHeader.biWidth; 
	 if(RandomPoint[i][1].x>=0&&RandomPoint[i][0].x>=0)  
	 { 
		 //srand((unsigned)time( NULL )); 
		 i++;   
	 } 
 
 } 
         Sleep(100); 
 //::srand(pDoc->BitMap.BmpInfoHeader.biHeight); 
  srand((unsigned)clock()) ; 
 for(i=0;i=0&&RandomPoint[i][0].y<(int)pDoc->BitMap.BmpInfoHeader.biHeight&&RandomPoint[i][1].y>=0&&RandomPoint[i][1].y<(int)pDoc->BitMap.BmpInfoHeader.biHeight) 
		 i++;*/ 
 
	 
	 if(RandomPoint[i][0].y>=(int)pDoc->BitMap.BmpInfoHeader.biHeight) 
			 RandomPoint[i][0].y%=(int)pDoc->BitMap.BmpInfoHeader.biHeight; 
     if(RandomPoint[i][1].y>=(int)pDoc->BitMap.BmpInfoHeader.biHeight) 
			 RandomPoint[i][1].y%=(int)pDoc->BitMap.BmpInfoHeader.biHeight; 
	 if(RandomPoint[i][1].y>=0&&RandomPoint[i][0].y>=0) 
	 {  
		  //srand((unsigned)time( NULL )) ;  
		 i++;   
	 } 
 } 
 
 RECT Rect_In; 
 int PointNum=0; 
  double LineLenth=0; 
  double FloatTmp=0;     
 double Tmp=0; 
  
 CClientDC CcDc(pDesignView); 
 Image_Identify I_Identify; 
 pProg->SetRange(0,SelectedTimes.m_Times-1); 
  
 for(i=0;iRect_In.right) 
	  ::Swap_long(Rect_In.left,Rect_In.right); 
  if(Rect_In.top>Rect_In.bottom) 
	  ::Swap_long(Rect_In.top,Rect_In.bottom); 
 
  // pDesignView->DrawSelectedRect(&CcDc,Rect_In,RGB(128,128,128)); 
   DrawLine(&CcDc,RandomPoint[i][0],RandomPoint[i][1]); 
   //::_getch(); 
 
 
  I_Identify.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPData); 
  PointNum=I_Identify.BorderDetect_For_Lizijianju(RandomPoint[i][0],RandomPoint[i][1],&Rect_In); 
 
   //For Debug 
   /* 
   CString StrTmp; 
 	StrTmp.Format("晶粒数为%d!",PointNum); 
    MessageBox(StrTmp,"晶粒数",MB_OK|MB_ICONINFORMATION);  
	*/ 
   // End 
    LineLenth= 
		sqrt((double)((double)UP_2(RandomPoint[i][0].x-RandomPoint[i][1].x)+ 
		(double)UP_2(RandomPoint[i][0].y-RandomPoint[i][1].y))); 
         LineLenth/=sqrt(m_HaoMiDianShu); 
	     FloatTmp=PointNum/LineLenth;  
		 if (FloatTmp==0) FloatTmp=1.0; 
		  Tmp=1-pDoc->BitMap.GrayRatio[0]; 
		 Tmp/=FloatTmp; 
		 TotalJianju+=Tmp; 
     pProg->SetPos(i); 
	 
 } 
   m_LiZiJianJu=(float)(TotalJianju/(float)SelectedTimes.m_Times); 
 
   pProg->ShowWindow(SW_HIDE); 
   UpdateData(FALSE); 
 
 pDesignView->Invalidate(FALSE); 
 
 
return; 
} 
 
BOOL Para_Detect::DrawLine(CDC *pDc,POINT P1,POINT P2,COLORREF color) 
{ 
	CDesignView *pDesignView; 
	CDesignDoc *pDoc; 
	CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
    pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	pDoc=pDesignView->GetDocument(); 
	POINT OrgPoint=pDesignView->GetScrollPosition(); 
	//OrgPoint=GetScrollPosition(); 
	P1.x=(long)(P1.x*pDesignView->TrueRatio); 
	P1.y=(long)(P1.y*pDesignView->TrueRatio); 
    P2.x=(long)(P2.x*pDesignView->TrueRatio); 
    P2.y=(long)(P2.y*pDesignView->TrueRatio); 
    
	P1.x-=OrgPoint.x; 
	P1.y-=OrgPoint.y; 
	P2.x-=OrgPoint.x; 
	P2.y-=OrgPoint.y; 
 
	 
	CPen newPen(/*PS_DASHDOTDOT*/PS_DOT,1,color); 
	CPen *pOldPen=pDc->SelectObject(&newPen); 
    
    int OldPOR2_Mode=pDc->SetROP2(R2_XORPEN);//R2_MASKPENNOT); 
      
	pDc->MoveTo(P1); 
     pDc->LineTo(P2); 
  	 pDc->SetROP2(OldPOR2_Mode); 
     pDc->SelectObject(pOldPen); 
 
	return TRUE; 
} 
 
void Para_Detect::OnButtonBujunyunduDetail()  
{ 
  For_BuJunYunDu_Detail Detail; 
  ::MessageBeep(0); 
  Detail.DoModal(); 
  return; 
	 
} 
 
void Para_Detect::OnButtonBujunyundu()  
{ 
    CDesignView *pDesignView; 
	 CDesignDoc *pDoc; 
	 CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
      pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	  pDoc=pDesignView->GetDocument(); 
 
	  Init_BuJunYunDu_Detecting_Area(); 
 
  for(int i=0;i<4;i++) 
  { 
      pDoc->BitMap.GetRectGrayRatio(Area[i]); 
	  this->BuJunYunDu_Array[i]=pDoc->BitMap.GrayRatio[0]; 
  } 
 
  MessageBox("检测已经完成,请单击右边"详细信息"按钮以查看详细信息!","信息",MB_OK|MB_ICONINFORMATION); 
 
return;	 
} 
 
void Para_Detect::Init_BuJunYunDu_Detecting_Area() 
{ 
 CDesignView *pDesignView; 
	 CDesignDoc *pDoc; 
	 CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
      pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	  pDoc=pDesignView->GetDocument(); 
 
  int HalfWidth,HalfHeight; 
  HalfWidth=pDoc->BitMap.BmpInfoHeader.biWidth/2-1; 
  HalfHeight=pDoc->BitMap.BmpInfoHeader.biHeight/2-1; 
  Area[0].left=Area[0].top=0; 
  Area[0].right=HalfWidth; 
  Area[0].bottom=HalfHeight; 
   
  Area[1].left=Area[0].right+1; 
  Area[1].top=0; 
  Area[1].right=pDoc->BitMap.BmpInfoHeader.biWidth-1; 
  Area[1].bottom=Area[0].bottom; 
 
  Area[2].left=0; 
  Area[2].top= Area[0].bottom+1; 
  Area[2].right=Area[0].right; 
  Area[2].bottom=pDoc->BitMap.BmpInfoHeader.biHeight-1; 
  
  Area[3].left=Area[0].right+1; 
  Area[3].top= Area[0].bottom+1; 
  Area[3].right=Area[1].right; 
  Area[3].bottom=pDoc->BitMap.BmpInfoHeader.biHeight-1; 
 
 
} 
 
void Para_Detect::DrawRect(CDC *pDc, RECT TheRect, COLORREF PenColor) 
{ 
	CDesignView *pDesignView; 
	CDesignDoc *pDoc; 
	CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
    pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	pDoc=pDesignView->GetDocument(); 
	POINT OrgPoint=pDesignView->GetScrollPosition(); 
	 
	CPen newPen(/*PS_DASHDOTDOT*/PS_DOT,1,PenColor); 
	CPen *pOldPen=pDc->SelectObject(&newPen); 
     
	TheRect.left  =(long)(TheRect.left*pDesignView->TrueRatio); 
    TheRect.right =(long)(TheRect.right*pDesignView->TrueRatio); 
    TheRect.top   =(long)(TheRect.top*pDesignView->TrueRatio); 
    TheRect.bottom=(long)(TheRect.bottom*pDesignView->TrueRatio); 
    
	TheRect.left-=OrgPoint.x; 
	TheRect.top-=OrgPoint.y; 
	TheRect.right-=OrgPoint.x; 
	TheRect.bottom-=OrgPoint.y; 
	 
	/*RectTmp.left-=OrgPoint.x; 
	RectTmp.right-=OrgPoint.x; 
	RectTmp.top-=OrgPoint.y; 
	RectTmp.bottom-=OrgPoint.y;*/ 
   int OldPOR2_Mode=pDc->SetROP2(R2_XORPEN);//R2_MASKPENNOT); 
	 //pDc->Rectangle(&(pNowDoc->BitMap.SelectedRect)); 
       pDc->MoveTo(TheRect.left,TheRect.top); 
	  
	 pDc->LineTo(TheRect.left, TheRect.bottom); 
	 pDc->LineTo(TheRect.right,TheRect.bottom); 
	 pDc->LineTo(TheRect.right,TheRect.top); 
	 pDc->LineTo(TheRect.left, TheRect.top); 
  //pDc->Rectangle(RectTmp.left,RectTmp.right,RectTmp.top,RectTmp.bottom); 
     
	 pDc->SetROP2(OldPOR2_Mode); 
     pDc->SelectObject(pOldPen); 
	return ; 
} 
 
 
void Para_Detect::OnButtonLizizhijing()  
{   
	double TotalD=0; 
	RECT TheRect; 
    int i=0; 
    double Dtmp=0; 
	CDesignView *pDesignView; 
	CDesignDoc *pDoc; 
	CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); 
    pDesignView=(CDesignView*)pMFrame->GetActiveView(); 
	pDoc=pDesignView->GetDocument(); 
	ForTimeSelect SelectedTimes; 
	For_Detecting_Area_Select Select; 
    Image_Identify I_Identify; 
		pDesignView->Invalidate(FALSE); 
	if(Select.DoModal()==IDCANCEL) return; 
    this->IsDetectBegin=TRUE; 
    MSG Message; 
   switch(this->Detecting_Area) 
   { 
 case 0: 
	 if(!pDoc->BitMap.RectSelected) 
		 MessageBox("请关闭该窗口后在图中选择一粒子!","选择一个粒子",MB_OK|MB_ICONINFORMATION); 
 
	// pDesignView->Invalidate(FALSE); 
   while(1) 
   { 
	 while(!pDoc->BitMap.RectSelected) 
	 { 
		 IsDetectBegin=TRUE; 
		  if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
	 } 
   
	  I_Identify.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPData);//Init It 
      if(I_Identify.BorderDetect_For_LiZiZhiJing(&pDoc->BitMap.ReallyRectOfBMP)==1) 
	    break; 
       else 
	   { 
        MessageBox("所选区域未选中或不唯一,请重新选择","重新选择",MB_OK|MB_ICONINFORMATION); 
        pDoc->BitMap.RectSelected=FALSE; 
        pDesignView->Invalidate(FALSE); 
	   } 
     } 
     
	Dtmp=(double)AreaPoint[0]/(double)m_HaoMiDianShu; 
	Dtmp*=4.0; 
    Dtmp/=PI; 
	Dtmp=sqrt(Dtmp); 
	m_LiZiZhiJin=(float)Dtmp; 
	UpdateData(FALSE); 
    //pDoc->BitMap.RectSelected=0;   
	IsDetectBegin =FALSE; 
	 break; 
 case 2: 
       IsDetectBegin=TRUE; 
      if(!pDoc->BitMap.RectSelected) 
		 MessageBox("请关闭该窗口后在图中选择一区域!","选择区域",MB_OK|MB_ICONINFORMATION); 
 
	// pDesignView->Invalidate(FALSE);  
  while(1) 
  { 
	  while(!pDoc->BitMap.RectSelected) 
	 { 
		 IsDetectBegin=TRUE; 
		  if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
	 } 
    
    TheRect=pDoc->BitMap.ReallyRectOfBMP; 
     
     I_Identify.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPData);// Init It 
	 I_Identify.BorderDetect_For_LiZiZhiJing(&TheRect); 
	 if(!I_Identify.ObjectNum)  
	 {  
		 MessageBox("区域无对象物,重新选取!","错误",MB_OK|MB_ICONERROR);  
	 } 
     else 
		 break; 
   pDoc->BitMap.RectSelected=FALSE; 
   pDesignView->Invalidate(FALSE); 
    
  } 
    //pProg->SetRange(0,I_Identify.ObjectNum-1); 
		for(i=0;iGetDlgItem(IDC_PROGRESS1); 
          pProg->ShowWindow(SW_SHOW); 
		  */ 
	 
        /* 
		if(SelectedTimes.DoModal()==IDCANCEL) return;*/ 
         
		pDesignView->Invalidate(FALSE); 
         this->Invalidate(FALSE); 
		 
		 for(i=0;i<200;i++)// Just For Flash Message 
          if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
		   
        CWaitCursor WaitCursor; 
		 
		TheRect.left=TheRect.top=0; 
		TheRect.right=pDoc->BitMap.BmpInfoHeader.biWidth-1; 
		TheRect.bottom=pDoc->BitMap.BmpInfoHeader.biHeight-1; 
 
        I_Identify.GetNormalInfo(pDoc->BitMap.BmpInfoHeader,pDoc->BitMap.pBMPData);// Init It 
		I_Identify.BorderDetect_For_LiZiZhiJing(&TheRect); 
         //pProg->SetRange(0,I_Identify.ObjectNum-1); 
		for(i=0;iGetWindowRect(&WinRect); 
 
   
   /*void (*Fun)();//[5]; 
  Fun=OnButtonJinglidu;*/ 
  int i=0; 
 MSG Message; 
 /* 
 DWORD MESSAGE[6]={  
	               IDC_BUTTON_TIJIFENGSHU, 
                   IDC_BUTTON_JINGLIDU, 
                   IDC_BUTTON_LIZIJIANJU, 
                   IDC_BUTTON_LIZIZHIJING, 
                   IDC_BUTTON_BUJUNYUNDU 
  }; 
   */ 
	GetWindowRect(&ThisRect); 
   // pForStep->MoveWindow(0,0,WinRect.right-WinRect.left+1,WinRect.bottom-WinRect.top+1); 
    pForStep->MoveWindow(ThisRect.left,ThisRect.bottom+1,WinRect.right-WinRect.left+1,WinRect.bottom-WinRect.top+1); 
    pForStep->ShowWindow(SW_SHOW); 
   /* 
   for(i=0;i<5;i++)  
   { 
	*/ 
      IsDetectBegin=TRUE; 
	 
	  pForStep->m_Step_Text=pForStep->StepTxt[0]; 
	// pForStep->m_Step_Text="fsl;f;a"; 
	  pForStep->UpdateData(FALSE); 
	   for(i=0;i<200;i++) //Just For Send Message 
	   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
	  this->OnButtonTijifengshu(); 
 
	 
	 pForStep->m_Step_Text=pForStep->StepTxt[1]; 
	   pForStep->UpdateData(FALSE); 
	   for(i=0;i<200;i++) //Just For Send Message 
	   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
	  this->OnButtonJinglidu();	   
 
	   
	  pForStep->m_Step_Text=pForStep->StepTxt[2]; 
	   pForStep->UpdateData(FALSE); 
	   for(i=0;i<200;i++) //Just For Send Message 
	   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
		   
	   this->OnButtonLizijianju(); 
 
 
 
	   pForStep->m_Step_Text=pForStep->StepTxt[3]; 
	   pForStep->UpdateData(FALSE); 
	   for(i=0;i<200;i++) //Just For Send Message 
	   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
 
	   this->OnButtonLizizhijing(); 
 
 
 
 
	   pForStep->m_Step_Text=pForStep->StepTxt[4]; 
	   pForStep->UpdateData(FALSE); 
	   for(i=0;i<200;i++) //Just For Send Message 
	   if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
	   this->OnButtonBujunyundu(); 
       
 
		  /* 
	 SendMessage(WM_COMMAND,MESSAGE[i],0); 
	  while(IsDetectBegin) 
	  { 
        if(::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) 
		  { 
			  ::TranslateMessage(&Message); 
			  ::DispatchMessage(&Message); 
		  } 
	  } 
        */ 
/*  } */ 
	  IsDetectBegin=FALSE; 
	pForStep->ShowWindow(SW_HIDE); 
		 return; 
 
} 
 
void Para_Detect::OnMove(int x, int y)  
{ 
  //RECT ThisRect,WinRect; 
	CDialog::OnMove(x, y); 
  //GetWindowRect(&ThisRect); 
  //pForStep->GetWindowRect(&WinRect); 
  //orStep->MoveWindow(ThisRect.left,ThisRect.bottom+1,WinRect.right-WinRect.left+1,WinRect.bottom-WinRect.top+1); 
 
}