www.pudn.com > JingXiang-Design-Jeff.rar > HistogramShow.cpp
// HistogramShow.cpp : implementation file
//
#include "stdafx.h"
#include "Design.h"
#include "HistogramShow.h"
#include "DesignDoc.h"
#include "MainFrm.h"
#include "DesignView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CONVER_X(x) (OrgX+1+(x))
#define CONVER_Y(y) (OrgY-1-(y))
/////////////////////////////////////////////////////////////////////////////
// HistogramShow dialog
HistogramShow::HistogramShow(CWnd* pParent /*=NULL*/)
: CDialog(HistogramShow::IDD, pParent)
{
//{{AFX_DATA_INIT(HistogramShow)
m_NumOfGray = 0;
m_CurrentGray = 0;
m_Current_x = 0;
m_Current_y = 0;
m_Gray_Inside = 0;
m_Current_Ratio = 0.0f;
//}}AFX_DATA_INIT
m_Ratio=200;
pTheParent=pParent;
mForeColor=RGB(255,0,0);
OrgX=35;
OrgY=241;
Showed_Ok=FALSE;
TotalLenth=200;
return;
}
void HistogramShow::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(HistogramShow)
DDX_Text(pDX, IDC_NUMOFGRAY, m_NumOfGray);
DDV_MinMaxInt(pDX, m_NumOfGray, 0, 256);
DDX_Text(pDX, IDC_CURRENTGRAY, m_CurrentGray);
DDV_MinMaxInt(pDX, m_CurrentGray, -1, 255);
DDX_Text(pDX, IDC_CURRENT_X, m_Current_x);
DDX_Text(pDX, IDC_CURRENT_Y, m_Current_y);
DDX_Text(pDX, IDC_CURRENT_GRAY_IN_HISTOGRAM, m_Gray_Inside);
DDV_MinMaxInt(pDX, m_Gray_Inside, -1, 255);
DDX_Text(pDX, IDC_CURRENT_GRAY_IN_HISTOGRAM_RATIO, m_Current_Ratio);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(HistogramShow, CDialog)
//{{AFX_MSG_MAP(HistogramShow)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_COLOR_BUTTON, OnColorButton)
ON_BN_CLICKED(IDC_DOWN, OnDown)
ON_BN_CLICKED(IDC_UP, OnUp)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_WM_RBUTTONUP()
ON_BN_CLICKED(IDC_BACK, OnBack)
ON_BN_CLICKED(IDC_BESTVIEW, OnBestview)
ON_BN_CLICKED(IDC_DETAIL_VIEW, OnDetailView)
ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// HistogramShow message handlers
void HistogramShow::GetParent(CWnd *pParent)
{
pTheParent=pParent;
return;
}
void HistogramShow::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
void HistogramShow::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawCoordinate(&dc);
CMainFrame *pMainFrm;
CDesignDoc *pDoc;
pMainFrm=(CMainFrame *)(AfxGetApp()->m_pMainWnd);
pDoc=(CDesignDoc *)pMainFrm->GetActiveDocument();
if(!pDoc->BitMap.GetIsGray())
{
Showed_Ok=FALSE;
EmptyNumOfGray();
pMainFrm->SetFocus();
return;
}
//if(pDoc->BitMap.RectSelected)
//SendMessage(WM_COMMAND,IDC_BESTVIEW,0);
this->m_NumOfGray=0;
CPen newPen(PS_SOLID,1,mForeColor);
CPen *pOldPen=dc.SelectObject(&newPen);
int Tmp=0;
for(int i=0;i<256;i++)
{
Tmp=(int)(pDoc->BitMap.GrayRatio[i]*m_Ratio);
if(pDoc->BitMap.GrayRatio[i]) m_NumOfGray++;
Tmp=Tmp>OrgY?OrgY:Tmp;
if(!Tmp) continue;
/* For Debug;
if(Tmp<0){CString Msg;Msg.Format("Tmp=%d",Tmp);::AfxMessageBox(Msg);}
*/
dc.MoveTo(CONVER_X(i),CONVER_Y(0));
dc.LineTo(CONVER_X(i),CONVER_Y(Tmp));
//if(CONVER_Y(Tmp)>CONVER_Y(0)) {CString Msg;Msg.Format("Tmp=%d",Tmp);::AfxMessageBox(Msg);}
}
dc.SelectObject(pOldPen);
UpdateData(FALSE);
Showed_Ok=TRUE;
pMainFrm->SetFocus();
return;
}
void HistogramShow::GetSelfClientRect()
{
this->GetClientRect(&mRect);
TotalLenth=mRect.bottom-40;
OrgY=TotalLenth+11;
}
void HistogramShow::OnColorButton()
{
CColorDialog CDlg(0,0,this);
if(CDlg.DoModal()==IDCANCEL) return;
mForeColor=CDlg.GetColor();
this->Invalidate(TRUE);
return;
}
void HistogramShow::DrawCoordinate(CDC *pDC,COLORREF Color)
{
// device context for painting
CPen newPen(PS_SOLID,1,Color);
CFont newFont;
newFont.CreateFont(10,5,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"ËÎÌå");
CFont *pOldFont=pDC->SelectObject(&newFont);
CPen *pOldPen=pDC->SelectObject(&newPen);
pDC->MoveTo(OrgX,OrgY);
pDC->LineTo(OrgX,CONVER_Y(TotalLenth+5));
pDC->MoveTo(OrgX,OrgY);
pDC->LineTo(CONVER_X(270),OrgY);
pDC->MoveTo(CONVER_X(256),OrgY+10);
pDC->LineTo(CONVER_X(256),OrgY-20);
pDC->MoveTo(OrgX-10,CONVER_Y(TotalLenth));
pDC->LineTo(OrgX+20,CONVER_Y(TotalLenth));
pDC->SetBkColor(RGB(192,192,192));
pDC->TextOut(0,CONVER_Y(TotalLenth+5),"100%");
pDC->TextOut(0,CONVER_Y(TotalLenth/2+5)," 50%");
pDC->TextOut(0,CONVER_Y(0+5)," 0%");
pDC->TextOut(CONVER_X(252),CONVER_Y(-10),"256");
//newFont.CreateFont(16,7,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"ËÎÌå");
//pDC->SelectObject(&newFont);
char NumAscii[4];
for(int i=0;i<255;i+=20)
{ pDC->MoveTo(CONVER_X(i),CONVER_Y(-1));
pDC->LineTo(CONVER_X(i),CONVER_Y(-8));
itoa(i,NumAscii,10);
pDC->TextOut(CONVER_X(i-6),CONVER_Y(-10),NumAscii);
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldFont);
return;
}
void HistogramShow::OnDown()
{
if(!Showed_Ok) return;
if(m_Ratio>100) m_Ratio-=100;
this->Invalidate(TRUE);
}
void HistogramShow::OnUp()
{
if(!Showed_Ok)return;
if(m_Ratio<100000) m_Ratio+=100;
this->Invalidate(TRUE);
}
void HistogramShow::OnClose()
{
CMainFrame *pMainFrm;
pMainFrm=(CMainFrame *)(AfxGetApp()->m_pMainWnd);
pMainFrm->SendMessage(WM_COMMAND,ID_VIEW_SHOW_HISTOGRAM,0);
return;
}
void HistogramShow::OnRButtonUp(UINT nFlags, CPoint point)
{
CMainFrame *pMainFrm;
pMainFrm=(CMainFrame *)(AfxGetApp()->m_pMainWnd);
pMainFrm->SendMessage(WM_COMMAND,ID_VIEW_SHOW_HISTOGRAM,0);
CDialog::OnRButtonUp(nFlags, point);
}
void HistogramShow::OnBack()
{
if(!Showed_Ok)return;
m_Ratio=TotalLenth;;
this->Invalidate(TRUE);
return;
}
void HistogramShow::OnBestview()
{
float Max=0;
if(!Showed_Ok)return;
CMainFrame *pMainFrm;
CDesignDoc *pDoc;
pMainFrm=(CMainFrame *)(AfxGetApp()->m_pMainWnd);
pDoc=(CDesignDoc *)pMainFrm->GetActiveDocument();
Max=pDoc->BitMap.GrayRatio[0];
for(int i=1;i<256;i++)
{
if(pDoc->BitMap.GrayRatio[i]>Max)
Max=pDoc->BitMap.GrayRatio[i];
}
m_Ratio=int (TotalLenth/Max);
this->Invalidate(TRUE);
return;
}
void HistogramShow::OnDetailView()
{
double Max=0;
if(!Showed_Ok)return;
CMainFrame *pMainFrm;
CDesignDoc *pDoc;
pMainFrm=(CMainFrame *)(AfxGetApp()->m_pMainWnd);
pDoc=(CDesignDoc *)pMainFrm->GetActiveDocument();
//Max=pDoc->BitMap.GrayRatio[0];
for(int i=0;i<256;i++)
{
if(pDoc->BitMap.GrayRatio[i]>0)
{
Max=pDoc->BitMap.GrayRatio[i];
break;
}
}
for(;i<256;i++)
{
if(pDoc->BitMap.GrayRatio[i]BitMap.GrayRatio[i]>0)
Max=pDoc->BitMap.GrayRatio[i];
}
m_Ratio=(int) (TotalLenth/(20*Max));
this->Invalidate(TRUE);
return;
}
BOOL HistogramShow::PreCreateWindow(CREATESTRUCT& cs)
{
return CDialog::PreCreateWindow(cs);
}
BOOL HistogramShow::EmptyNumOfGray()
{
this->m_NumOfGray=0;
UpdateData(FALSE);
return TRUE;
}
void HistogramShow::OnRefresh()
{
CDesignDoc *pDesignDoc;
CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
pDesignDoc=(CDesignDoc*)pMFrame->GetActiveDocument();
if(pDesignDoc->BitMap.RectSelected)
pDesignDoc->BitMap.GetRectGrayRatio(pDesignDoc->BitMap.ReallyRectOfBMP);
else
pDesignDoc->BitMap.GetGrayRatio();
OnBestview();
//this->Invalidate(TRUE); //ReFresh View
return;
}
void HistogramShow::OnMouseMove(UINT nFlags, CPoint point)
{
CDesignDoc *pDoc;
CMainFrame* pMFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
pDoc=(CDesignDoc*) pMFrame->GetActiveDocument();
if(point.xCONVER_X(255)||!(pDoc->BitMap.GetIsGray()))
{
m_Gray_Inside=-1;
m_Current_Ratio=-1;
}
else
{
m_Gray_Inside=point.x-OrgX-1;
m_Current_Ratio=pDoc->BitMap.GrayRatio[m_Gray_Inside];
}
UpdateData(FALSE);
CDialog::OnMouseMove(nFlags, point);
}