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

24 篇文章 1 订阅
5 篇文章 0 订阅

[一个好东西]一个自己写的头文件包装-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;//没有检测到
}
好的,这是一个比较复杂的问题,我将分步为您解答。 首先,我们需要使用 Windows.h 头文件中的窗口类来创建一个窗口。以下是一个简单的示例代码,可以创建一个大小为 800x600 像素,标题为“贪吃蛇”的窗口: ```c++ #include <Windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Register the window class const char CLASS_NAME[] = "SnakeWindowClass"; WNDCLASS wc = { }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window HWND hwnd = CreateWindowEx( 0, // Optional window styles CLASS_NAME, // Window class "贪吃蛇", // Window text WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data ); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, nCmdShow); // Run the message loop MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } // Window procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ``` 接下来,我们需要使用 GDI 来绘制游戏界面和蛇身。以下是一个简单的示例代码,可以在窗口中绘制一个矩形: ```c++ // Window procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // Draw a rectangle RECT rect = { 100, 100, 200, 200 }; HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0)); FillRect(hdc, &rect, brush); DeleteObject(brush); EndPaint(hwnd, &ps); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ``` 最后,我们需要实现贪吃蛇的逻辑。这包括蛇的移动、食物的生成、碰撞检测等等。这部分代码比较复杂,我无法在这里完整地展示。您可以在网上搜索“C++ 贪吃蛇游戏代码”,找到很多现成的代码示例。您可以根据这些示例代码来实现您自己的贪吃蛇游戏。 希望这些代码示例能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值