?? zhifangtuxing.cpp
字號:
// ZhifangTuxing.cpp : implementation file
//
#include "stdafx.h"
#include "Ex07b.h"
#include "ZhifangTuxing.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZhifangTuxing
IMPLEMENT_DYNCREATE(CZhifangTuxing, CScrollView)
CZhifangTuxing::CZhifangTuxing()
{
}
CZhifangTuxing::~CZhifangTuxing()
{
}
BEGIN_MESSAGE_MAP(CZhifangTuxing, CScrollView)
//{{AFX_MSG_MAP(CZhifangTuxing)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint) //Attention here!!!!
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint) //Attention here!!!!
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview) //Attention here!!!!
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZhifangTuxing drawing
void CZhifangTuxing::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
void CZhifangTuxing::OnDraw(CDC* pDC)
{
CEx07bDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int b=350; //直方圖的下界
int w=30; //每一條直方圖的寬度
//定坐標
int Hsx=10; //橫坐標起始點的X坐標
int Hsy=b;
int Hex=w*pDoc->m_lk+100; //------終止點的-----
int Hey=Hsy;
int Zsx=50;
int Zsy=Hsy;
int Zex=Zsx;
int Zey=50;
//畫直方圖
int i=0;
int top=80; //確定顯示圖形距框架頂的距離
char str[64];
long Hz=0; //確定顯示圖形的高度
CZftfzsj *pZftfzsj;
POSITION pos=pDoc->m_zfdataList.GetHeadPosition();
//取出頻率的最最大值
while (pos!=NULL)
{
pZftfzsj=pDoc->m_zfdataList.GetNext(pos);
if (pZftfzsj->m_zfi>Hz)
Hz=pZftfzsj->m_zfi;
}
pos=pDoc->m_zfdataList.GetHeadPosition();
while (pos!=NULL)
{
CZftfzsj * pZftfzsj=pDoc->m_zfdataList.GetNext(pos);
RECT Rect;
Rect.left=Zsx+i*w;//(298-12)/6;
Rect.top=b-(pZftfzsj->m_zfi*(b-top)/Hz);
Rect.right=Rect.left+w;//(298-12)/6;
Rect.bottom=b;
CBrush Brush3(RGB((50+i*210),((i-1)*310),(150+i*125)));
CBrush *pOldBrush=pDC->SelectObject(&Brush3);
pDC->FillRect(&Rect,&Brush3);
//寫方框上的頻率
sprintf(str,"%d",pZftfzsj->m_zfi);
pDC->TextOut(Rect.left+8,Rect.top-20,str);
//寫坐標下的數據
if (i%2==0)
{
pDC->SetTextColor(RGB((50+i*210),((i-1)*310),(150+i*125)));
sprintf(str,"%6.1f",pZftfzsj->m_zmin);
pDC->TextOut(Zsx-25+i*w,b+20,str);
sprintf(str,"%6.1f",pZftfzsj->m_zmax);
pDC->TextOut(Zsx-25+(i+1)*w,b+5,str);
}
i++;
pDC->SelectObject(pOldBrush);
}
//補充坐標下的數據
// pDoc->m_lk=int(pDoc->m_lk);
// if (pDoc->m_lk%2==0)
// {
// sprintf(str,"%6.1f",pZftfzsj->m_zmax);
// pDC->TextOut(Zsx-25+i*w,b+20,str);
// }
//畫坐標
CPen pen1;
pen1.CreatePen(PS_ENDCAP_ROUND,2,RGB(255,128,128));
CPen *pOldPen=pDC->SelectObject(&pen1);
// pDC->SelectObject(&pen1);
pDC->MoveTo(Hsx,Hsy);
pDC->LineTo(Hex,Hey);
pDC->MoveTo(Zsx,Zsy);
pDC->LineTo(Zex,Zey);
//畫坐標的箭頭
pDC->MoveTo(Hex,Hey);
pDC->LineTo(Hex-10,Hey-3);
pDC->MoveTo(Hex,Hey);
pDC->LineTo(Hex-10,Hey+3);
pDC->MoveTo(Zex,Zey);
pDC->LineTo(Zex-3,Zey+10);
pDC->MoveTo(Zex,Zey);
pDC->LineTo(Zex+3,Zey+10);
pDC->SelectObject(pOldPen);
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = i*w+100;
sizeTotal.cy = b+50;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CZhifangTuxing diagnostics
#ifdef _DEBUG
void CZhifangTuxing::AssertValid() const
{
CScrollView::AssertValid();
}
void CZhifangTuxing::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CEx07bDoc * CZhifangTuxing::GetDocument()
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx07bDoc)));
return (CEx07bDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZhifangTuxing message handlers
void CZhifangTuxing::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
Invalidate(true);
}
//#ifdef _DEBUG
// CEx07bDoc * CZhifangTuxing::GetDocument()
// {
// ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx07bDoc)));
// return (CEx07bDoc*)m_pDocument;
// }
//#endif
// TODO: Add your command handler code here
/* // get default print info
CPrintInfo printInfo;
ASSERT(printInfo.m_pPD != NULL); // must be set
if (LOWORD(GetCurrentMessage()->wParam) == ID_FILE_PRINT_DIRECT)
{
CCommandLineInfo* pCmdInfo = AfxGetApp()->m_pCmdInfo;
if (pCmdInfo != NULL)
{
if (pCmdInfo->m_nShellCommand == CCommandLineInfo::FilePrintTo)
{
printInfo.m_pPD->m_pd.hDC = ::CreateDC(pCmdInfo->m_strDriverName,
pCmdInfo->m_strPrinterName, pCmdInfo->m_strPortName, NULL);
if (printInfo.m_pPD->m_pd.hDC == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
return;
}
}
}
printInfo.m_bDirect = TRUE;
}
if (OnPreparePrinting(&printInfo))
{
// hDC must be set (did you remember to call DoPreparePrinting?)
ASSERT(printInfo.m_pPD->m_pd.hDC != NULL);
// gather file to print to if print-to-file selected
CString strOutput;
if (printInfo.m_pPD->m_pd.Flags & PD_PRINTTOFILE && !printInfo.m_bDocObject)
{
// construct CFileDialog for browsing
CString strDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULTEXT));
CString strPrintDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULT));
CString strFilter(MAKEINTRESOURCE(AFX_IDS_PRINTFILTER));
CString strCaption(MAKEINTRESOURCE(AFX_IDS_PRINTCAPTION));
CFileDialog dlg(FALSE, strDef, strPrintDef,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter);
dlg.m_ofn.lpstrTitle = strCaption;
if (dlg.DoModal() != IDOK)
return;
// set output device to resulting path name
strOutput = dlg.GetPathName();
}
// set up document info and start the document printing process
CString strTitle;
CDocument* pDoc = GetDocument();
if (pDoc != NULL)
strTitle = pDoc->GetTitle();
else
GetParentFrame()->GetWindowText(strTitle);
if (strTitle.GetLength() > 31)
strTitle.ReleaseBuffer(31);
DOCINFO docInfo;
memset(&docInfo, 0, sizeof(DOCINFO));
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = strTitle;
CString strPortName;
int nFormatID;
if (strOutput.IsEmpty())
{
docInfo.lpszOutput = NULL;
strPortName = printInfo.m_pPD->GetPortName();
nFormatID = AFX_IDS_PRINTONPORT;
}
else
{
docInfo.lpszOutput = strOutput;
AfxGetFileTitle(strOutput,
strPortName.GetBuffer(_MAX_PATH), _MAX_PATH);
nFormatID = AFX_IDS_PRINTTOFILE;
}
// setup the printing DC
CDC dcPrint;
if (!printInfo.m_bDocObject)
{
dcPrint.Attach(printInfo.m_pPD->m_pd.hDC); // attach printer dc
dcPrint.m_bPrinting = TRUE;
}
OnBeginPrinting(&dcPrint, &printInfo);
if (!printInfo.m_bDocObject)
dcPrint.SetAbortProc(_AfxAbortProc);
// disable main window while printing & init printing status dialog
AfxGetMainWnd()->EnableWindow(FALSE);
CPrintingDialog dlgPrintStatus(this);
CString strTemp;
dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, strTitle);
dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME,
printInfo.m_pPD->GetDeviceName());
AfxFormatString1(strTemp, nFormatID, strPortName);
dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strTemp);
dlgPrintStatus.ShowWindow(SW_SHOW);
dlgPrintStatus.UpdateWindow();
// start document printing process
if (!printInfo.m_bDocObject && dcPrint.StartDoc(&docInfo) == SP_ERROR)
{
// enable main window before proceeding
AfxGetMainWnd()->EnableWindow(TRUE);
// cleanup and show error message
OnEndPrinting(&dcPrint, &printInfo);
dlgPrintStatus.DestroyWindow();
dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor
AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
return;
}
// Guarantee values are in the valid range
UINT nEndPage = printInfo.GetToPage();
UINT nStartPage = printInfo.GetFromPage();
if (nEndPage < printInfo.GetMinPage())
nEndPage = printInfo.GetMinPage();
if (nEndPage > printInfo.GetMaxPage())
nEndPage = printInfo.GetMaxPage();
if (nStartPage < printInfo.GetMinPage())
nStartPage = printInfo.GetMinPage();
if (nStartPage > printInfo.GetMaxPage())
nStartPage = printInfo.GetMaxPage();
int nStep = (nEndPage >= nStartPage) ? 1 : -1;
nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep;
VERIFY(strTemp.LoadString(AFX_IDS_PRINTPAGENUM));
// If it's a doc object, we don't loop page-by-page
// because doc objects don't support that kind of levity.
BOOL bError = FALSE;
if (printInfo.m_bDocObject)
{
OnPrepareDC(&dcPrint, &printInfo);
OnPrint(&dcPrint, &printInfo);
}
else
{
// begin page printing loop
for (printInfo.m_nCurPage = nStartPage;
printInfo.m_nCurPage != nEndPage; printInfo.m_nCurPage += nStep)
{
OnPrepareDC(&dcPrint, &printInfo);
// check for end of print
if (!printInfo.m_bContinuePrinting)
break;
// write current page
TCHAR szBuf[80];
wsprintf(szBuf, strTemp, printInfo.m_nCurPage);
dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf);
// set up drawing rect to entire page (in logical coordinates)
printInfo.m_rectDraw.SetRect(0, 0,
dcPrint.GetDeviceCaps(HORZRES),
dcPrint.GetDeviceCaps(VERTRES));
dcPrint.DPtoLP(&printInfo.m_rectDraw);
// attempt to start the current page
if (dcPrint.StartPage() < 0)
{
bError = TRUE;
break;
}
// must call OnPrepareDC on newer versions of Windows because
// StartPage now resets the device attributes.
if (afxData.bMarked4)
OnPrepareDC(&dcPrint, &printInfo);
ASSERT(printInfo.m_bContinuePrinting);
// page successfully started, so now render the page
OnPrint(&dcPrint, &printInfo);
if (dcPrint.EndPage() < 0 || !_AfxAbortProc(dcPrint.m_hDC, 0))
{
bError = TRUE;
break;
}
}
}
// cleanup document printing process
if (!printInfo.m_bDocObject)
{
if (!bError)
dcPrint.EndDoc();
else
dcPrint.AbortDoc();
}
AfxGetMainWnd()->EnableWindow(); // enable main window
OnEndPrinting(&dcPrint, &printInfo); // clean up after printing
dlgPrintStatus.DestroyWindow();
dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor
}
}
}
*/
void CZhifangTuxing::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::OnBeginPrinting(pDC, pInfo);
}
void CZhifangTuxing::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::OnEndPrinting(pDC, pInfo);
}
void CZhifangTuxing::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::OnPrepareDC(pDC, pInfo);
pDC->SetMapMode(MM_ANISOTROPIC);
//轉換坐標映射方式
CSize size = CSize(800, 580);
pDC->SetWindowExt(size);
//確定窗口大小
//得到實際設備每邏輯英寸的像素數量
int xLogPixPerInch=pDC->GetDeviceCaps(LOGPIXELSX);
int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);
//得到設備坐標和邏輯坐標的比例
long xExt = (long)size.cx * xLogPixPerInch/96 ;
long yExt = (long)size.cy * yLogPixPerInch/96 ;
pDC->SetViewportExt((int)xExt, (int)yExt);
//確定視口大小
}
BOOL CZhifangTuxing::OnPreparePrinting(CPrintInfo* pInfo)
{
// TODO: call DoPreparePrinting to invoke the Print dialog box
// return CScrollView::OnPreparePrinting(pInfo);
BOOL b=CView::DoPreparePrinting(pInfo);
return b;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -