读取目录下的图片写入剪切板然后发送到qq窗口

        平时为了奖励群友,把自己私藏的好康图片发给群友,但是一张一张发太过于麻烦,于是就想着写一个脚本。用C语言写的。具体涉及到读取文件夹下的文件,剪切板操作,跟键盘事件的相关操作,控制台窗口字体颜色修改。废话不多说,源码贴上。

        控制台窗口字体修改

        

void color(short x) 
{
	if (x >= 0 && x <= 15)//参数在0-15的范围颜色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else//传入x的参数不在0到15默认颜色为白色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

        键盘事件

void Chat(char *windowText)
{
	HWND h_win= FindWindowA(NULL,windowText);//根据窗口标题获取窗口句柄
	SetForegroundWindow(h_win);//将需要发送消息的窗口置顶
	keybd_event(VK_CONTROL, 0,0,0 );//模拟键盘按下Ctrl
	keybd_event('V', 0, 0, 0);//模拟键盘按下V
	keybd_event('V', 0, 2, 0);//模拟键盘松开V
	keybd_event(VK_CONTROL, 0, 2, 0);//模拟键盘松开Ctrl
	keybd_event(VK_RETURN, 0, 0, 0);//模拟键盘按下回车
	keybd_event(VK_RETURN, 0, 2, 0);//模拟键盘松开回车
}

文件夹下的文件读取

/*
参数一:需要读取的目录
参数二: 需要发送窗口的标题(主要是在下do while语句处理文件时给指定窗口传参使用)
返回值: 一个标识,没有具体的意义
*/
int rFwCV(char *dir, char* windowText)
{
//定义两个文件结构体,里面存放的就是读取到文件夹下文件的信息,具体可以百度搜索
	struct _finddata_t fileinfo1;//文件存储信息结构体 
	struct _finddata_t fileinfo2;
//因为这边我们收藏的图片有.jpg跟.png所以我们定义两个文件句柄,处理这两种不同的文件类型,毕竟不能只发一部分吧。奖励群友就要彻底好吧。	
	long fjpgHandle=0;//保存文件句柄 
	long fpngHandle=0;
	int i = 0;//记录文件个数
	char* jpgfile = (char*)malloc(strlen(dir) + strlen((char *)"\\*.jpg")+1);//设置存放读取到的.jpg格式的文件的路径
	char* pngfile = (char*)malloc(strlen(dir) + strlen((char *)"\\*.png") + 1);//设置存放读取到的.png格式的文件的路径
	join(jpgfile, dir,(char *)"\\*.jpg");//自己写的一个字符串拼接函数,可以自己实现或者用sprintf()函数
	join(pngfile, dir, (char*)"\\*.png");
	fjpgHandle = _findfirst(jpgfile, &fileinfo1);//第一次查找指定类型的文件
	fpngHandle = _findfirst(pngfile, &fileinfo2);
	if (fjpgHandle == -1L&& fpngHandle == -1L)//返回值为-1则不存在该类型文件
	{
		printf("\t当前目录下没有.png文件或者.jpg文件\n");
		return 0;
	}
	else {
            /*
                下面是分成三种情况,,jpg类型不存在或者.png类型不存在,或者都存在的情况去处理
            */
		if (fjpgHandle != -1 && fpngHandle != -1)
		{
			do{
                /*
                    这里就可以添加处理你获得的指定文件的代码
                */

			} while ((_findnext(fjpgHandle, &fileinfo1) == 0) && (_findnext(fpngHandle, &fileinfo2) == 0));//遍历下一个类型的文件,具体可查msdn
			_findclose(fjpgHandle);//使用完句柄记得关闭
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}
		else if (fjpgHandle != -1L && fpngHandle == -1L)
		{
			do
			{
				                /*
                    这里就可以添加处理你获得的指定文件的代码
                */
			} while (_findnext(fjpgHandle, &fileinfo1) == 0 );
			_findclose(fjpgHandle);
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}
		else {
			do
			{
				                /*
                    这里就可以添加处理你获得的指定文件的代码
                */
			} while ((_findnext(fpngHandle, &fileinfo2) == 0));
			_findclose(fjpgHandle);
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}

	}

}

        将图片文件写入剪切板

/*    将图片写入剪贴板的原理大致是:先将图片转化为bitmap格式(因为剪贴板图片仅支持bitmap格式其他格式写不进去)
       这里使用 Gdiplus提供的类去进行转化.具体可以百度了解这个类,我就不过多介绍。这个是我抄其他博客的代码来源:https://stackoverflow.com/questions/39200677/how-to-copy-a-picture-from-disk-into-the-clipboard-with-win32
*/
static bool CopyFileToClipboard(const wchar_t* path)//参数一:图片的绝对路径
{
	bool result = false;
	Gdiplus::Bitmap* gdibmp = Gdiplus::Bitmap::FromFile(path);
	if (gdibmp)
	{
		HBITMAP hbitmap;
		gdibmp->GetHBITMAP(0, &hbitmap);
		if (OpenClipboard(NULL))
		{
			EmptyClipboard();//清空剪贴板
			DIBSECTION ds;
			if (GetObject(hbitmap, sizeof(DIBSECTION), &ds))
			{
				HDC hdc = GetDC(HWND_DESKTOP);
				//create compatible bitmap (get DDB from DIB)
				HBITMAP hbitmap_ddb = CreateDIBitmap(hdc, &ds.dsBmih, CBM_INIT,
					ds.dsBm.bmBits, (BITMAPINFO*)&ds.dsBmih, DIB_RGB_COLORS);
				ReleaseDC(HWND_DESKTOP, hdc);
				SetClipboardData(CF_BITMAP, hbitmap_ddb);
				DeleteObject(hbitmap_ddb);
				result = true;
			}
			CloseClipboard();
		}

		//cleanup:
		DeleteObject(hbitmap);
		delete gdibmp;
	}
	return result;
}

最后不多说了,把我完整的代码奉上,全部复制粘贴到.cpp文件中编译就可以使用,至于项目的配置,你百度就可以解决了,不然你在私聊我呗。



#include <stdio.h>
#include <Windows.h>
#include <io.h>
#include <WinUser.h>
#include <atlconv.h>
#include <GdiPlus.h>
#include <time.h>
#pragma comment(lib, "Gdiplus.lib")

void color(short x) //自定义函根据参数改变颜色 
{
	if (x >= 0 && x <= 15)//参数在0-15的范围颜色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else//默认的颜色白色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

 
void Chat(char *windowText)
{
	HWND h_win= FindWindowA(NULL,windowText);
	SetForegroundWindow(h_win);
	keybd_event(VK_CONTROL, 0,0,0 );
	keybd_event('V', 0, 0, 0);
	keybd_event('V', 0, 2, 0);
	keybd_event(VK_CONTROL, 0, 2, 0);
	keybd_event(VK_RETURN, 0, 0, 0);
	keybd_event(VK_RETURN, 0, 2, 0);
}

void join(char* str1, char* str2,char *str3=NULL)
{
	sprintf(str1, "%s", str2);
	if (str3 != NULL)
	{
		sprintf(str1, "%s%s", str2,str3);
	}
}


static bool CopyFileToClipboard(const wchar_t* path)
{
	bool result = false;
	Gdiplus::Bitmap* gdibmp = Gdiplus::Bitmap::FromFile(path);
	if (gdibmp)
	{
		HBITMAP hbitmap;
		gdibmp->GetHBITMAP(0, &hbitmap);
		if (OpenClipboard(NULL))
		{
			EmptyClipboard();
			DIBSECTION ds;
			if (GetObject(hbitmap, sizeof(DIBSECTION), &ds))
			{
				HDC hdc = GetDC(HWND_DESKTOP);
				//create compatible bitmap (get DDB from DIB)
				HBITMAP hbitmap_ddb = CreateDIBitmap(hdc, &ds.dsBmih, CBM_INIT,
					ds.dsBm.bmBits, (BITMAPINFO*)&ds.dsBmih, DIB_RGB_COLORS);
				ReleaseDC(HWND_DESKTOP, hdc);
				SetClipboardData(CF_BITMAP, hbitmap_ddb);
				DeleteObject(hbitmap_ddb);
				result = true;
			}
			CloseClipboard();
		}

		//cleanup:
		DeleteObject(hbitmap);
		delete gdibmp;
	}
	return result;
}



int rFwCV(char *dir, char* windowText)
{
	//文件存储信息结构体 
	struct _finddata_t fileinfo1;
	struct _finddata_t fileinfo2;
	//保存文件句柄 
	long fjpgHandle=0;
	long fpngHandle=0;
	int i = 0;//记录文件个数
	char* jpgfile = (char*)malloc(strlen(dir) + strlen((char *)"\\*.jpg")+1);
	char* pngfile = (char*)malloc(strlen(dir) + strlen((char *)"\\*.png") + 1);
	join(jpgfile, dir,(char *)"\\*.jpg");
	join(pngfile, dir, (char*)"\\*.png");
	fjpgHandle = _findfirst(jpgfile, &fileinfo1);
	fpngHandle = _findfirst(pngfile, &fileinfo2);
	if (fjpgHandle == -1L&& fpngHandle == -1L)
	{
		printf("\t当前目录下没有.png文件或者.jpg文件\n");
		return 0;
	}
	else {
		if (fjpgHandle != -1 && fpngHandle != -1)
		{
			do
			 {
				i++;
				char* Imgpath1 = (char*)malloc(strlen(dir) + strlen("\\") + strlen(fileinfo1.name) + 1);
				char* Imgpath2= (char*)malloc(strlen(dir) + strlen("\\") + strlen(fileinfo1.name) + 1);
				sprintf(Imgpath1, "%s%s%s", dir, "\\", fileinfo1.name);
				sprintf(Imgpath1, "%s%s%s", dir, "\\", fileinfo2.name);
				USES_CONVERSION;//A2W的宏
				Gdiplus::GdiplusStartupInput gdiplusStartupInput;
				ULONG_PTR gdiplusToken;
				Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
				CopyFileToClipboard(A2W(Imgpath1));
				CopyFileToClipboard(A2W(Imgpath2));
				Gdiplus::GdiplusShutdown(gdiplusToken);


				Chat(windowText);
				Sleep(1000);
			} while ((_findnext(fjpgHandle, &fileinfo1) == 0) && (_findnext(fpngHandle, &fileinfo2) == 0));
			_findclose(fjpgHandle);
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}
		else if (fjpgHandle != -1L && fpngHandle == -1L)
		{
			do
			{
				i++;
				char* Imgpath = (char*)malloc(strlen(dir) + strlen("\\") + strlen(fileinfo1.name) + 1);
				sprintf(Imgpath, "%s%s%s", dir, "\\", fileinfo1.name);

				USES_CONVERSION;//A2W的宏
				Gdiplus::GdiplusStartupInput gdiplusStartupInput;
				ULONG_PTR gdiplusToken;
				Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
				CopyFileToClipboard(A2W(Imgpath));
				Gdiplus::GdiplusShutdown(gdiplusToken);

				Chat(windowText);
				Sleep(1000);
			} while (_findnext(fjpgHandle, &fileinfo1) == 0 );
			_findclose(fjpgHandle);
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}
		else {
			do
			{
				i++;
				char* Imgpath = (char*)malloc(strlen(dir) + strlen("\\") + strlen(fileinfo2.name) + 1);
				sprintf(Imgpath, "%s%s%s", dir, "\\", fileinfo2.name);
				
				USES_CONVERSION;//A2W的宏
				Gdiplus::GdiplusStartupInput gdiplusStartupInput;
				ULONG_PTR gdiplusToken;
				Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
				CopyFileToClipboard(A2W(Imgpath));
				Gdiplus::GdiplusShutdown(gdiplusToken);

				Chat(windowText);
				Sleep(1000);
			} while ((_findnext(fpngHandle, &fileinfo2) == 0));
			_findclose(fjpgHandle);
			_findclose(fpngHandle);
			printf("\t共发送%d张图片\n", i);
			return 0;
		}

	}

}

void menu()
{
	system("cls");
	srand(unsigned(time(NULL)));
	int col = rand() % 16;
	color(col);
	printf("\t---------------------欢迎使用龙德学院炼金术士开发的qq图片奖励代码----------------\n");
	printf("\t请输入图片奖励代码的文件夹路径(绝对路径)\n");


}
int main()
{
	char Imgpath[100] = {0};
	char Windtext[100] = { 0 };
	while (true)
	{
		menu();
		scanf("%s", &Imgpath);
		system("pause");
		printf("\n");
		printf("需要发送到的窗口标题: ");
		scanf("%s",&Windtext);
		rFwCV(Imgpath, Windtext);
		system("pause");
	}

	return 0;
}

效果演示

                                         视频演示

 

实际演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值