[一个好东西]一个自己写的头文件包装-C++简单绘图-draw.h-2.1

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

[一个好东西]一个自己写的头文件包装-C++简单绘图-draw.h

draw.h里面包含一堆我自制的依赖的头文件,放在最后,可能有一段时间没有更新这些头文件代码了,请去我的主页看最新的依赖文件


这是我自己平常写小程序用的绘图头文件draw.h

//基于Windows的简单绘画当前窗口的头文件draw.h
#ifndef _DRAW_
#define _DRAW_
#include<Windows.h>
#include<math.h>
#include <STDIO.H>
HDC DC=GetDC(GetForegroundWindow());
#include "button.h"
#include "image.h"
#include "strif.h"
#include "windowset.h"
//-------------------------------------------------
void StartDraw(int h,int w);//绘制场景 
void dw_dot(int x,int y,COLORREF rgb);//描点
void dw_rec(int x,int y,int w,int h,COLORREF rgb,bool dr);//矩形
void dw_line(int x1,int y1,int x2,int y2,COLORREF color);//画线
void dw_o(int x,int y,int r,COLORREF color);//圆形
//-------------------------------------------------
void StartDraw(int w,int h){
	modeset(w,h);
}
void dw_dot(int x,int y,COLORREF rgb) {
	SetPixel(DC,x,y,rgb);
	return;
}
void dw_rec(int x,int y,int w,int h,COLORREF rgb,bool dr/*表示是否实心*/) {
	if(dr==true) {
		for(int i=0; i<w; i++) {
			for(int j=0; j<h; j++) {
				SetPixel(DC,x+i,y+j,rgb);
			}
		}
	} else {
		dw_line(x,y,x+w,y,rgb);
		dw_line(x,y,x,y+h,rgb);
		dw_line(x+w,y,x+w,y+h,rgb);
		dw_line(x,y+h,x+w,y+h,rgb);
	}
	return;
}
void dw_line(int x1,int y1,int x2,int y2,COLORREF color) {
	int dx,dy,n,k;
	double xinc,yinc,x,y;
	dx = x2-x1;
	dy = y2-y1;
	if(abs(dx)-abs(dy)>0)
		n = abs(dx);
	else
		n = abs(dy);
	xinc = (double)dx/n;
	yinc = (double)dy/n;
	x = x1;
	y = y1;
	for(k = 0; k<n; k++) {
		SetPixel(DC,floor(x + 0.5),floor(y + 0.5),color);
		x+=xinc;
		y+=yinc;
	}
	return;
}
void dw_o(int x,int y,int r,COLORREF color) {
	int xc=x, yc=y;
	COLORREF c=color;
	double d=(double)1.25-r;
	x=0;
	y=r;
	SetPixel (DC,(xc+x),(yc+y),c);
	SetPixel (DC,(xc-x),(yc+y),c);
	SetPixel (DC,(xc+x),(yc-y),c);
	SetPixel (DC,(xc-x),(yc-y),c);
	SetPixel (DC,(xc+y),(yc+x),c);
	SetPixel (DC,(xc-y),(yc+x),c);
	SetPixel (DC,(xc+y),(yc-x),c);
	SetPixel (DC,(xc-y),(yc-x),c);
	while(x<=y) {
		if(d<0) d+=2*x+3;
		else {
			d+=2*(x-y)+5;
			y--;
		}
		x++;
		SetPixel (DC,(xc+x),(yc+y),c);
		SetPixel (DC,(xc-x),(yc+y),c);
		SetPixel (DC,(xc+x),(yc-y),c);
		SetPixel (DC,(xc-x),(yc-y),c);
		SetPixel (DC,(xc+y),(yc+x),c);
		SetPixel (DC,(xc-y),(yc+x),c);
		SetPixel (DC,(xc+y),(yc-x),c);
		SetPixel (DC,(xc-y),(yc-x),c);
	}
	return;
}
#endif

如果提示SetPixel外部符号问题,请去编译选项设置成这样
a
设置为-static-libgcc -lgdi32,我这里是Dev-C++ 5.9.2,使用其他应用或者其他版本的请根据情况自行修改


依赖文件

一个自己写的头文件包装-image.h-1.0

