[学习DOS下的图形操作]第二天 TC中像素的绘制

1.设置背景和前景颜色
void setcolor( int color );
void setbkcolor( int color );
默认的VGA调色板支持16种颜色,分别为
0 BLACK 黑
1 BLUE 蓝
2 GREEN 绿
3 CYAN 青
4 RED 红
5 MAGENTA 洋红
6 BROWN 棕
7 LIGHTGAY 浅灰
8 DARKGRAY 深灰
9 LIGHTBLUE 淡蓝
10 LIGHTGREEN淡绿
11 LIGHTCYAN 淡青
12 LIGHTRED 淡红
13 LIGHTMAGENTA 淡洋红
14 YELLOW 黄
15 WHITE 白
练习1:
设置背景颜色为青色(测试一下,不翻书,把代码完整写下来)
#include <stdio.h>
#include <graphics.h>
int main(void)
{
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";
initgraph( &graph_driver, &graph_mode, graph_path );
setbkcolor( CYAN );//或者使用setbkcolor(3);也可以
getch();
closegraph();
return 0;
}
练习2:
随机每隔一秒钟自动更换背景颜色,直到用户按键为止。
需要用到的函数有:
kbhit()如果返回0,表示没有键按下
srand()设置随机数的初始值
rand()返回一个随机数
time(NULL)返回当前的时间,整数的形式
sleep(1)睡眠
#include <stdio.h>
#include <graphics.h>
#include <dos.h>//sleep
#include <stdlib.h>//srand,rand
#include <time.h>//time
#include <conio.h>//kbhit
int main(void)
{
int bg_color = 0;
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";
initgraph( &graph_driver, &graph_mode, graph_path );
srand( time(NULL) );
while( kbhit()==0 )
{

bg_color = rand( ) % 16 ;//0-15
setbkcolor( bg_color );
sleep(1);
}
return 0;
}
二、设置或提取屏幕上某一个像素点的颜色
void putpixel( int x, int y, int color );
把(x,y)点处的像素设置为color指定的颜色。
int getpixel( int x, int y );
获取(x,y)点处的颜色值。
练习:
随机画点程序,随机画不同颜色,不同为止的点.
#include <stdio.h>
#include <stdlib.h> // srand, rand
#include <time.h> // time
#include <conio.h> // kbhit
#include <graphics.h>
#include <dos.h>

int main(void)
{
int x, y , color;

int i,num;
int graph_driver = VGA;
int graph_mode = VGAHI;
char *graph_path = "D:\\DOS\\TC\\BGI";

initgraph( &graph_driver, &graph_mode, graph_path );
srand( time(NULL) );
setbkcolor( BLACK );
num = 3000;
while( kbhit() == 0 )
{
cleardevice();//清空屏幕
for( i = 0; i < num; i++ )
{
x = rand()%640;
y = rand()%480;
color = rand()%16+0;
putpixel( x, y, color );
}
sleep(0.1);
}
closegraph();
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微型16/32位嵌入式GUI,自主开发,历时四年左右,拥有全部版权. http://www.ecgui.com 体积小巧 大小在 100KB~180KB! 特性: 多窗口支持| 完全文输入/显示| 多数常用GUI控件| 99.9% ANSI C 编| 已成功移植 DOS,Linux,uc/OS-II| 组件式 API(易用,高效) GUI SDK-Green for DOS Beta 开发包下载 305 KB 火热下载 http://ecurb2006.googlepages.com/sdk-green-4dos.zip 演示程序下载 264 KB http://ecurb2006.googlepages.com/demo.zip 微型嵌入式GUI编程指南 PDF 下载 349 KB http://ecurb2006.googlepages.com/embeddedguiprogrammingguide.pdf GUI SDK For Windows Beta 开发包下载 583 KB http://ecurb2006.googlepages.com/sdk4win.zip 包含详细使用说明 部分应用程序 截图 记事本 http://ecurb2006.googlepages.com/edit.jpg 计算器 http://ecurb2006.googlepages.com/calc.jpg 画笔 http://ecurb2006.googlepages.com/painter.zip 支持控件 MAINWINDOW 主窗口 DLGWINDOW 对话框窗口,从属于主窗 BUTTON 按钮 MAINMENU 下拉式菜单 POPMENU 弹出式菜单 HSCROLLBAR 横向滚动条 VSCROLLBAR 竖向滚动条 TEXTBOX 单行文本编辑框 EDITBOX 多行文本编辑框 COMBOBOX 组合框 LISTBOX 列表框 CHECKBOX 多项选择框 CHOICEBOX 单项选择框 LABEL 文字标签 PAGEVIEW 标签页 SPEEDBAR 进度条 商业使用,将提供 GUI 全部源代码,低价授权策略,并提供 GUI 移植技术支持! 请联系 sales@ecurb2006.com 更多信息请访问 http://www.ecurb2006.com 针对 不少网友开发免费 DOS 软件的想法,现在提供 DOS 下的开发库,个人或者组织可以开发公共使用的免费软件,借这个机会,也希望能更好的推广这个 GUI.会在程序显示区域头部显示 GUI 的介绍性信息(无其他无关信息)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值