您的位置:首页 > 编程语言 > C语言/C++

怎样用C语言打造炫酷的图形编程

2017-08-27 16:41 267 查看
原文地址:

怎样打造炫酷的图形编程  https://www.toutiao.com/i6400951971158688258/ 

怎样打造炫酷的图形编程

C语言小白入门到大神 2017-03-24 14:42

怎么去写好看的图形编程,之前在学校总是接触到的黑窗口方面的程序,并不是很好看。先暂时给一下思路,顺便最后小编拿一个例子的代码发一下。

1 初始化图形窗口

1.1 窗口

1.1.1 初始化

HWND initgraph(

int width,

int height,

);

flag

绘图环境的样式,默认为 NULL。

NOCLOSE:禁用绘图环境的关闭按钮。

NOMINIMIZE:禁用绘图环境的最小化按钮。

SHOWCONSOLE:保留原控制台窗口。

1.1.2 刷新窗口

void cleardevice();

1.1.3 关闭窗口

void closegraph();

1.2 坐标

1.2.1 逻辑坐标(设备坐标)

void setorigin(int x, int y);

1.2.2 物理坐标(文档坐标)



加群466572167,可以交流学习
1.2.3 坐标转换

setorigin(int x, int y)

cX=XX+x;

CY=YY+y;

1.3 函数

1.3.1 格式控制函数

三原色配置颜色:COLORREF RGB(

BYTE byRed, // 颜色的红色部分

BYTE byGreen, // 颜色的绿色部分

BYTE byBlue // 颜色的蓝色部分

);

设置背景颜色:void setbkcolor(COLORREF color);

设置背景模式:void setbkmode(int mode);

mode:

OPAQUE:背景用当前背景色填充(默认)。

TRANSPARENT:背景是透明的。

设置填充颜色:void setfillcolor(COLORREF color);

设置线颜色:void setlinecolor(COLORREF color);

设置文字格式:settextstyle(int width, int height)

1.3.2 图形绘制函数

画图:void circle(

int x,

int y,

int radius

);

画填充圆:void fillcircle(

int x,

int y,

int radius

);

画线:void line(

int x1,

int y1,

int x2,

int y2

);

画填矩形void rectangle(

int left,

int top,

int right,

int bottom

);

画填充矩形void fillrectangle(

int left,

int top,

int right,

int bottom

);

1.3.3 文字输出函数

输出字符串:void outtextxy(

int x,

int y,

LPCTSTR str

);

输出数值,先将数字格式化输出为字符串

char s[5];

sprintf(s, "%d", 1024);

outtextxy(10, 60, s);

1.3.4 图像处理函数

// 从当前绘图设备获取图像

void getimage(

IMAGE* pDstImg, // 保存图像的 IMAGE 对象指针

int srcX, // 要获取图像区域左上角 x 坐标

int srcY, // 要获取图像区域的左上角 y 坐标

int srcWidth, // 要获取图像区域的宽度

int srcHeight // 要获取图像区域的高度

);

// 绘制图像

void putimage(

int dstX, // 绘制位置的 x 坐标

int dstY, // 绘制位置的 y 坐标

IMAGE *pSrcImg, // 要绘制的 IMAGE 对象指针

DWORD dwRop = SRCCOPY // 三元光栅操作码);

// 从图片文件获取图像(bmp/jpg/gif/emf/wmf/ico)

void loadimage(

IMAGE* pDstImg, // 保存图像的 IMAGE 对象指针

LPCTSTR pImgFile, // 图片文件名

int nWidth = 0, // 图片的拉伸宽度

int nHeight = 0, // 图片的拉伸高度

bool bResize = false // 是否调整 IMAGE 的大小以适应图片

);

1.3.5 鼠标处理函数

MOUSEMSG GetMouseMsg();

鼠标信息:struct MOUSEMSG

{

UINT uMsg; // 当前鼠标消息

bool mkCtrl; // Ctrl 键是否按下

bool mkShift; // Shift 键是否按下

bool mkLButton; // 鼠标左键是否按下

bool mkMButton; // 鼠标中键是否按下

bool mkRButton; // 鼠标右键是否按下

int x; // 当前鼠标 x 坐标(物理坐标)

int y; // 当前鼠标 y 坐标(物理坐标)

int wheel; // 鼠标滚轮滚动值

};

2 帮助文档导读

3 图形编程应用



加群466572167,可以交流学习
小编下面写个例子吧,就写一下坦克大战的地图哈: 加下小编的群466572167可以交流学习,群内也有资料下载学习

/*

记住几个函数就可以了

绘图---绘图窗口

*/

//编译器

#de
acfe
fine _CRT_SECURE_NO_WARNINGS

#include<graphics.h>

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

const int rows = 13;

const int cols = 13;

void InitInstance()

{

//0大鸟,1银块 , 2空地,3砖头

int mapIndex[rows][cols] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,

2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,

2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,

2, 3, 2, 3, 2, 3, 1, 3, 2, 3, 2, 3, 2,

2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,

2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,

1, 2, 3, 3, 2, 2, 2, 2, 2, 3, 3, 2, 1,

2, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2,

2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2,

2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,

2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 2,

2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2,

2, 2, 2, 2, 2, 3, 0, 3, 2, 2, 2, 2, 2 };

//文件名字格式化写入

/*

printf()

sprintf(); 可以指定输出的位置

*/

char filename[20] = "";

IMAGE map[4];

//循环把照片放进去

for (int i = 0; i < 4; i++)

{

sprintf(filename, "map%d.jpg", i);

/*

map0.jpg ---filename--map[0]

map1.jpg ---filename--map[1]

map2.jpg ---filename--map[2]

map3.jpg ---filename--map[3]

*/

loadimage(&map[i], filename);

}

int x, y;

for (int i = 0; i < rows; i++)

{

for (int j = 0; j < cols; j++)

{

x = j * 50;

y = i * 50;

putimage(x, y, &map[mapIndex[i][j]]);

/*

mapIndex[i][j],地图函数 0--3

map[i] 0-3

*/

}

}

getchar();

}

int main()

{

//初始化画布: 确定窗口的大小

initgraph(cols*50, rows*50);//图形窗口

InitInstance();

closegraph();//释放窗口

return 0;

}

最终的效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: