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

随想录(最简单的windows图形编程之Easyx)

2016-12-04 18:06 155 查看
【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱: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,用的也很多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: