c++語言程式設計報告
題目使用api函式進行視覺化程式設計
學生姓名: 李光輝
班學號; 112073-33 20071001673
指導老師: 吳讓仲
中國地質大學(武漢)機械與電子資訊學院
2010 年 1月
題目1:使用api函式進行視覺化程式設計
一、問題描述
用vc的繪圖功能繪製如下圖形:
要求:1、用文字功能給圖形加標題,如「我的寵物『三毛』」,自己建立字型;
2、要求圖形是動態的,能動,如嘴巴自動的動;
3、編寫鍵盤事件,控制圖形的上下左右移動;
4、編寫滑鼠事件,控制圖形的填充顏色。
二、演算法設計
1、設計思想:本程式的設計是基於api函式進行視覺化程式設計,在教材例子的基礎上改編而成的。首先建立乙個視窗函式,再在這個視窗函式上進行適當的改編得到三毛。
2、設計表示:
利用vc提供的繪圖功能依次畫出「三毛」的各部分,具體如下:
(1) 畫頭部:呼叫畫橢圓函式 bool ellipse(hdc, int x1, int y1, int x2,int y2);
具體座標為: ellipse(hdc,45,50,105,150); //頭
(2)畫頭髮:直頭髮用函式movetoex( hdc,int x, int y, lppoint lppoint)和函式linetoex(hdc,int x,int y);彎頭髮用畫弧方法呼叫函式bool arc(hdc,int x1, int y1, int x2, int y2,int x3, int y3, int x4,int y4)
具體座標:arc(hdc,30,30,70,100,70,65,50,30); //彎頭髮
arc(hdc,80,30,120,100,100,30,80,65);
movetoex(hdc,75,32,null); // 直頭髮
lineto(hdc,75,65);
(3) 畫嘴巴,呼叫函式 bool pie(hdc,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
具體座標為:pie(hdc,60,115,90,135,60,125,90,125);//嘴巴
ellipse(hdc,60,115,90,135);
(4) 畫耳朵, 呼叫畫橢圓函式 bool ellipse(hdc, int x1, int y1, int x2,int y2);
具體座標為: ellipse(hdc,35,90,45,110);//耳朵
ellipse(hdc,105,90,115,110);
(5)脖子:呼叫函式rectangle;(hdc, int x1, int y1, int x2,int y2具體座標rectangle(hdc,65,150,85,175); //脖子
(6)畫身體, 呼叫函式 bool p olygon( hdc,lppoint lppoint,int ncount)
具體座標為: p[0].x=40;p[0].y=175;
p[1].x=30;p[1].y=195;
p[2].x=30; p[2].y=225;
p[3].x=120;p[3].y=225;
p[4].x=120; p[4].y=195;
p[5].x=110;p[5].y=175;
3、除錯報告
問題1及解決方法:繪畫多邊形時,座標陣列定義格式錯誤,致使第一次執行時,身體沒能畫出來,後來重新定義陣列,如上所示,再次執行,得到多邊形。
問題2及解決方法:解決嘴巴會動問題時,起初不知如何下手,課堂老師講解後,經過幾次改進後,如定義時間的長短等,才達到預期效果(前幾次不是定義時間太短,嘴巴動態不明顯;要不就是定義時間太長,嘴巴動的間隔時間太長,都不是很理想)。
五、附錄及程式執行結果
源程式:
#include
#include
#include
long winapi wndproc(hwnd hwnd,uint imessage,uint wparam,long lparam);
bool initwindowsclass(hinstance hinstance);
bool initwindowsclass(hinstance hinstance,int ncmdshow);
int winapi winmain( hinstance hinstance,hinstance hprevinst,lpstr lpszcmdline,int ncmdshow)
return message.wparam;
}long winapi wndproc(hwnd hwnd,uint imessage,uint wparam,long lparam)
{ hdc hdc;
hbrush hbrush;
hpen hpen;
paintstruct ptstr;
textmetric tm;
char lpsz_1="這是我的寵物'三毛'";
static int a;
static int x=0;
static int y=0;
point p[6];
p[0].x=40;
p[0].y=175;
p[1].x=30;
p[1].y=195;
p[2].x=30;
p[2].y=225;
p[3].x=120;
p[3].y=225;
p[4].x=120;
p[4].y=195;
p[5].x=110;
p[5].y=175;
settimer(hwnd,1,1000,null);
switch(imessage)
{case wm_paint:
hdc=beginpaint(hwnd,&ptstr);
settextcolor(hdc,rgb(255,0,0));
gettextmetrics(hdc,&tm);
textout(hdc,x,y,lpsz_1,strlen(lpsz_1));
setmapmode(hdc,mm_anisotropic);
setwindowextex(hdc,150,150,null);
setviewportextex(hdc,150,150,null);
setviewportorgex(hdc,x,y,null);
hpen=(hpen)getstockobject(dkgray_brush);
hbrush=(hbrush)getstockobject(white_brush);
selectobject(hdc, hbrush);
selectobject(hdc,hpen);
ellipse(hdc,45,50,105,150); //頭
ellipse(hdc,68,93,82,108);//鼻子
arc(hdc,30,30,70,100,70,65,50,30); //彎頭髮
arc(hdc,80,30,120,100,100,30,80,65);
movetoex(hdc,75,32,null); // 直頭髮
lineto(hdc,75,65);
ellipse(hdc,58,68,74,80);// 眼睛
ellipse(hdc,78,68,95,80);
hbrush=(hbrush)getstockobject(black_brush);
selectobject(hdc,hbrush);
ellipse(hdc,62,70,70,78);
ellipse(hdc,83,70,91,78);
hbrush=createsolidbrush(rgb(0,0,255)); //耳朵
selectobject(hdc,hbrush);
ellipse(hdc,35,90,45,110);
ellipse(hdc,105,90,115,110);
deleteobject(hbrush);
hbrush=createsolidbrush(rgb(255,0,255)); //脖子
selectobject(hdc,hbrush);
rectangle(hdc,65,150,85,175);
deleteobject(hbrush);
if (a==0)//身體
hbrush=createsolidbrush(rgb(0,255,255));
else
三毛經典名句
1 鎖上我的記憶,鎖上我的憂傷,不再想你,怎麼可能再想你,快樂是禁地,生死之後,找不到進去的鑰匙。2 生活,是一種緩緩如夏日流水般地前進,我們不要焦急我們三十歲的時候,不應該去急五十歲的事情,我們生的時候,不必去期望死的來臨,這一切,總會來的。3 連死的勇氣都有,何況活呢?4 生命的滋味,無論是陽春...
讀三毛有感
三毛,寫這篇文字之前,我對你的印象只有乙個有著烏黑頭髮,兩隻黑的有點憂傷的大眼睛,穿些獨特而又藝術化的服裝,寫過許多好的文字的女作家。你不喜歡被人稱為作家,這才是你的真性情,溫和與虛心。看過 夢裡花落知多少 之後,我開始理解你,甚至心疼你。你必定是在平靜之中走向你一直沉思而盼求的世界。我只想問你一句...
讀《三毛文集》有感
在沙漠,這個大鬍子男人向三毛求婚 我想的很清楚,要留你在身邊,只有跟你結婚,要不然我的心永遠不能減少這份痛楚的感覺,我們夏天結婚好嗎?我在沙漠等你。荷西和三毛的婚姻是美好而溫馨的,乙個主內,乙個主外。三毛滿足這種平淡的幸福生活,她沒有過分的要求,她只需要乙個平淡的家而已。三毛與荷西,雖然是兩個不同文...