//基于当前窗口的简单显示图片函数头文件image.h
#ifndef _IMAGE_
#define _IMAGE_
#include<iostream>
#include<Windows.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"draw.h"
#include"strif.h"
using namespace std;
void imagedraw(char fileName[1000000],int x,int y,bool existerror/*如果文件不存在是否弹出错误窗口*/) {
	char *buf;                                //定义文件读取缓冲区
	char *p;
	int r,g,b,pix;
//	HWND wnd;                                 //窗口句柄
//	HDC dc;                                   //绘图设备环境句柄
	FILE *fp;                                 //定义文件指针
	FILE *fpw;                                //定义保存文件指针
	DWORD w,h;                                //定义读取图像的长和宽
	DWORD bitCorlorUsed;                      //定义
	DWORD bitSize;                            //定义图像的大小
	BITMAPFILEHEADER bf;                      //图像文件头
	BITMAPINFOHEADER bi;                      //图像文件头信息
	if(findstr(fileName,".bmp") != 1 && findstr(fileName,".BMP") != 1) {
		MessageBox(GetForegroundWindow(),"Support only BMP images!","Error",MB_OK);
		return;
	}
	if((fp=fopen(fileName,"rb"))==NULL) {
		if(existerror==true) MessageBox(GetForegroundWindow(),"file does not exist!","Error",MB_OK);
		return;
	}
	fread(&bf,sizeof(BITMAPFILEHEADER),1,fp);//读取BMP文件头文件
	fread(&bi,sizeof(BITMAPINFOHEADER),1,fp);//读取BMP文件头文件信息
	w=bi.biWidth;                            //获取图像的宽
	h=bi.biHeight;                           //获取图像的高
	bitSize=bi.biSizeImage;                  //获取图像的size
	buf=(char*)malloc(w*h*3);                //分配缓冲区大小
	fseek(fp,long(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)),0);//定位到像素起始位置
	fread(buf,1,w*h*3,fp);                   //开始读取数据
//	wnd=GetForegroundWindow();               //获取窗口句柄
//	dc=GetDC(wnd);                           //获取绘图设备
	system("cls");
	p=buf;
	for(int j=0; j<h; j++) {
		for(int i=0; i<w; i++) {
			b=*p++;
			g=*p++;
			r=*p++;
			pix=RGB(r,g,b);
			SetPixel(DC,x+i,y+h-j,pix);
		}
	}
	fclose(fp);
	return;
}
#endif

一个自己写的头文件包装-strif.h-1.0

//简单字符串判断函数 
#ifndef _STRIF_
#define _STRIF_
#include<string.h>
int findstr(char *s, char *c) {
//	判断字符串s是否包含c子串
	int i=0,j=0,flag=-1;
	while(i<strlen(s) && j<strlen(c)) {
		if(s[i]==c[j]) {
			i++;
			j++;
		} else {
			i=i-j+1;
			j=0;
		}
		if(j==strlen(c)) {
			flag=1;
			break;
		}
	}
	return flag;
}
#endif

一个自己写的头文件包装-windowset.h-1.0

//简单窗口设置函数
#ifndef _WINDOWSSET_
#define _WINDOWSSET_
#include<Windows.h>
#include<iostream>
#include <crtdefs.h>
#include <conio.h>
#include "draw.h"
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//------------
void modeset(int w,int h);	//设置窗口大小
void pause(void);			//无回显暂停
void GetPos(POINT &pt);		//获得当前控制台鼠标坐标 
void color(int a);			//修改当前控制台颜色
void gto(int x,int y);		//设置当前控制台光标到x行y列
//------------
void modeset(int w,int h) {
//	设置窗口大小为 w*h
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD size = {w, h};
	SetConsoleScreenBufferSize(hOut,size);
	SMALL_RECT rc = {1,1, w, h};
	SetConsoleWindowInfo(hOut ,true ,&rc);
	Sleep(10);
	return;
}
void pause(void) {
	std::cout<<"请按任意键继续. . .";
	getch();
	std::cout<<"\r                      \r";
	return;
}
void GetPos(POINT &pt) {
	HWND hwnd=GetForegroundWindow();
	GetCursorPos(&pt);
	ScreenToClient(hwnd,&pt);
	pt.y=pt.y/16,pt.x=pt.x/8;
}
void color(int a) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gto(int x,int y) {
	COORD pos;
	pos.X=y;
	pos.Y=x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
#endif

一个自己写的头文件包装-button.h-1.0

//c++简单按钮创建头文件
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<windows.h>
#include<algorithm>
#include"windowset.h"
using namespace std;
struct Button {
	int x,y,color;//位置x,y颜色color
	//----------------------other
	char *label;//label
	int llen;//label的长度
};
void SetButton(int x,int y,int color,Button &t,char* label) {//设置按钮,返回它
	t.x=x,t.y=y;
	t.color=color;
	t.label=label;
	t.llen=strlen(label);
}

int Preserve(Button A) {//维护一个按钮
	POINT pt;
	GetPos(pt);
	gto(A.x,A.y),color(A.color),printf("%s",A.label);
	if(pt.y==A.x&&(pt.x>=A.y&&pt.x<A.y+A.llen)) {
		color(A.color+16),gto(A.x,A.y),printf("%s",A.label);
		if(KEY_DOWN(MOUSE_MOVED)) return 1;//检测到左键
		if(KEY_DOWN(DOUBLE_CLICK)) return 2;//检测到右键
		if(KEY_DOWN(MOUSE_WHEELED))	return 3;//检测到滚轮
	}
	return 0;//没有检测到
}

猜你喜欢

转载自blog.csdn.net/yuanwow/article/details/94209033
今日推荐