📄 c++ builder 程序设计实用教程 - 4_2_2 利用鼠标绘制图形 - 张晋 - 17xie.htm
字号:
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>Button2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:结束绘图;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>Button1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:结束程序的运行;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>Image1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:在程序运行的过程中用于完成具体的绘图操作,并且充当绘图的容器,所有绘图的操作都是在该组件上完成的;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>RadioButton1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:决定在绘图的过程中跟踪鼠标轨迹的方式为“圆”;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>RadioButton2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:决定在绘图的过程中跟踪鼠标轨迹的方式为“矩形”;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>RadioButton3</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:决定在绘图的过程中跟踪鼠标轨迹的方式为“直线”;</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; tab-stops: list 21.25pt; mso-list: l0 level1 lfo1"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US>RadioButton4</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组件:决定在绘图的过程中跟踪鼠标轨迹的方式为“椭圆”。</SPAN></P>
<H4><SPAN lang=EN-US>2</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.响应鼠标动作</SPAN></H4>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在这里仅以鼠标的移动操作来说明在程序中如何响应鼠标的动作,其余的代码请参看附后的源程序。</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先用鼠标的左键激活组件</SPAN><SPAN
lang=EN-US>Image1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,在窗体的事件列表中选择</SPAN><SPAN
lang=EN-US>OnMouseMove</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,并且用鼠标左键双击右侧的输入框,在屏幕上就会弹出一个代码窗口,用户在其中就可以添加如下所示的响应鼠标移动的代码了:</SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">void
__fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,int X, int
Y)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">if
(start)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>{<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>if (move)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN><SPAN
style="mso-spacerun: yes"> </SPAN>if
(num==1)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->Image1->Canvas->Ellipse(X-5,Y-5,X+5,Y+5);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以圆的形式跟踪鼠标轨迹</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>else if
(num==2)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->Image1->Canvas->Rectangle(X-5,Y-5,X+5,Y+5);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以矩形的形式跟踪鼠标轨迹</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>else if
(num==3)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->Image1->Canvas->LineTo(X,Y);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以直线的形式跟踪鼠标轨迹</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>else if
(num==4)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->Image1->Canvas->Ellipse(X-5,Y-2,X+5,Y+2);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以椭圆的形式跟踪鼠标轨迹</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>}<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按照鼠标的运动轨迹绘图</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序说明:</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在程序运行的过程中,当用户在窗体上移动鼠标时,就会激活组件的</SPAN><SPAN
lang=EN-US>Image1MouseMove</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">事件,然后程序就会通过条件判断语句来判断用户在移动鼠标之前是否按下“开始绘图”按钮和鼠标的左键;</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果单击“开始绘图”后在窗体上按下并且移动鼠标,程序就会对跟踪鼠标的绘图形式作出判断,最后通过:</SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Image1->Canvas->Ellipse(X-5,Y-5,X+5,Y+5);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Image1->Canvas->Rectangle(X-5,Y-5,X+5,Y+5);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Image1->Canvas->LineTo(X,Y);<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Image1->Canvas->Ellipse(X-5,Y-2,X+5,Y+2);<o:p></o:p></SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等语句来完成绘制鼠标轨迹的操作。</SPAN></P>
<H4><SPAN lang=EN-US>3</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.设置鼠标跟踪方式</SPAN></H4>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在本程序中,是通过设置变量</SPAN><SPAN
lang=EN-US>num</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值来改变对鼠标跟踪方式的,例如在程序中如果选中“圆”选项,那么就会激活组件的</SPAN><SPAN
lang=EN-US>RadioButton1Click</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">事件,然后执行以下的程序代码:</SPAN></P>
<P class=a1><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">void
__fastcall TForm1::RadioButton1Click(TObject *Sender)<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">num=1;<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置绘制鼠标轨迹的方式为圆</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=a1><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序说明:</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过以上的程序代码,设置了变量</SPAN><SPAN
lang=EN-US>num</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值为</SPAN><SPAN
lang=EN-US>1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,在绘图的过程中通过判断</SPAN><SPAN
lang=EN-US>num</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值就可以用“圆”的形式对鼠标进行跟踪,进而完成绘图的操作。</SPAN></P>
<H4><SPAN lang=EN-US>4</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.运行程序</SPAN></H4>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">做完以上的工作后,选择菜单</SPAN><SPAN
lang=EN-US>File</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -