编一个循环程序,用Line控件对象在屏幕上随机产生20条长度.颜色.宽的不同的直线。
展开全部
1) 首先建立单文档应用程序Elastic;
在视窗类CElasticView中添加成员变量:
private:
CPoint start,end; //纪录直线的起点和终点
int flag; //纪录鼠标左键按下的次数(用来判断是否为直线的起点)
在构造函数中设置变量初值:
CElasticView::CElasticView()
{
// TODO: add construction code here
flag=0;
}
2) 击鼠标左键输入线段起点,或输入终点并画线:
void CElasticView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC(); //取得当前绘图设备的指针
CPen newpen(PS_SOLID,0,RGB(255,0,0)); //创建新的画笔
CPen* oldpen;
if(flag==0) //第一次按下鼠标(直线起点)
{
flag=1;
start=end=point;
}else{ //第二次按下鼠标(直线的终点)
oldpen=dc->SelectObject(&newpen); //将新画笔选进设备中
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
dc->MoveTo(start);
dc->LineTo(end); //擦去图线,恢复背景图形
end=point;
dc->SetROP2(R2_COPYPEN); //设置画线模式为复制画笔
dc->MoveTo(start);
dc->LineTo(end); //画出图线
dc->SelectObject(oldpen); //恢复旧画笔
flag=0; //设置直线起点输入状态
}
ReleaseDC(dc); //释放绘图设备
CView::OnLButtonDown(nFlags, point);
}
3) 鼠标移动,绘制橡皮筋:
void CElasticView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC();
CPen newpen(PS_SOLID,0,RGB(255,0,0));
CPen* oldpen=dc->SelectObject(&newpen);
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
if(flag==1) //已输入直线起点,绘制橡皮筋
{
dc->MoveTo(start);
dc->LineTo(end);
end=point;
dc->MoveTo(start);
dc->LineTo(end);
}
dc->SetROP2(R2_COPYPEN); //设置画线模式为复制画笔
dc->SelectObject(oldpen);
ReleaseDC(dc);
CView::OnMouseMove(nFlags, point);
}
4) 击鼠标右键,取消橡皮筋:
void CElasticView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC();
CPen newpen(PS_SOLID,0,RGB(255,0,0));
CPen* oldpen=dc->SelectObject(&newpen);
if(flag==1) //已输入直线起点,取消橡皮筋
{
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
dc->MoveTo(start);
dc->LineTo(end);
dc->SetROP2(R2_COPYPEN);
flag=0;
}
dc->SelectObject(oldpen);
ReleaseDC(dc);
CView::OnRButtonDown(nFlags, point);
}
在视窗类CElasticView中添加成员变量:
private:
CPoint start,end; //纪录直线的起点和终点
int flag; //纪录鼠标左键按下的次数(用来判断是否为直线的起点)
在构造函数中设置变量初值:
CElasticView::CElasticView()
{
// TODO: add construction code here
flag=0;
}
2) 击鼠标左键输入线段起点,或输入终点并画线:
void CElasticView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC(); //取得当前绘图设备的指针
CPen newpen(PS_SOLID,0,RGB(255,0,0)); //创建新的画笔
CPen* oldpen;
if(flag==0) //第一次按下鼠标(直线起点)
{
flag=1;
start=end=point;
}else{ //第二次按下鼠标(直线的终点)
oldpen=dc->SelectObject(&newpen); //将新画笔选进设备中
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
dc->MoveTo(start);
dc->LineTo(end); //擦去图线,恢复背景图形
end=point;
dc->SetROP2(R2_COPYPEN); //设置画线模式为复制画笔
dc->MoveTo(start);
dc->LineTo(end); //画出图线
dc->SelectObject(oldpen); //恢复旧画笔
flag=0; //设置直线起点输入状态
}
ReleaseDC(dc); //释放绘图设备
CView::OnLButtonDown(nFlags, point);
}
3) 鼠标移动,绘制橡皮筋:
void CElasticView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC();
CPen newpen(PS_SOLID,0,RGB(255,0,0));
CPen* oldpen=dc->SelectObject(&newpen);
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
if(flag==1) //已输入直线起点,绘制橡皮筋
{
dc->MoveTo(start);
dc->LineTo(end);
end=point;
dc->MoveTo(start);
dc->LineTo(end);
}
dc->SetROP2(R2_COPYPEN); //设置画线模式为复制画笔
dc->SelectObject(oldpen);
ReleaseDC(dc);
CView::OnMouseMove(nFlags, point);
}
4) 击鼠标右键,取消橡皮筋:
void CElasticView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* dc=GetDC();
CPen newpen(PS_SOLID,0,RGB(255,0,0));
CPen* oldpen=dc->SelectObject(&newpen);
if(flag==1) //已输入直线起点,取消橡皮筋
{
dc->SetROP2(R2_XORPEN); //设置画线模式为异或
dc->MoveTo(start);
dc->LineTo(end);
dc->SetROP2(R2_COPYPEN);
flag=0;
}
dc->SelectObject(oldpen);
ReleaseDC(dc);
CView::OnRButtonDown(nFlags, point);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询