概述
void TestManualFormatClipboard()
{
//自定义数据结构体
typedef struct _MYSTRUCT
{
CHAR charVal[16];
int iVal;
long lVal;
double fVal;
}MYSTRUCT, *LPMYSTRUCT;
MYSTRUCT wrStruct = {"ABCD", 0, 1, 2.0};
MYSTRUCT rdStruct = {"DCBA", 0, 2, 4.0};
DWORD dwError = 0;
UINT uFormat = 0;
//注册自定义格式
if(dwError == 0)
{
uFormat = RegisterClipboardFormat(_T("TestManulaFormat"));
if(uFormat == 0)//注册格式失败
{
dwError = GetLastError();
TRACE(_T("RegisterClipboardFormat Error (%ld)n"), dwError);
}
}
//将数据写入剪切板
if(dwError == 0)
{
if(OpenClipboard(NULL))//打开剪切板
{
if(EmptyClipboard())//清空剪切板
{
DWORD dwSize = sizeof(wrStruct);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dwSize); //分配内存
if(hMem)//分配内存有效
{
memcpy(GlobalLock(hMem), &wrStruct, dwSize);//拷贝数据
GlobalUnlock(hMem);//解锁
if(SetClipboardData(uFormat, hMem) == NULL) //数据写入剪切板
{
dwError = GetLastError();
TRACE(_T("SetClipboardData Error for write(%ld)n"), dwError);
}
}
else //分配内存出错
{
dwError = GetLastError();
TRACE(_T("GlobalAlloc Error for write(%ld)n"), dwError);
}
}
else //清空剪切板失败
{
dwError = GetLastError();
TRACE(_T("EmptyClipboard Error for write(%ld)n"), dwError);
}
if(!CloseClipboard())//关闭剪切板
{
dwError = GetLastError();
TRACE(_T("CloseClipboard Error for write(%ld)n"), dwError);
}
}
else//打开剪切板失败
{
dwError = GetLastError();
TRACE(_T("OpenClipboard Error for write(%ld)n"), dwError);
}
}
//从剪切板读出数据
if(dwError == 0)
{
if(OpenClipboard(NULL)) //打开剪切板
{
if(IsClipboardFormatAvailable(uFormat))//检查格式有效
{
HGLOBAL hMem = GetClipboardData(uFormat);//读取数据句柄
if(hMem != NULL)
{
memcpy(&rdStruct, GlobalLock(hMem), sizeof(rdStruct));//读出数据
GlobalUnlock(hMem);//解锁
}
else//读取句柄失败
{
dwError = GetLastError();
TRACE(_T("GetClipboardData Error for read (%ld)n"), dwError);
}
}
else//格式无效
{
dwError = GetLastError();
TRACE(_T("IsClipboardFormatAvailable Error for read (%ld)n"), dwError);
}
if(!CloseClipboard())//关闭剪切板
{
dwError = GetLastError();
TRACE(_T("CloseClipboard Error for read (%ld)n"), dwError);
}
}
else //打开剪切板失败
{
dwError = GetLastError();
TRACE(_T("OpenClipboard Error for read (%ld)n"), dwError);
}
}
//比较数据
if(dwError == 0)
{
if(memcmp(&wrStruct, &rdStruct, sizeof(wrStruct))==0)//相等
{
}
else
{
TRACE(_T("failed to compare data!n"));
ASSERT(FALSE);
}
}
}
最后
以上就是尊敬小刺猬为你收集整理的在剪切板中使用自定义格式的数据的全部内容,希望文章能够帮你解决在剪切板中使用自定义格式的数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复