www.gusucode.com > VC++增强型“画图版”及图像处理程序源码程序 > VC++增强型“画图版”及图像处理程序源码程序\code\毕业设计View.cpp
//Download by http://www.NewXing.com // 毕业设计View.cpp : implementation of the CMyView class // #include "stdafx.h" #include "毕业设计.h" #include "毕业设计Doc.h" #include "毕业设计View.h" #include "SetColDlg.h" #include "SetAguDlg.h" #include "SMSetDlg.h" #include "MySetDlg.h" #include "SetDlg2.h" #include "ZoomSetDlg.h" #include "MoveSetDlg.h" #include "TurnSetDLg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern CBmpDat * pbmpdat; extern int * psaved; ///////////////////////////////////////////////////////////////////////////// // CMyView IMPLEMENT_DYNCREATE(CMyView, CFormView) BEGIN_MESSAGE_MAP(CMyView, CFormView) //{{AFX_MSG_MAP(CMyView) ON_COMMAND(ID_FILE_NEW, OnFileNew) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) ON_COMMAND(ID_FILE_SAVE, OnFileSave) ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) ON_WM_PAINT() ON_COMMAND(ID_MBY, OnMby) ON_COMMAND(ID_MFD, OnMfd) ON_COMMAND(ID_MLB, OnMlb) ON_COMMAND(ID_MMH, OnMmh) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_RBUTTONDOWN() ON_WM_RBUTTONUP() ON_COMMAND(ID_MLU, OnMlu) ON_COMMAND(ID_MLD, OnMld) ON_COMMAND(ID_MENUITEM32778, OnMenuitem32778) ON_COMMAND(ID_MENUITEM32779, OnMenuitem32779) ON_COMMAND(ID_MENUITEM32780, OnMenuitem32780) ON_COMMAND(ID_MENUITEM32781, OnMenuitem32781) ON_COMMAND(ID_MENUITEM32782, OnMenuitem32782) ON_COMMAND(ID_MENUITEM32783, OnMenuitem32783) ON_COMMAND(ID_MENUITEM32784, OnMenuitem32784) ON_COMMAND(ID_MENUITEM32785, OnMenuitem32785) ON_COMMAND(ID_MENUITEM32786, OnMenuitem32786) ON_COMMAND(ID_MENUITEM32787, OnMenuitem32787) ON_COMMAND(ID_MENUITEM32788, OnMenuitem32788) ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771) ON_COMMAND(ID_MENUITEM32790, OnMenuitem32790) //}}AFX_MSG_MAP // Standard printing commands END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyView construction/destruction CMyView::CMyView() : CFormView(CMyView::IDD) ,texttool(this) { //{{AFX_DATA_INIT(CMyView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // TODO: add construction code here saved = -1; contyp = 0; cnum = 6; lbd = FALSE; rbd = FALSE; tx[5] = 0; psaved = &saved; texttool.Create(IDD_DIALOG6); } CMyView::~CMyView(){ } void CMyView::DoDataExchange(CDataExchange* pDX){ CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyView) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs); } void CMyView::OnInitialUpdate() { CFormView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); CPaintDC dc(this); bmp = new CBmpDat(&dc); bmp->New(500,350); pbmpdat = bmp; UINT ID = IDB_B1; for(int i = 0;i<cnum;i++){ con[i].Create(i,ID+i*2,ID+i*2+1,&dc,i%2*40+10,i/2*40+10,30); } } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } void CMyView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/) { // TODO: add customized printing code here } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CFormView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnFileNew() { // TODO: Add your command handler code here // MessageBox("New"); if(saved == 1){ int i = MessageBox("文件已修改,是否保存?","提示",MB_YESNOCANCEL); if(i == 6){ bmp->Save(); } else if(i == 2){ return; } } bmp->New(bmp->GetW(),bmp->GetH()); Invalidate(TRUE); saved = -1; } void CMyView::OnFileOpen() { // TODO: Add your command handler code here // MessageBox("Open"); if(saved == 1){ int i = MessageBox("文件已修改,是否保存?","提示",MB_YESNOCANCEL); if(i == 6){ saved = bmp->Save(); } else if(i == 2){ return; } } CPaintDC pdc(this); CFileDialog ff(true,NULL,NULL,OFN_OVERWRITEPROMPT,"*.bmp|*.bmp|",NULL); if(ff.DoModal()==IDOK){ CString path = ff.GetPathName(); bmp->Open(path); saved = 0; Invalidate(TRUE); } } void CMyView::OnFileSave() { // TODO: Add your command handler code here // MessageBox("Save"); saved = bmp->Save(); } void CMyView::OnFileSaveAs() { // TODO: Add your command handler code here CFileDialog ff(false,NULL,NULL,OFN_OVERWRITEPROMPT,"*.bmp|*.bmp|",NULL); if(ff.DoModal()==IDOK){ CString path = ff.GetPathName(); StringEnd(path,".bmp"); bmp->Save(path); saved = 0; Invalidate(TRUE); } } void CMyView::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here for(int i = 0;i<cnum;i++){ dc.BitBlt(con[i].GetX(),con[i].GetY(),30,30,con[i].GetDC(contyp),0,0,SRCCOPY); } CBitmap bp,*bop; bp.LoadBitmap(IDB_PC); CDC pd; pd.CreateCompatibleDC(&dc); bop = pd.SelectObject(&bp); bop->DeleteObject(); CPen pe1,pe2; pe1.CreatePen(PS_SOLID,35,bmp->bgcol); pd.SelectObject(&pe1); pd.MoveTo(50,27); pd.LineTo(51,27); pe2.CreatePen(PS_SOLID,35,bmp->pecol); pd.SelectObject(&pe2); pd.MoveTo(25,22); pd.LineTo(26,22); dc.BitBlt(12,300,75,50,&pd,0,0,SRCCOPY); dc.MoveTo(99,0); dc.LineTo(99,1000); dc.BitBlt(100,0,bmp->GetW(),bmp->GetH(),bmp->GetDC(),0,0,SRCCOPY); // Do not call CFormView::OnPaint() for painting messages } void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { //鼠标左键盘按下时 if(rbd){//如果右键也按下的 rbd = FALSE; bmp->Cancel();//撤消所作的工作 Invalidate(FALSE); tx[5] = 0; } else if(point.x<100){ for(int i = 0;i<cnum;i++){ if(con[i].InIt(point.x,point.y)){//选择画图工具 if(contyp == 4&&i!=4){ bmp->BeiFen(); saved = 1; tx[5] = 0; } if(i == 5&&contyp!=5){//选择的是文字工具时,要打开文字工具对话框 texttool.Set(&contyp,contyp,bmp); texttool.SetXY(0,0); tx[9] = contyp;contyp = i; Invalidate(FALSE); texttool.ShowWindow(SW_RESTORE); } else if(i != 5){ if(contyp == 5){ texttool.Close(); } contyp = i; Invalidate(FALSE); } return; } } if(point.x>30&&point.x<50&&point.y>310&&point.y<330){ CColorDialog cscd(bmp->pecol);//选择前景颜色 if(cscd.DoModal() == IDOK){ bmp->SetPen(cscd.GetColor()); Invalidate(FALSE); } } if(point.x>55&&point.x<75&&point.y>320&&point.y<340){ CColorDialog cscd(bmp->bgcol);//选择背景颜色 if(cscd.DoModal() == IDOK){ bmp->bgcol = cscd.GetColor(); Invalidate(FALSE); } } } else{ if(point.x<bmp->GetW()+100&&point.y<bmp->GetH()){ //绘图区域 lbd = TRUE; if(contyp == 3){//填充 bmp->TC(point.x-100,point.y); Invalidate(FALSE); } else if(contyp == 2){//擦除 bmp->Clear(point.x-100,point.y); Invalidate(FALSE); } else if(contyp == 4){//曲线 if(tx[5]==0){//第一点(第二点在释放时记录) lbd = TRUE; tx[2] = tx[1] = tx[0] = point.x-100; ty[2] = ty[1] = ty[0] = point.y; } else if(tx[5] == 1){//第三点 tx[1] = tx[2] = point.x - 100; ty[1] = ty[2] = point.y; bmp->ReBezier(tx,ty); Invalidate(FALSE); } else{//第四点 tx[2] = point.x - 100; ty[2] = point.y; bmp->ReBezier(tx,ty); Invalidate(FALSE); } } else if(contyp == 5){//文字 if(texttool.Select(point.x-100,point.y)){ tx[0] = point.x-100; ty[0] = point.y; } else{ lbd = FALSE; } } else{ lbd = TRUE; tx[1] = tx[0] = point.x-100; ty[1] = ty[0] = point.y; } } } CFormView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(lbd){ lbd = FALSE; if(contyp==0){//画笔 bmp->Line(tx[1],ty[1],point.x-100,point.y); bmp->Darw(point.x-100,point.y); bmp->BeiFen(); saved = 1; } else if(contyp == 1){//画直线 bmp->ReLine(tx[0],ty[0],point.x-100,point.y,1,0); bmp->Darw(point.x-100,point.y); bmp->BeiFen(); saved = 1; } else if(contyp == 2){//擦除 bmp->Clear(point.x-100,point.y); bmp->BeiFen(); saved = 1; } else if(contyp == 3){//填充 bmp->BeiFen(); saved = 1; } else if(contyp == 4){//曲线 tx[5] ++; if(tx[5] == 2){ tx[2] = tx[1] = point.x - 100; ty[2] = ty[1] = point.y; } else if(tx[5] == 3){ tx[2] = point.x-100; ty[2] = point.y; tx[5] = 0; } bmp->ReBezier(tx,ty); if(tx[5]==0){ bmp->BeiFen(); saved = 1; } } else if(contyp == 5){//文字 texttool.Move(point.x-100-tx[0],point.y-ty[0]); Invalidate(FALSE); } Invalidate(FALSE); } CFormView::OnLButtonUp(nFlags, point); } void CMyView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(lbd){ if(contyp == 0){//画笔工具 bmp->Line(tx[1],ty[1],point.x-100,point.y); tx[1] = point.x-100; ty[1] = point.y; Invalidate(FALSE); } else if(contyp == 1){//画直线 bmp->ReLine(tx[0],ty[0],point.x-100,point.y,0,0); tx[1] = point.x-100; ty[1] = point.y; Invalidate(FALSE); } else if(contyp == 2){//擦除 bmp->Clear(point.x-100,point.y); tx[1] = point.x-100; ty[1] = point.y; Invalidate(FALSE); } else if(contyp == 4){//曲线 if(tx[5] == 0){ tx[3] = point.x - 100;ty [3] = point.y; } else if(tx[5] == 1){ tx[1] = tx[2] = point.x - 100; ty[1] = ty[2] = point.y; } else{ tx[2] = point.x - 100; ty[2] = point.y; } bmp->ReBezier(tx,ty); Invalidate(FALSE); } else if(contyp == 5){//文字 texttool.Move(point.x-100-tx[0],point.y-ty[0]); Invalidate(FALSE); tx[0] = point.x-100; ty[0] = point.y; } } else if(rbd){ if(contyp == 0){ bmp->Line(tx[1],ty[1],point.x-100,point.y,1); Invalidate(FALSE); tx[1] = point.x-100; ty[1] = point.y; } else if(contyp == 1){ bmp->ReLine(tx[0],ty[0],point.x-100,point.y,0,1); Invalidate(FALSE); tx[1] = point.x-100; ty[1] = point.y; } } CFormView::OnMouseMove(nFlags, point); } void CMyView::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(lbd){ if(contyp==5){ return; } lbd = FALSE; bmp->Cancel(); Invalidate(FALSE); tx[5] = 0; } else if(point.x<bmp->GetW()+100&&point.y<bmp->GetH()&&point.x>=100){ rbd = TRUE; if(contyp == 3){ bmp->TC(point.x-100,point.y,1); Invalidate(FALSE); } else if(contyp == 2){ } else{ tx[1] = tx[0] = point.x-100; ty[1] = ty[0] = point.y; } } CFormView::OnRButtonDown(nFlags, point); } void CMyView::OnRButtonUp(UINT nFlags, CPoint point){ //右键释放 if(rbd){ rbd = FALSE; if(contyp == 0){ bmp->Line(tx[1],ty[1],point.x-100,point.y,1); bmp->Darw(point.x-100,point.y,1); } else if(contyp == 1){ bmp->ReLine(tx[0],ty[0],point.x-100,point.y,1,1); bmp->Darw(point.x-100,point.y,1); } bmp->BeiFen(); Invalidate(FALSE); saved = 1; } CFormView::OnRButtonUp(nFlags, point); } void CMyView::OnMby() { // TODO: Add your command handler code here // CSetAguDlg sad; // sad.SetType(1); // if(sad.DoModal() == IDOK){ // int i[1]; // bmp->HDTX(0); // bmp->CZ(0,i,1,1,1); bmp->BY(); saved = 1; Invalidate(FALSE); // } } void CMyView::OnMfd() { // TODO: Add your command handler code here // CSetAguDlg sad; // sad.SetType(1); // if(sad.DoModal() == IDOK){ // int i[1]; // bmp->HDTX(0); // bmp->CZ(1,i,1,1,1); bmp->FD(); saved = 1; Invalidate(FALSE); // } } void CMyView::OnMlb() { CSetDlg2 sad; if(sad.DoModal() == IDOK){ int i[1]; i[0] = sad.m_n; bmp->CZ(2,i,1,1,1); saved = 1; Invalidate(FALSE); } } void CMyView::OnMmh() { // TODO: Add your command handler code here // CSetAguDlg sad; // sad.SetType(1); // if(sad.DoModal() == IDOK){ int i[1]; bmp->CZ(3,i,1,1,1); saved = 1; Invalidate(FALSE); // } } void CMyView::OnMlu() { // TODO: Add your command handler code here CSetAguDlg sad; sad.SetStr("参数为0时,亮度不变,参数越大,亮度越高。"); if(sad.DoModal() == IDOK){ int i[1]; i[0] = sad.m_n; bmp->CZ(4,i,sad.m_r,sad.m_g,sad.m_b); saved = 1; Invalidate(FALSE); } } void CMyView::OnMld() { // TODO: Add your command handler code here CSetAguDlg sad; sad.SetStr("参数为255时,亮度不变,参数越小,亮度越低。"); if(sad.DoModal() == IDOK){ int i[1]; i[0] = sad.m_n; bmp->CZ(5,i,sad.m_r,sad.m_g,sad.m_b); saved = 1; Invalidate(FALSE); } } void CMyView::OnMenuitem32778() { // TODO: Add your command handler code here bmp->DBD(); saved = 1; Invalidate(FALSE); } void CMyView::OnMenuitem32779() { // TODO: Add your command handler code here // bmp->SMXG(); bmp->NOT(); saved = 1; Invalidate(FALSE); } void CMyView::OnMenuitem32780() { // TODO: Add your command handler code here bmp->HDTX(); saved = 1; Invalidate(FALSE); } void CMyView::OnMenuitem32781() { // TODO: Add your command handler code here if(bmp->UnDoOK()){ bmp->UnDo(); saved = 1; Invalidate(); // MessageBox("Un"); } } void CMyView::OnMenuitem32782() { // TODO: Add your command handler code here if(bmp->ReDoOK()){ bmp->ReDo(); saved = 1; Invalidate(); // MessageBox("Un"); } // MessageBox("Re"); } void CMyView::OnMenuitem32783() { // TODO: Add your command handler code here if(bmp->pecol==bmp->bgcol){ MessageBox("前景色和背景色不同时进行此操作才有意义!"); return; } CSMSetDlg sms; if(sms.DoModal()==IDOK){ bmp->SMXG(sms.m_bl,sms.m_bw,sms.dx,sms.dy); saved = 1; Invalidate(FALSE); } } void CMyView::OnMenuitem32784() { // TODO: Add your command handler code here if(bmp->pecol==bmp->bgcol){ MessageBox("前景色和背景色不同时进行此操作才有意义!"); return; } CMySetDlg msd; if(msd.DoModal()==IDOK){ bmp->TEST(msd.m_m+1); saved = 1; Invalidate(FALSE); } } void CMyView::OnMenuitem32785() { // TODO: Add your command handler code here bmp->RH(); saved = 1; Invalidate(FALSE); } void CMyView::OnMenuitem32786() { //缩放 CZoomSetDlg czsd(bmp->GetW(),bmp->GetH()); if(czsd.DoModal()==IDOK){ bmp->Zoom(czsd.m_w,czsd.m_h); saved = 1; Invalidate(TRUE); } } void CMyView::OnMenuitem32787() { //平移 CMoveSetDlg czsd; if(czsd.DoModal()==IDOK){ bmp->Move(czsd.m_r,czsd.m_d); saved = 1; Invalidate(FALSE); } } void CMyView::OnMenuitem32788() { // TODO: Add your command handler code here CTurnSetDLg ctsd; if(ctsd.DoModal()==IDOK){ bmp->Turn(ctsd.m_d); saved = 1; Invalidate(); } } void CMyView::OnMenuitem32771() { // TODO: Add your command handler code here ShellExecute(NULL,"open","help.doc",NULL,NULL,0); } void CMyView::OnMenuitem32790() { // TODO: Add your command handler code here CZoomSetDlg czsd(bmp->GetW(),bmp->GetH()); if(czsd.DoModal()==IDOK){ bmp->Size(czsd.m_w,czsd.m_h); saved = 1; Invalidate(); } }