📄 painterview.cpp
字号:
CComboBox* pCBox = (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_FILL_PATTERN);
CRect rect;
pWnd->GetClientRect(rect);//获得对话框工具条中填充预览框的客户矩形
CDC *dc=pWnd->GetDC();
//以当前填充模式填充预览框,并设置组合框选项
CurColor = color.GetColor();
int i = pCBox->GetCurSel();
CBrush Brush;
if(i==6)
{
Brush.CreateSolidBrush(CurColor);
hatchIndex=SOLIDBRUSH;
}
else
{
hatchIndex = i;
Brush.CreateHatchBrush( hatchIndex, CurColor );
}
dc->FillRect(rect,&Brush);
}
}
//处理选择画笔颜色
void CPainterView::OnPenColor()
{
// TODO: Add your control notification handler code here
CColorDialog color;
if(color.DoModal()==IDOK)
{
CMainFrame *OwnWnd;
OwnWnd=(CMainFrame *)GetParentOwner();
//获得对话框工具条中线型预览框的指针
CWnd* pWnd = OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_PREVIEW);
//获得对话框工具条中线型和线宽组合框的指针
CComboBox* pCBox1,*pCBox2;
pCBox1= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_TYPE);
pCBox2= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_WIDTH);
//获得预览框客户区矩形
CRect rect;
pWnd->GetClientRect(rect);
CDC *dc=pWnd->GetDC();
//取出系统画笔颜色与组框选项
if(!m_bModify)
{
PenCurColor = color.GetColor();
}
else
{
m_ModifyPen->Color=color.GetColor();
}
int i = pCBox1->GetCurSel();
int j = pCBox2->GetCurSel();
//以当前画笔绘制直线填充预览框
CBrush WhiteBrush;
WhiteBrush.CreateSolidBrush( 0xffffffff );
dc->FillRect(rect,&WhiteBrush);
CPen pen(i,j+1,PenCurColor),*OldPen;
OldPen=dc->SelectObject(&pen);
dc->MoveTo(rect.left+5, (rect.bottom-rect.top)/2);
dc->LineTo(rect.right-5, (rect.bottom-rect.top)/2);
dc->SelectObject(OldPen);
}
}
//处理线宽选择
void CPainterView::OnSelchangeLineWidth()
{
// TODO: Add your control notification handler code here
CMainFrame *OwnWnd;
OwnWnd=(CMainFrame *)GetParentOwner();
//获得对话框工具条中线型预览框的指针
CWnd* pWnd = OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_PREVIEW);
//获得对话框工具条中线型和线宽组合框的指针
CComboBox *pCBox1,*pCBox2;
pCBox1= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_TYPE);
pCBox2= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_WIDTH);
//获得预览框客户区矩形
CRect rect;
pWnd->GetClientRect(rect);
CDC *dc=pWnd->GetDC();
//取出系统画笔颜色与组框选项
int i = pCBox1->GetCurSel();
int j = pCBox2->GetCurSel();
if(!m_bModify)
{
penType = i;
penWidth = j+1;
}
else
{
m_ModifyPen->iStyle=i;
m_ModifyPen->iWidth=j+1;
}
//以当前画笔绘制直线填充预览框
CBrush WhiteBrush;
WhiteBrush.CreateSolidBrush( 0xffffffff );
dc->FillRect(rect,&WhiteBrush);
CPen pen(penType,penWidth,PenCurColor);
dc->SelectObject(&pen);
dc->MoveTo(rect.left+5, (rect.bottom-rect.top)/2);
dc->LineTo(rect.right-5, (rect.bottom-rect.top)/2);
}
//处理线型选择
void CPainterView::OnSelchangeLineType()
{
// TODO: Add your control notification handler code here
CMainFrame *OwnWnd;
OwnWnd=(CMainFrame *)GetParentOwner();
CWnd* pWnd = OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_PREVIEW);
CComboBox* pCBox1,*pCBox2;
pCBox1= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_TYPE);
pCBox2= (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_LINE_WIDTH);
CRect rect;
pWnd->GetClientRect(rect);
CDC *dc=pWnd->GetDC();
int i = pCBox1->GetCurSel();
int j = pCBox2->GetCurSel();
// penType = i;
// penWidth = j+1;
if(!m_bModify)
{
penType = i;
// penCur.iStyle=penType;
penWidth = j+1;
// PenColor=PenCurColor;
}
else
{
m_ModifyPen->iStyle=i;
m_ModifyPen->iWidth=j+1;
// PenColor=m_ModifyPen->Color;
}
CBrush WhiteBrush;
WhiteBrush.CreateSolidBrush( 0xffffffff );
dc->FillRect(rect,&WhiteBrush);
CPen pen(penType,penWidth,PenCurColor);
dc->SelectObject(&pen);
dc->MoveTo(rect.left+5, (rect.bottom-rect.top)/2);
dc->LineTo(rect.right-5, (rect.bottom-rect.top)/2);
}
//处理填充模式选择
void CPainterView::OnSelchangeFillPattern()
{
// TODO: Add your control notification handler code here
CMainFrame *OwnWnd;
OwnWnd=(CMainFrame *)GetParentOwner();
CWnd* pWnd=OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_FILL_PREVIEW);
CComboBox* pCBox = (CComboBox*)OwnWnd->m_wndMyDialogBar.GetDlgItem(IDC_FILL_PATTERN);
CRect rect;
pWnd->GetClientRect(rect);
CDC *dc=pWnd->GetDC();
dc->Rectangle(rect);
hatchIndex= pCBox->GetCurSel();
CBrush Brush;
if(hatchIndex== 6)
{
Brush.CreateSolidBrush(CurColor);
hatchIndex=SOLIDBRUSH;
}
else
{
//////////////////////////////////////////////////////////
//#define HS_HORIZONTAL 0 /* ----- */
//#define HS_VERTICAL 1 /* ||||| */
//#define HS_FDIAGONAL 2 /* \\\\\ */
//#define HS_BDIAGONAL 3 /* ///// */
//#define HS_CROSS 4 /* +++++ */
//#define HS_DIAGCROSS 5 /* xxxxx */
/////////////////////////////////////////////////////////
Brush.CreateHatchBrush( hatchIndex, CurColor );
}
dc->FillRect(rect,&Brush);
}
//绘制直线命令
void CPainterView::OnDrawLine()
{
// TODO: Add your command handler code here
DrawType=new BYTE;
*DrawType=LINE;
}
//绘制矩形命令
void CPainterView::OnDrawRectangle()
{
// TODO: Add your command handler code here
DrawType=new BYTE;
*DrawType=RECTANGLE;
}
//处理鼠标释放左键
void CPainterView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPen pen(penType,penWidth,PenCurColor),*OldPen;
CPainterDoc* pDoc=GetDocument();
CDC* dc=GetDC();
OldPen=dc->SelectObject(&pen);
if(m_bModify==FALSE){
switch(*DrawType)
{
case PASTE:
case MOVE:
MoveCompent();
*DrawType=SELECT;
DrawStep=0;
break;
case RECTANGLE:
if(DrawStep==1)
{
rectanglenode->rT.right=point.x;
rectanglenode->rT.bottom=point.y;
dc->MoveTo(rectanglenode->rT.left,rectanglenode->rT.top);
dc->LineTo(rectanglenode->rT.right,rectanglenode->rT.top);
dc->LineTo(rectanglenode->rT.right,rectanglenode->rT.bottom);
dc->LineTo(rectanglenode->rT.left,rectanglenode->rT.bottom);
dc->LineTo(rectanglenode->rT.left,rectanglenode->rT.top);
rectanglenode->pen.Color=PenCurColor;
rectanglenode->pen.iWidth=penWidth;
rectanglenode->pen.iStyle=penType;
OppIndex+=1;
rectanglenode->Index=OppIndex;
pDoc->RecordList.AddTail(DrawType);
pDoc->RecordList.AddTail(rectanglenode);
DrawStep=0;
}
break;
case TRIANGLE:
if(DrawStep==1)
{
trianglenode->rT.right=point.x;
trianglenode->rT.bottom=point.y;
DrawTriangle(dc,trianglenode->rT);
trianglenode->pen.iWidth=penWidth;
trianglenode->pen.iStyle=penType;
trianglenode->pen.Color=PenCurColor;
OppIndex+=1;
trianglenode->Index=OppIndex;
pDoc->RecordList.AddTail(DrawType);
pDoc->RecordList.AddTail(trianglenode);
DrawStep=0;
}
break;
case ELLIPSE:
if(DrawStep==1)
{
ellipsenode->rT.right=point.x;
ellipsenode->rT.bottom=point.y;
Arc(dc->m_hDC,ellipsenode->rT.left,ellipsenode->rT.top,
ellipsenode->rT.right,ellipsenode->rT.bottom,0,0,0,0);
ellipsenode->pen.Color=PenCurColor;
ellipsenode->pen.iStyle=penType;
ellipsenode->pen.iWidth=penWidth;
OppIndex+=1;
ellipsenode->Index=OppIndex;
pDoc->RecordList.AddTail(DrawType);
pDoc->RecordList.AddTail(ellipsenode);
DrawStep=0;
}
break;
}}
else if(m_bModify==TRUE && m_nModifyStep==1)
{
m_nModifyStep=0;
}
Invalidate();
dc->SelectObject(OldPen);
CScrollView::OnLButtonUp(nFlags, point);
}
//处理鼠标移动
void CPainterView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC();
int iR;
//设置状态栏输出
CStatusBar* pStatus=(CStatusBar*)
AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
if(pStatus)
{
char tbuf[40];
sprintf(tbuf,"(%d,%d)",point.x,point.y);
//在状态条的第二个窗格中输出当前鼠标的位置
pStatus->SetPaneText(1,tbuf,TRUE);
pStatus->SetPaneInfo( 1, ID_SEPARATOR,SBPS_STRETCH, 10);
}
if(m_bModify==FALSE){
//如果绘图未完成则返回
if(DrawStep==0 )
return;
//否则处理
switch(*DrawType)
{
case PASTE:
case MOVE:
iR=dc->SetROP2(R2_NOTXORPEN);//设置XOR绘图方式,擦去原图
dc->MoveTo(PrePoint);
dc->LineTo(TempPoint);
TempPoint=point;
dc->MoveTo(PrePoint);
dc->LineTo(TempPoint);
dc->SetROP2(iR);
break;
case LINE:
iR=dc->SetROP2(R2_NOTXORPEN);
dc->MoveTo(linenode->Start);
dc->LineTo(PrePoint);
dc->MoveTo(linenode->Start);
dc->LineTo(point);
PrePoint=point;
dc->SetROP2(iR);
break;
case RECTANGLE:
iR=dc->SetROP2(R2_NOTXORPEN);
dc->Rectangle(&rectanglenode->rT);
rectanglenode->rT.bottom=point.y;
rectanglenode->rT.right=point.x;
dc->Rectangle(&rectanglenode->rT);
dc->SetROP2(iR);
break;
case TRIANGLE:
iR=dc->SetROP2(R2_NOTXORPEN);
DrawTriangle(dc,trianglenode->rT);
trianglenode->rT.bottom=point.y;
trianglenode->rT.right=point.x;
DrawTriangle(dc,trianglenode->rT);
dc->SetROP2(iR);
break;
case ELLIPSE:
iR=dc->SetROP2(R2_NOTXORPEN);
Arc(dc->m_hDC,ellipsenode->rT.left,ellipsenode->rT.top,
ellipsenode->rT.right,ellipsenode->rT.bottom,0,0,0,0);
ellipsenode->rT.bottom=point.y;
ellipsenode->rT.right=point.x;
Arc(dc->m_hDC,ellipsenode->rT.left,ellipsenode->rT.top,
ellipsenode->rT.right,ellipsenode->rT.bottom,0,0,0,0);
dc->SetROP2(iR);
break;
}}
else if(m_bModify==TRUE && m_nModifyStep==1)
{
float fFX,fFY;
CClientDC DC(this);
fFX=1.;
fFY=1.;
CPainterDoc *pDoc=GetDocument();
POSITION pos=pDoc->RecordList.GetHeadPosition(),TempPos;
int finish=0;
while(pos!=NULL && finish<=0)
{
BYTE *style;
TempPos=pos;
style=(BYTE *)pDoc->RecordList.GetNext(pos);
switch(*style)
{
case LINE:
LINENODE *line;
line=(LINENODE *)pDoc->RecordList.GetNext(pos);
iR=DC.SetROP2(R2_NOTXORPEN);
DrawModifyRec(&DC);
m_pModifyPoint->x=point.x/fFX;
m_pModifyPoint->y=point.y/fFY;
DrawModifyRec(&DC);
DC.SetROP2(iR);
break;
case TEXT:
TEXTNODE *text;
text=(TEXTNODE *)pDoc->RecordList.GetNext(pos);
break;
case RECTANGLE:
RECTANGLENODE *rectangle;
rectangle=(RECTANGLENODE *)pDoc->RecordList.GetNext(pos);
iR=DC.SetROP2(R2_NOTXORPEN);
DrawModifyRec(&DC);
if(m_nModifyIndex==0)
{
rectangle->rT.left=point.x/fFX;
rectangle->rT.top=point.y/fFY;
}
else if(m_nModifyIndex==1)
{
rectangle->rT.right=point.x/fFX;
rectangle->rT.top=point.y/fFY;
}
else if(m_nModifyIndex==2)
{
rectangle->rT.right=point.x/fFX;
rectangle->rT.bottom=point.y/fFY;
}
else if(m_nModifyIndex==3)
{
rectangle->rT.left=point.x/fFX;
rectangle->rT.bottom=point.y/fFY;
}
RectToPoints(rectangle->rT,*m_pModifyPoints);
DrawModifyRec(&DC);
DC.SetROP2(iR);
break;
case TRIANGLE:
TRIANGLENODE *triangle;
triangle=(TRIANGLENODE *)pDoc->RecordList.GetNext(pos);
iR=DC.SetROP2(R2_NOTXORPEN);
DrawModifyRec(&DC);
m_pModifyPoint->x=point.x/fFX;
m_pModifyPoint->y=point.y/fFY;
DrawModifyRec(&DC);
DC.SetROP2(iR);
break;
case ELLIPSE:
ELLIPSENODE *ellipse;
ellipse=(ELLIPSENODE *)pDoc->RecordList.GetNext(pos);
iR=DC.SetROP2(R2_NOTXORPEN);
DrawModifyRec(&DC);
if(m_nModifyIndex==0)
{
ellipse->rT.left=point.x/fFX;
ellipse->rT.top=point.y/fFY;
}
else if(m_nModifyIndex==1)
{
ellipse->rT.right=point.x/fFX;
ellipse->rT.top=point.y/fFY;
}
else if(m_nModifyIndex==2)
{
ellipse->rT.right=point.x/fFX;
ellipse->rT.bottom=point.y/fFY;
}
else if(m_nModifyIndex==3)
{
ellipse->rT.left=point.x/fFX;
ellipse->rT.bottom=point.y/fFY;
}
RectToPoints(ellipse->rT,*m_pModifyPoints);
DrawModifyRec(&DC);
DC.SetROP2(iR);
break;
}
}
}
CScrollView::OnMouseMove(nFlags, point);
}
//绘制椭圆命令
void CPainterView::OnDrawEllipse()
{
// TODO: Add your command handler code here
DrawType=new BYTE;
*DrawType=ELLIPSE;
}
//填充命令
void CPainterView::OnDrawFill()
{
// TODO: Add your command handler code here
DrawType=new BYTE;
*DrawType=FILL;
}
//判断图元是否被选择
void CPainterView::PickComponent(/*CDC *pDC*/)
{
CPainterDoc *pDoc=GetDocument();
BOOL IsPicked=FALSE;
POSITION pos=pDoc->RecordList.GetHeadPosition();
while(pos!=NULL)
{
BYTE *drawtype;
drawtype=(BYTE*)pDoc->RecordList.GetNext(pos);
switch(*drawtype)
{
case LINE:
LINENODE *line;
line=(LINENODE *)pDoc->RecordList.GetNext(pos);
if(line->bDo>0)//如果图元显示则继续处理
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -