随想录(最简单的windows图形编程之Easyx)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
对于很多使用过Turbo C的同学来说,以前使用编译器进行图形编程是一件非常容易的事情。但是在win32之后,使用windows提供的SDK进行图像方面的编程就显得很麻烦,于是就有人想,是不是可以用win32 SDK仿照过去的graphics API的方法,让过去编程的方法也可以继续发挥作用。Easyx就是做了这么一件事情。
Easyx的官方网站在这里。其实它的原理也不复杂,如果你调试的时候,打开threads option就会发现,其实easyx就是利用win32 SDK封装一层graphics的API,整个应用其实是双线程的。当然,它的安装也非常简单,是我比较喜欢的那种。基本上你下载了压缩包解压之后,将头文件或者lib文件拷贝到对应的位置就可以了。
为了说明怎么使用,我这边就写了几个最简单的demo,方便大家快速入门。
1)demo程序
#include <graphics.h>
#include <conio.h>int
main(int argc, char* argv[]){initgraph(600, 800);getch();closegraph();return 0;
}
2)画直线
#include <graphics.h>
#include <conio.h>int
main(int argc, char* argv[]){initgraph(600, 800);line(100,100, 300, 300);getch();closegraph();return 0;
}
3)画圆
#include <graphics.h>
#include <conio.h>int
main(int argc, char* argv[]){initgraph(600, 800);circle(100, 100, 50);getch();closegraph();return 0;
}
4)画矩形
#include <graphics.h>
#include <conio.h>int
main(int argc, char* argv[]){initgraph(600, 800);rectangle(10, 10, 200,200);getch();closegraph();return 0;
}
5)画椭圆
#include <graphics.h>
#include <conio.h>int
main(int argc, char* argv[]){initgraph(600, 800);ellipse(100, 100, 300,200);getch();closegraph();return 0;
}
6) 更详细的api,直接查看easyx.h
/******************************************************* EasyX Library for C++ (Ver:20151015(beta))* http://www.easyx.cn** EasyX.h* 在 VC 下实现简单的绘图******************************************************/#pragma once#ifndef WINVER
#define WINVER 0x0400 // Specifies that the minimum required platform is Windows 95 and Windows NT 4.0.
#endif#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 // Specifies that the minimum required platform is Windows 2000.
#endif#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410 // Specifies that the minimum required platform is Windows 98.
#endif#if defined(UNICODE) && (_MSC_VER > 1200)#pragma comment(lib,"easyxw.lib")
#elif !defined(UNICODE) && (_MSC_VER > 1200)#pragma comment(lib,"easyx.lib")
#elif defined(UNICODE)#pragma comment(lib,"easyxw6.lib")
#elif !defined(UNICODE)#pragma comment(lib,"easyx6.lib")
#endif#ifndef __cplusplus
#error EasyX is only for C++
#endif#include <windows.h>
#include <tchar.h>// 绘图窗口初始化参数
#define SHOWCONSOLE 1 // 创建图形窗口时,保留控制台的显示
#define NOCLOSE 2 // 没有关闭功能
#define NOMINIMIZE 4 // 没有最小化功能// 颜色
#define BLACK 0
#define BLUE 0xAA0000
#define GREEN 0x00AA00
#define CYAN 0xAAAA00
#define RED 0x0000AA
#define MAGENTA 0xAA00AA
#define BROWN 0x0055AA
#define LIGHTGRAY 0xAAAAAA
#define DARKGRAY 0x555555
#define LIGHTBLUE 0xFF5555
#define LIGHTGREEN 0x55FF55
#define LIGHTCYAN 0xFFFF55
#define LIGHTRED 0x5555FF
#define LIGHTMAGENTA 0xFF55FF
#define YELLOW 0x55FFFF
#define WHITE 0xFFFFFF// 定义颜色转换宏
#define BGR(color) ( (((color) & 0xFF) << 16) | ((color) & 0xFF00FF00) | (((color) & 0xFF0000) >> 16) )class IMAGE;// 定义线的样式
class LINESTYLE
{
public:LINESTYLE();LINESTYLE(const LINESTYLE &style);LINESTYLE& operator = (const LINESTYLE &style); // 赋值运算符重载函数virtual ~LINESTYLE();DWORD style;DWORD thickness;DWORD *puserstyle;DWORD userstylecount;
};class FILLSTYLE
{
public:FILLSTYLE();FILLSTYLE(const FILLSTYLE &style);FILLSTYLE& operator = (const FILLSTYLE &style); // 赋值运算符重载函数virtual ~FILLSTYLE();int style; // 填充形式long hatch; // 填充图案样式IMAGE* ppattern; // 填充图像
};// 定义图像对象
class IMAGE
{
public:int getwidth() const; // 获取对象的宽度int getheight() const; // 获取对象的高度private:int width, height; // 对象的宽高HBITMAP m_hBmp;HDC m_hMemDC;int m_MemCurX; // 当前点X坐标int m_MemCurY; // 当前点Y坐标float m_data[6];COLORREF m_LineColor; // 当前线条颜色COLORREF m_FillColor; // 当前填充颜色COLORREF m_TextColor; // 当前文字颜色COLORREF m_BkColor; // 当前背景颜色DWORD* m_pBuffer; // 绘图区的内存LINESTYLE m_LineStyle; // 画线样式FILLSTYLE m_FillStyle; // 填充样式virtual void SetDefault(); // 设置为默认状态public:IMAGE(int _width = 0, int _height = 0); // 创建图像IMAGE(const IMAGE &img); // 拷贝构造函数IMAGE& operator = (const IMAGE &img); // 赋值运算符重载函数virtual ~IMAGE();virtual void Resize(int _width, int _height); // 调整尺寸
};// 绘图模式相关函数HWND initgraph(int width, int height, int flag = NULL); // 初始化图形环境
void closegraph(); // 关闭图形环境// 绘图环境设置void cleardevice(); // 清屏
void setcliprgn(HRGN hrgn); // 设置当前绘图设备的裁剪区
void clearcliprgn(); // 清除裁剪区的屏幕内容void getlinestyle(LINESTYLE* pstyle); // 获取当前画线样式
void setlinestyle(const LINESTYLE* pstyle); // 设置当前画线样式
void setlinestyle(int style, int thickness = 1, const DWORD *puserstyle = NULL, DWORD userstylecount = 0); // 设置当前画线样式
void getfillstyle(FILLSTYLE* pstyle); // 获取当前填充样式
void setfillstyle(const FILLSTYLE* pstyle); // 设置当前填充样式
void setfillstyle(int style, long hatch = NULL, IMAGE* ppattern = NULL); // 设置当前填充样式
void setfillstyle(BYTE* ppattern8x8); // 设置当前填充样式void setorigin(int x, int y); // 设置坐标原点
void getaspectratio(float *pxasp, float *pyasp); // 获取当前缩放因子
void setaspectratio(float xasp, float yasp); // 设置当前缩放因子int getrop2(); // 获取前景的二元光栅操作模式
void setrop2(int mode); // 设置前景的二元光栅操作模式
int getpolyfillmode(); // 获取多边形填充模式
void setpolyfillmode(int mode); // 设置多边形填充模式void graphdefaults(); // 重置所有绘图设置为默认值COLORREF getlinecolor(); // 获取当前线条颜色
void setlinecolor(COLORREF color); // 设置当前线条颜色
COLORREF gettextcolor(); // 获取当前文字颜色
void settextcolor(COLORREF color); // 设置当前文字颜色
COLORREF getfillcolor(); // 获取当前填充颜色
void setfillcolor(COLORREF color); // 设置当前填充颜色
COLORREF getbkcolor(); // 获取当前绘图背景色
void setbkcolor(COLORREF color); // 设置当前绘图背景色
int getbkmode(); // 获取背景混合模式
void setbkmode(int mode); // 设置背景混合模式// 颜色模型转换函数
COLORREF RGBtoGRAY(COLORREF rgb);
void RGBtoHSL(COLORREF rgb, float *H, float *S, float *L);
void RGBtoHSV(COLORREF rgb, float *H, float *S, float *V);
COLORREF HSLtoRGB(float H, float S, float L);
COLORREF HSVtoRGB(float H, float S, float V);// 绘图函数COLORREF getpixel(int x, int y); // 获取点的颜色
void putpixel(int x, int y, COLORREF color); // 画点void moveto(int x, int y); // 移动当前点(绝对坐标)
void moverel(int dx, int dy); // 移动当前点(相对坐标)void line(int x1, int y1, int x2, int y2); // 画线
void linerel(int dx, int dy); // 画线(至相对坐标)
void lineto(int x, int y); // 画线(至绝对坐标)void rectangle (int left, int top, int right, int bottom); // 画矩形
void fillrectangle (int left, int top, int right, int bottom); // 画填充矩形(有边框)
void solidrectangle(int left, int top, int right, int bottom); // 画填充矩形(无边框)
void clearrectangle(int left, int top, int right, int bottom); // 清空矩形区域void circle (int x, int y, int radius); // 画圆
void fillcircle (int x, int y, int radius); // 画填充圆(有边框)
void solidcircle(int x, int y, int radius); // 画填充圆(无边框)
void clearcircle(int x, int y, int radius); // 清空圆形区域void ellipse (int left, int top, int right, int bottom); // 画椭圆
void fillellipse (int left, int top, int right, int bottom); // 画填充椭圆(有边框)
void solidellipse(int left, int top, int right, int bottom); // 画填充椭圆(无边框)
void clearellipse(int left, int top, int right, int bottom); // 清空椭圆形区域void roundrect (int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight); // 画圆角矩形
void fillroundrect (int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight); // 画填充圆角矩形(有边框)
void solidroundrect(int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight); // 画填充圆角矩形(无边框)
void clearroundrect(int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight); // 清空圆角矩形区域void arc (int left, int top, int right, int bottom, double stangle, double endangle); // 画椭圆弧(起始角度和终止角度为弧度制)
void pie (int left, int top, int right, int bottom, double stangle, double endangle); // 画椭圆扇形(起始角度和终止角度为弧度制)
void fillpie (int left, int top, int right, int bottom, double stangle, double endangle); // 画填充椭圆扇形(有边框)
void solidpie(int left, int top, int right, int bottom, double stangle, double endangle); // 画填充椭圆扇形(无边框)
void clearpie(int left, int top, int right, int bottom, double stangle, double endangle); // 清空椭圆扇形区域void polyline (const POINT *points, int num); // 画多条连续的线
void polygon (const POINT *points, int num); // 画多边形
void fillpolygon (const POINT *points, int num); // 画填充的多边形(有边框)
void solidpolygon(const POINT *points, int num); // 画填充的多边形(无边框)
void clearpolygon(const POINT *points, int num); // 清空多边形区域void floodfill(int x, int y, int border); // 填充区域// 文字相关函数void outtext(LPCTSTR str); // 在当前位置输出字符串
void outtext(TCHAR c); // 在当前位置输出字符
void outtextxy(int x, int y, LPCTSTR str); // 在指定位置输出字符串
void outtextxy(int x, int y, TCHAR c); // 在指定位置输出字符
int textwidth(LPCTSTR str); // 获取字符串占用的像素宽
int textwidth(TCHAR c); // 获取字符占用的像素宽
int textheight(LPCTSTR str); // 获取字符串占用的像素高
int textheight(TCHAR c); // 获取字符占用的像素高
int drawtext(LPCTSTR str, RECT* pRect, UINT uFormat); // 在指定区域内以指定格式输出字符串
int drawtext(TCHAR c, RECT* pRect, UINT uFormat); // 在指定区域内以指定格式输出字符// 设置当前字体样式(详见帮助)
// nHeight: 字符的平均高度;
// nWidth: 字符的平均宽度(0 表示自适应);
// lpszFace: 字体名称;
// nEscapement: 字符串的书写角度(单位 0.1 度);
// nOrientation: 每个字符的书写角度(单位 0.1 度);
// nWeight: 字符的笔画粗细(0 表示默认粗细);
// bItalic: 是否斜体;
// bUnderline: 是否下划线;
// bStrikeOut: 是否删除线;
// fbCharSet: 指定字符集;
// fbOutPrecision: 指定文字的输出精度;
// fbClipPrecision: 指定文字的剪辑精度;
// fbQuality: 指定文字的输出质量;
// fbPitchAndFamily: 指定以常规方式描述字体的字体系列。
void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace);
void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut);
void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut, BYTE fbCharSet, BYTE fbOutPrecision, BYTE fbClipPrecision, BYTE fbQuality, BYTE fbPitchAndFamily);
void settextstyle(const LOGFONT *font); // 设置当前字体样式
void gettextstyle(LOGFONT *font); // 获取当前字体样式// 图像处理函数
void loadimage(IMAGE *pDstImg, LPCTSTR pImgFile, int nWidth = 0, int nHeight = 0, bool bResize = false); // 从图片文件获取图像(bmp/jpg/gif/emf/wmf)
void loadimage(IMAGE *pDstImg, LPCTSTR pResType, LPCTSTR pResName, int nWidth = 0, int nHeight = 0, bool bResize = false); // 从资源文件获取图像(bmp/jpg/gif/emf/wmf)
void saveimage(LPCTSTR pImgFile, IMAGE* pImg = NULL); // 保存图像
void getimage(IMAGE *pDstImg, int srcX, int srcY, int srcWidth, int srcHeight); // 从当前绘图设备获取图像
void putimage(int dstX, int dstY, const IMAGE *pSrcImg, DWORD dwRop = SRCCOPY); // 绘制图像到屏幕
void putimage(int dstX, int dstY, int dstWidth, int dstHeight, const IMAGE *pSrcImg, int srcX, int srcY, DWORD dwRop = SRCCOPY); // 绘制图像到屏幕(指定宽高)
void rotateimage(IMAGE *dstimg, IMAGE *srcimg, double radian, COLORREF bkcolor = BLACK, bool autosize = false, bool highquality = true);// 旋转图像
void Resize(IMAGE* pImg, int width, int height); // 调整绘图设备的大小
DWORD* GetImageBuffer(IMAGE* pImg = NULL); // 获取绘图设备的显存指针
IMAGE* GetWorkingImage(); // 获取当前绘图设备
void SetWorkingImage(IMAGE* pImg = NULL); // 设置当前绘图设备
HDC GetImageHDC(IMAGE* pImg = NULL); // 获取绘图设备句柄(HDC)// 其它函数int getwidth(); // 获取绘图区宽度
int getheight(); // 获取绘图区高度
int getx(); // 获取当前 x 坐标
int gety(); // 获取当前 y 坐标void BeginBatchDraw(); // 开始批量绘制
void FlushBatchDraw(); // 执行未完成的绘制任务
void FlushBatchDraw(int left, int top, int right, int bottom); // 执行指定区域内未完成的绘制任务
void EndBatchDraw(); // 结束批量绘制,并执行未完成的绘制任务
void EndBatchDraw(int left, int top, int right, int bottom); // 结束批量绘制,并执行指定区域内未完成的绘制任务HWND GetHWnd(); // 获取绘图窗口句柄(HWND)
TCHAR* GetEasyXVer(); // 获取 EasyX 当前版本// 获取用户输入
bool InputBox(LPTSTR pString, int nMaxCount, LPCTSTR pPrompt = NULL, LPCTSTR pTitle = NULL, LPCTSTR pDefault = NULL, int width = 0, int height = 0, bool bOnlyOK = true);// 鼠标消息
// 支持如下消息:
// WM_MOUSEMOVE 鼠标移动
// WM_MOUSEWHEEL 鼠标滚轮拨动
// WM_LBUTTONDOWN 左键按下
// WM_LBUTTONUP 左键弹起
// WM_LBUTTONDBLCLK 左键双击
// WM_MBUTTONDOWN 中键按下
// WM_MBUTTONUP 中键弹起
// WM_MBUTTONDBLCLK 中键双击
// WM_RBUTTONDOWN 右键按下
// WM_RBUTTONUP 右键弹起
// WM_RBUTTONDBLCLK 右键双击
struct MOUSEMSG
{UINT uMsg; // 当前鼠标消息bool mkCtrl; // Ctrl 键是否按下bool mkShift; // Shift 键是否按下bool mkLButton; // 鼠标左键是否按下bool mkMButton; // 鼠标中键是否按下bool mkRButton; // 鼠标右键是否按下short x; // 当前鼠标 x 坐标short y; // 当前鼠标 y 坐标short wheel; // 鼠标滚轮滚动值 (120 的倍数)
};bool MouseHit(); // 检查是否存在鼠标消息
MOUSEMSG GetMouseMsg(); // 获取一个鼠标消息。如果没有,就等待
void FlushMouseMsgBuffer(); // 清空鼠标消息缓冲区
再次感谢easyx的作者,提供了这么好的一个lib,方便了大家进行图像图像的编程。:-)
补充:
后面还看到了ege库,也不错,网址在这。
复杂一点的应用,可以选择使用duilib,用的也很多。
随想录(最简单的windows图形编程之Easyx)相关推荐
- java 网络编程简单聊天_网络编程之 TCP 实现简单聊天
网络编程之 TCP 实现简单聊天 客户端 1.连接服务器 Socket 2.发送消息 package lesson02;import java.io.IOException;import java.i ...
- 随想录(简单的windows驱动代码)
[ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 没写过windows驱动,但是看到简单的windows driver code之后,发现入门也 ...
- windows 声音编程之waveout
本教程将帮助您了解如何使用Windows waveOut接口播放数字音频.根据经验,这些接口函数掌握起来有些困难.在本教程中,我们将会建立一个Windows命令行程序来原始数字音频.注意:本教程假设您 ...
- C语言图形化编程之easyx的简单使用
easyx的简单使用 该库要使用c++ easyx库下载即文档查看请点此处. 窗口的创建 initgraph这个函数用于初始化绘图窗口. //原型 HWND initgraph(int width,i ...
- windows Socket编程之TCP服务端与客户端
在前面的文章中有一篇讲到了命名管道通信,它是创建一根管道来进行进程之间或网络之间通信的.但是它有些缺陷,比如说效率较低等.而从这篇文章开始将介绍socket编程.socket是通过TCP,UDP,IP ...
- Windows Socket编程之TCP实现大文件的传输
前言: UDP版本可参考被人博客: http://blog.csdn.net/luchengtao11/article/details/71016222 本文所述代码工程托管在Github: http ...
- Windows网络编程之Winsock 编程接口实验
实验一 Winsock 编程接口实验 1 实验类型 验证型实验 2 实验目的 1. 掌握Winsock 的启动和初始化: 2. 掌握gethostname(),gethostbyname(),GetA ...
- c/c++游戏编程之用Easyx封装按钮
c/c++游戏编程之Easyx图形库基础(一) EasyX基础 c/c++游戏编程之Easyx图形库基础(二) 绘制图片 c/c++游戏编程之Easyx图形库基础(三) 用Easyx封装按钮 文章目录 ...
- python qt5 增加文本框_Python用PyQt5制作弹出式输入框,Qt图形界面编程之QInputDialog...
前面内容,我们介绍了PyQt5的一些常用基本控件. 温故而知新 计数器QSpinBox使用详解,Python用PyQt5制作图形界面基础控件篇 Python用PyQt5实现多种弹出消息框,QMessa ...
最新文章
- 看过漫改,但你看过「改漫」吗?AI 一键让影视变漫画
- Could not find com.afollestad:material-dialogs:0.7.3.1解决
- Mac OS 安全性 任何来源
- 数据中心自动化及其优势
- 我的第一个微信好友分析
- SPL - 写着简单跑得又快的数据库语言
- ubuntu 符号连接的层数过多_Linux符号连接的层数过多解决
- 古巴比伦乘法_古巴:为生产做准备
- C++/C--在堆栈中使用数组的区别
- 12.解决SUSE Linux无法使用SSH登录的问题
- spark线性svm支持向量机 小结
- ubuntu安装最新的rails-4.2.0
- [投稿]Speex回声消除代码分析
- JAVA→异常、异常类体系结构、try→catch→finally处理异常、throw new Exception(“重构异常“);、throws Exception{ }、自定义异常
- python以写模式打开录入_Python基础06
- 编程语言之父6大经典名言,C语言之父这一段代码你见过吗?
- iOS 适配iPhone XR/XS/XS MAX
- Win10 企业版ltsc 无法访问samba网络共享问题及解决!(转)
- 高考查询系统 (完善中)
- 算法导论(三)--分治法