VC++画图 运行Win32时窗口会卡死
#include "stdafx.h"#include "Ele.h"
#include "EleDoc.h"
#include "EleView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CEleView
IMPLEMENT_DYNCREATE(CEleView, CScrollView)
BEGIN_MESSAGE_MAP(CEleView, CScrollView)
ON_COMMAND(ID_SELECT, &CEleView::OnSelect)
ON_UPDATE_COMMAND_UI(ID_SELECT, &CEleView::OnUpdateSelect)
ON_COMMAND(ID_FRONT, &CEleView::OnFront)
ON_COMMAND(ID_COLOR, &CEleView::OnColor)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL()
ON_WM_CONTEXTMENU()
ON_UPDATE_COMMAND_UI(ID_COLOR, &CEleView::OnUpdateColor)
ON_UPDATE_COMMAND_UI(ID_WIRE, &CEleView::OnUpdateWire)
ON_COMMAND(ID_WIRE, &CEleView::OnWire)
END_MESSAGE_MAP()
// CEleView 构造/析构
CEleView::CEleView()
: m_type(0),
m_element(NULL)
, m_ldPoint(0)
, m_mmPoint(0)
, m_luPoint(0)
{
// TODO: 在此处添加构造代码
}
CEleView::~CEleView()
{
}
BOOL CEleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CScrollView::PreCreateWindow(cs);
}
// CEleView 绘制
void CEleView::OnDraw(CDC* pDC)
{
CEleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
pDoc->m_list.Show(pDC);
if(this->m_element != NULL)
this->m_element->Draw(pDC);
ReleaseDC(pDC);
}
void CEleView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: 计算此视图的合计大小
sizeTotal.cx = sizeTotal.cy = 50000;
SetScrollSizes(MM_TEXT, sizeTotal);
}
// CEleView 诊断
#ifdef _DEBUG
void CEleView::AssertValid() const
{
CScrollView::AssertValid();
}
void CEleView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CEleDoc* CEleView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEleDoc)));
return (CEleDoc*)m_pDocument;
}
#endif //_DEBUG
// CEleView 消息处理程序
void CEleView::OnSelect()
{
// TODO: 在此添加命令处理程序代码
if(this->m_toolFlag.m_createtype != ID_SELECT)
{
this->m_toolFlag.m_createtype = ID_SELECT;
}
else
{
this->m_toolFlag.m_createtype = 0;
}
this->m_element = NULL;
this->m_list.Clear();
}
void CEleView::OnUpdateSelect(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
if(this->m_toolFlag.m_createtype == pCmdUI->m_nID)
{
pCmdUI->SetCheck();
}
else
{
pCmdUI->SetCheck(0);
}
}
void CEleView::OnFront()
{
// TODO: 在此添加命令处理程序代码
CFontDialog* dlg = new CFontDialog(NULL,CF_EFFECTS|CF_SCREENFONTS,NULL,NULL);
if(dlg->DoModal() == IDOK)
{
dlg->GetCurrentFont(& this->m_toolFlag.m_font);
}
}
void CEleView::OnColor()
{
// TODO: 在此添加命令处理程序代码
CColorDialog *dlg = new CColorDialog();
if(dlg->DoModal() == IDOK)
{
this->m_toolFlag.m_color = dlg->GetColor();
}
}
void CEleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
this->SetCapture();
this->m_ldPoint = point;
CDC *dc = this->GetDC();
if(dc == NULL)
return;
if(this->m_element != NULL)
this->m_element->Draw(dc);
this->m_element = NULL;
switch(this->m_toolFlag.m_createtype)
{
case ID_SELECT:
this->m_element = this->GetDocument()->m_list.IsSelect(point);
if(this->m_element != NULL)
{
this->m_element->HighShow(dc,1);
C_Complex *complex = static_cast<C_Complex*>(this->m_element);
complex->m_point = complex->SelectPoint(point);
if(complex->m_point != NULL)
complex->m_point->HighShow(dc,2);
}
break;
default:
switch(this->m_toolFlag.m_createtype)
{
case ID_WIRE:
do{
C_Point p1(point,this->m_toolFlag.m_color,5);
C_Line line(point,point,2,PS_SOLID,this->m_toolFlag.m_color);
this->m_element = new C_Wire(p1,p1,line);
this->m_element->Draw(dc);
}while(0);
break;
default:
break;
}
}
dc->ReleaseOutputDC();
this->ReleaseDC(dc);
//CScrollView::OnLButtonDown(nFlags, point);
}
void CEleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
this->m_luPoint = point;
CDC *dc = this->GetDC();
if(dc == NULL)
return;
switch(this->m_toolFlag.m_createtype)
{
case ID_SELECT:
if(this->m_element != NULL)
this->m_element->HighShow(dc,1);
break;
default:
switch(this->m_toolFlag.m_createtype)
{
case ID_WIRE:
if(this->m_element != NULL)
{
this->m_element->Draw(dc);
this->GetDocument()->m_list.Add(this->m_element);
delete this->m_element;
this->m_element = NULL;
}
break;
default:
break;
}
}
//C_List *m = &(this->GetDocument()->m_list);
this->ReleaseDC(dc);
ReleaseCapture();
//CScrollView::OnLButtonUp(nFlags, point);
}
void CEleView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
this->m_mmPoint = point;
CDC *dc = this->GetDC();
if(dc == NULL)
return;
switch(this->m_toolFlag.m_createtype)
{
case ID_SELECT:
if(this->m_element != NULL)
{
C_Complex *complex = static_cast<C_Complex*>(this->m_element);
complex->HighShow(dc,1);
if(complex->m_point != NULL && (nFlags & MK_LBUTTON))
{
this->InvalidateRect(&complex->m_rect,1);
if(complex->ChangePoint(point))
{
complex->HighShow(dc,1);
}
}
else
if(nFlags & MK_LBUTTON)
{
if(this->m_element->Move(this->m_mmPoint - this->m_ldPoint))
{
this->InvalidateRect(&complex->m_rect,1);
this->m_element->HighShow(dc,1);
}
this->m_ldPoint = point;
}
else
{
complex->m_point = complex->SelectPoint(point);
if(complex->m_point != NULL)
complex->m_point->HighShow(dc,2);
}
}
break;
default:
switch(this->m_toolFlag.m_createtype)
{
case ID_WIRE:
if(this->m_element != NULL &&
this->m_element->ReturnType() == ID_WIRE)
{
C_Wire *pWire = static_cast<C_Wire*>(this->m_element);
this->InvalidateRect(&pWire->m_rect);
pWire->m_point = pWire->m_endPoint;
pWire->ChangePoint(point);
pWire->m_point = NULL;
}
break;
default:
break;
}
}
dc->ReleaseOutputDC();
//CScrollView::OnMouseMove(nFlags, point);
}
BOOL CEleView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return CScrollView::OnMouseWheel(nFlags, zDelta, pt);
}
void CEleView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: 在此处添加消息处理程序代码
}
void CEleView::OnUpdateColor(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CEleView::OnUpdateWire(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
if(this->m_toolFlag.m_createtype == pCmdUI->m_nID)
{
pCmdUI->SetCheck();
}
else
{
pCmdUI->SetCheck(0);
}
}
void CEleView::OnWire()
{
// TODO: 在此添加命令处理程序代码
if(this->m_toolFlag.m_createtype != ID_WIRE)
{
this->m_toolFlag.m_createtype = ID_WIRE;
}
else
{
this->m_toolFlag.m_createtype = 0;
}
this->m_element = NULL;
this->m_list.Clear();
}