www.pudn.com > 2DCAD_duojiemian.rar > 2DCADView.cpp
// 2DCADView.cpp : implementation of the CMy2DCADView class
//
#include "stdafx.h"
#include "2DCAD.h"
#include "2DCADDoc.h"
#include "2DCADView.h"
#include "resource.h"
#include "LineWidthDlg.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_MY_MESSAGE (WM_USER+101)
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView
IMPLEMENT_DYNCREATE(CMy2DCADView, CView)
BEGIN_MESSAGE_MAP(CMy2DCADView, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CMy2DCADView)
ON_COMMAND(ID_GRAPH_POINT, OnGraphPoint)
ON_COMMAND(ID_GRAPH_LINE, OnGraphLine)
ON_UPDATE_COMMAND_UI(ID_GRAPH_LINE, OnUpdateGraphLine)
ON_UPDATE_COMMAND_UI(ID_GRAPH_POINT, OnUpdateGraphPoint)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_CHAR()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_SET_LINEWIDTH, OnSetLinewidth)
ON_COMMAND(ID_SET_COLOR, OnSetColor)
ON_COMMAND(ID_SET_FILE, OnSetFile)
ON_COMMAND(ID_THREAD_SINGLE, OnThreadSingle)
ON_COMMAND(ID_THREAD_WORKER, OnThreadWorker)
ON_COMMAND(ID_THREAD_UI, OnThreadUi)
ON_COMMAND(ID_THREAD_SUSPEND, OnThreadSuspend)
ON_COMMAND(ID_THREAD_RESUME, OnThreadResume)
ON_COMMAND(ID_THREAD_EVENT, OnThreadEvent)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView construction/destruction
CMy2DCADView::CMy2DCADView()
{
// TODO: add construction code here
m_bIsLine=FALSE;
m_bIsPoint=FALSE;
m_nStart=0;
m_nEnd=0;
m_nStep=0;
m_nLineWidth=1;
m_nLineStyle=PS_SOLID;
m_nLineColor=RGB(0,0,0);
m_pThread=NULL; //工作线程指针初始为空
m_hThread=0; //工作线程句柄初始为0
m_pUIThread=NULL;
}
CMy2DCADView::~CMy2DCADView()
{
}
BOOL CMy2DCADView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView drawing
void CMy2DCADView::OnDraw(CDC* pDC)
{
CMy2DCADDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
for(int i=0;im_nPointNum;i++)
{
int x=pDoc->m_nPoint[i][0];
int y=pDoc->m_nPoint[i][1];
pDC->Ellipse(x-2,y-2,x+2,y+2);
}
for(i=0;im_nLineNum;i++)
{
pDC->MoveTo(pDoc->m_nLine[i][0],pDoc->m_nLine[i][1]);
pDC->LineTo(pDoc->m_nLine[i][2],pDoc->m_nLine[i][3]);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView printing
BOOL CMy2DCADView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy2DCADView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy2DCADView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView diagnostics
#ifdef _DEBUG
void CMy2DCADView::AssertValid() const
{
CView::AssertValid();
}
void CMy2DCADView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy2DCADDoc* CMy2DCADView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy2DCADDoc)));
return (CMy2DCADDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView message handlers
void CMy2DCADView::OnGraphPoint()
{
// TODO: Add your command handler code here
m_bIsPoint=TRUE;
// CClientDC dc(this);
// dc.TextOut(100,100,"Point");
m_bIsLine=FALSE;
// PostMessage(WM_MY_MESSAGE);
}
void CMy2DCADView::OnGraphLine()
{
// TODO: Add your command handler code here
m_bIsLine=TRUE;
// CClientDC dc(this);
// dc.TextOut(200,100,"Line");
m_bIsPoint=FALSE;
}
void CMy2DCADView::OnUpdateGraphLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bIsLine);
}
void CMy2DCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bIsPoint);
}
void CMy2DCADView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Pop-up Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_MY2_DCADVIEW));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
}
LRESULT CMy2DCADView::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
CClientDC dc(this);
dc.TextOut(0,0,"Test");
return 1;
}
void CMy2DCADView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMy2DCADDoc* pDoc=GetDocument(); //获取文档指针
CClientDC dc(this);
CPen newpen(m_nLineStyle,m_nLineWidth,m_nLineColor); //select LineWidth
CPen *pOldPen=dc.SelectObject(&newpen);
if(m_bIsPoint)
{
dc.Ellipse(point.x-2,point.y-2,point.x+2,point.y+2);
int i=pDoc->m_nPointNum;
pDoc->m_nPoint[i][0]=point.x;
pDoc->m_nPoint[i][1]=point.y;
pDoc->m_nPointNum++;
pDoc->SetModifiedFlag(); //设置文档“脏”标记
pDoc->UpdateAllViews(this);
}
if(m_bIsLine)
{
if(m_nStep==0) //如果是线段起点,记录起点坐标
{
m_nStart=point;
m_nStep++;
SetCapture();//捕捉鼠标
}
else //如果是线段终点,则在视图区中划出线段
{
dc.MoveTo(m_nStart);
dc.LineTo(point);
//修改文档中的记录点的数据成员
int i=pDoc->m_nLineNum;
pDoc->m_nLine[i][0]=m_nStart.x;
pDoc->m_nLine[i][1]=m_nStart.y;
pDoc->m_nLine[i][2]=point.x;
pDoc->m_nLine[i][3]=point.y;
pDoc->m_nLineNum++;
pDoc->SetModifiedFlag(); //设置文档“脏”标记
pDoc->UpdateAllViews(this);
m_nStep=0;
// m_nStart=0;
::ReleaseCapture();//释放鼠标
}
}
dc.SelectObject(pOldPen);
CView::OnLButtonDown(nFlags, point);
}
BOOL CMy2DCADView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(m_bIsPoint)
{
HCURSOR hCursor;
hCursor=AfxGetApp()->LoadCursor(IDC_CURSOR1);
SetCursor(hCursor);
return TRUE;
}
if (m_bIsLine)
{
HCURSOR hCursor;
hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
SetCursor(hCursor);
return TRUE;
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CMy2DCADView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_ESCAPE)
{
m_bIsLine=FALSE;
m_bIsPoint=FALSE;
m_nStep=0;
m_nStart=0;
::ReleaseCapture();
}
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CMy2DCADView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if(m_bIsLine)
{
if(m_nStep!=0)
{
dc.SetROP2(R2_NOTXORPEN);
if(m_nEnd.x!=0)
{
dc.MoveTo(m_nStart);
dc.LineTo(m_nEnd);
}
m_nEnd=point;
dc.MoveTo(m_nStart);
dc.LineTo(m_nEnd);
}
}
CStatusBar *pStatusbar;
pStatusbar=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
CString str;
if(pStatusbar)
{
str.Format("x=%3d,y=%3d",point.x,point.y);
pStatusbar->SetPaneText(1,str);
}
CView::OnMouseMove(nFlags, point);
}
void CMy2DCADView::OnSetLinewidth()
{
// TODO: Add your command handler code here
CLineWidthDlg dlg;
dlg.m_nLineWidth=m_nLineWidth;
if(dlg.DoModal()==IDOK)
{
m_nLineWidth=dlg.m_nLineWidth;
}
}
void CMy2DCADView::OnSetColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
m_nLineColor=dlg.GetColor();
}
}
void CMy2DCADView::OnSetFile()
{
// TODO: Add your command handler code here
// char const szFilter[]="All files (*.*)|*.*|Text files(*.txt)|Word documents(*.doc)|*.doc||";
CFileDialog dlg(TRUE);
// dlg.m_ofn.lpstrFilter=&szFilter;
if(dlg.DoModal()==IDOK)
{
CString str=dlg.GetPathName();
}
}
/////////////////////////////////////////////////////////////
// 工作线程函数 //
////////////////////////////////////////////////////////////
UINT ThreadProc(LPVOID ThreadParam)
{
CDC *pDC;
CMy2DCADView *pview;
CMainFrame *pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
pview=(CMy2DCADView*)(pFrame->GetActiveView());
pDC=pview->GetDC();//获取设备环境
RECT rect;
pview->GetClientRect(&rect);
int num=0;
CBrush brush(HS_CROSS,RGB(0,255,0));
CBrush *pOldBrush=pDC->SelectObject(&brush);
do
{
num++;
pDC->Rectangle(0,rect.bottom-(num/10),100,rect.bottom);
//for(int i=0;i<300000;i++)//delay 3ms
Sleep(3);
}while(num<1000);
pDC->SelectObject(pOldBrush);
pview->ReleaseDC(pDC);
return 1;
}
void CMy2DCADView::OnThreadSingle()
{
// TODO: Add your command handler code here
ThreadProc(0);
}
void CMy2DCADView::OnThreadWorker()
{
// TODO: Add your command handler code here
//方法1:使用AfxBeginThread 一般使用此法
m_pThread=AfxBeginThread(ThreadProc,0);
/* //方法2:使用CreateThread
DWORD ThreadID; //记录线程ID
m_hThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)ThreadProc,0,0,&ThreadID);*/
}
void CMy2DCADView::OnThreadUi()
{
// TODO: Add your command handler code here
m_pUIThread=AfxBeginThread(RUNTIME_CLASS(CUIThread));
}
void CMy2DCADView::OnThreadSuspend()
{
// TODO: Add your command handler code here
if(m_pThread)
{
m_pThread->SuspendThread();//如果工作线程指针非空,将线程挂起数加1
}
if(m_pUIThread)
{
m_pUIThread->SuspendThread();//如果工作线程指针非空,将线程挂起数加1
}
}
void CMy2DCADView::OnThreadResume()
{
// TODO: Add your command handler code here
if(m_pThread)
{
m_pThread->ResumeThread();//如果工作线程指针非空,将线程挂起数减1
}
if(m_pUIThread)
{
m_pUIThread->ResumeThread();//如果工作线程指针非空,将线程挂起数减1
}
}
char MyString[]="software engineer garden!!";
///////////////////////////////////////////////
// 会发生冲突的工作线程函数 //
// 此函数将传入的字符串全部变成大写 //
//////////////////////////////////////////////
UINT MyWorkThread(LPVOID pParam)
{
CWnd *pMainWnd=AfxGetMainWnd();
if(pParam==NULL)
{
::MessageBox(pMainWnd->m_hWnd,"参数传递出错,线程结束","线程发生错误",MB_OK | MB_ICONERROR);
AfxEndThread(2); //使用AfxEndThread函数终止线程,退出码为2
}
char *pStr=(char *)pParam;
while(pStr)
{
if(*pStr>='a' && *pStr<='z')
*pStr-=32;
pStr++;
}
return 0;
}
void CMy2DCADView::OnThreadEvent()
{
// TODO: Add your command handler code here
CDC *pDC=GetDC();
pDC->TextOut(20,20,"The string before translation:");
pDC->TextOut(40,60,MyString);
char *pString=(char*)MyString; //将全局字符串作为线程函数参数
//方法1:直接调用线程函数来转换字符串
// MyWorkThread((LPVOID)pString);
//方法2:产生一个工作线程来转换字符串
AfxBeginThread(MyWorkThread,(LPVOID)pString);
pDC->TextOut(20,100,"The string after translation");
pDC->TextOut(40,140,MyString);
ReleaseDC(pDC);
}