从剪贴板获取数据
Getting data from clipboard
我正在尝试从剪贴板获取数据,但当我复制到剪贴板时它似乎没有显示任何数据
这是我的剪贴板 class
namespace Diall_ClipBoard_catch
{
class ClipBoard
{
private:
::HANDLE dHDat;
::std::string tmpstringsign;
bool isopen;
char * dHbuffer;
char * dHbuffertemp;
char * dNtoken;
public:
ClipBoard(void)
{
this->dHbuffer = "";
this->dHbuffertemp = "";
this->tmpstringsign = "dnb_4554_2102";
this->isopen = false;
};
~ClipBoard(void)
{
if(this-isopen)
{
::CloseClipboard();
this->isopen = false;
}
}
void GetData(void)
{
this->Start();
if(this->isopen)
{
this->dHDat = ::GetClipboardData(CF_TEXT);
if(this->dHDat)
{
this->dHbuffer = (char*)::GlobalLock(this->dHDat);
if(::std::strcmp(this->dHbuffertemp, this->dHbuffer)!=0 && this->dHbuffer != "" && this->dHbuffer != NULL)
{
this->dHbuffertemp = this->dHbuffer;
::std::cout << this->dHbuffer << "\n";
}
::GlobalUnlock(this->dHDat);
}
this->~ClipBoard();
::Sleep(1000);
}
}
private:
Start(void)
{
}
} ;
}
这是我的 main.cpp
#include <iostream>
#include <windows.h>
#include <cstring>
#include "Clipboard.h"
int main()
{
::Diall_ClipBoard_catch::ClipBoard * clipboard = new Diall_ClipBoard_catch::ClipBoard();
while(1)
{
clipboard->GetData();
}
return 0;
}
即使我在 Codeblocks 上构建和 运行 时没有收到任何错误,我仍然没有在我的控制台上显示任何数据。
尽管我已经尝试过 codeblock 上的编译器设置,但我仍然无法让它工作
评论提供了足够的信息来解决这个问题。
在Start()
函数中,需要一些API调用,比如OpenClipboard, SetClipboardData.
而且你没有设置isopen
为true,所以调试的时候会发现断点无法到达GetClipboardData ()
。
添加以下代码进行测试:
Start(void)
{
const char* output = "Hello World";
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
if (!OpenClipboard(NULL))
{
return;
}
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
this->isopen = TRUE;
}
注意:以上代码使用SetClipboardData
将文本复制到剪贴板,然后使用GetClipboardData
获取文本
您也可以使用剪贴板自己复制文本。 运行 代码并检查 GetClipboardSequenceNumber 函数的 return 值是否已更改。如果发生变化,说明剪贴板内容发生变化或剪贴板被清空
代码示例:
#include <iostream>
#include <windows.h>
#include <cstring>
namespace Diall_ClipBoard_catch
{
class ClipBoard
{
private:
::HANDLE dHDat;
::std::string tmpstringsign;
bool isopen;
char* dHbuffer;
char* dHbuffertemp;
char* dNtoken;
public:
ClipBoard(void)
{
this->dHbuffer = const_cast <char*>("");
this->dHbuffertemp = const_cast <char*>("");
this->tmpstringsign = "dnb_4554_2102";
this->isopen = false;
};
~ClipBoard(void)
{
}
void GetData(void)
{
this->Start();
if (this->isopen)
{
this->dHDat = ::GetClipboardData(CF_TEXT);
if (this->dHDat)
{
this->dHbuffer = (char*)::GlobalLock(this->dHDat);
if (::std::strcmp(this->dHbuffertemp, this->dHbuffer) != 0 && this->dHbuffer != "" && this->dHbuffer != NULL)
{
this->dHbuffertemp = this->dHbuffer;
::std::cout << this->dHbuffer << "\n";
}
::GlobalUnlock(this->dHDat);
}
EmptyClipboard();
CloseClipboard();
this->isopen = FALSE;
::Sleep(1000);
}
}
private:
void Start(void)
{
if (!OpenClipboard(NULL))
{
return;
}
this->isopen = TRUE;
}
};
}
int main()
{
::Diall_ClipBoard_catch::ClipBoard* clipboard = new Diall_ClipBoard_catch::ClipBoard();
int temp1 = 0, temp2 = 0;
EmptyClipboard();
while (1)
{
temp1 = GetClipboardSequenceNumber();
if (temp1!= temp2)
{
clipboard->GetData();
}
temp2 = temp1;
}
return 0;
}
调试:
我正在尝试从剪贴板获取数据,但当我复制到剪贴板时它似乎没有显示任何数据 这是我的剪贴板 class
namespace Diall_ClipBoard_catch
{
class ClipBoard
{
private:
::HANDLE dHDat;
::std::string tmpstringsign;
bool isopen;
char * dHbuffer;
char * dHbuffertemp;
char * dNtoken;
public:
ClipBoard(void)
{
this->dHbuffer = "";
this->dHbuffertemp = "";
this->tmpstringsign = "dnb_4554_2102";
this->isopen = false;
};
~ClipBoard(void)
{
if(this-isopen)
{
::CloseClipboard();
this->isopen = false;
}
}
void GetData(void)
{
this->Start();
if(this->isopen)
{
this->dHDat = ::GetClipboardData(CF_TEXT);
if(this->dHDat)
{
this->dHbuffer = (char*)::GlobalLock(this->dHDat);
if(::std::strcmp(this->dHbuffertemp, this->dHbuffer)!=0 && this->dHbuffer != "" && this->dHbuffer != NULL)
{
this->dHbuffertemp = this->dHbuffer;
::std::cout << this->dHbuffer << "\n";
}
::GlobalUnlock(this->dHDat);
}
this->~ClipBoard();
::Sleep(1000);
}
}
private:
Start(void)
{
}
} ;
}
这是我的 main.cpp
#include <iostream>
#include <windows.h>
#include <cstring>
#include "Clipboard.h"
int main()
{
::Diall_ClipBoard_catch::ClipBoard * clipboard = new Diall_ClipBoard_catch::ClipBoard();
while(1)
{
clipboard->GetData();
}
return 0;
}
即使我在 Codeblocks 上构建和 运行 时没有收到任何错误,我仍然没有在我的控制台上显示任何数据。 尽管我已经尝试过 codeblock 上的编译器设置,但我仍然无法让它工作
评论提供了足够的信息来解决这个问题。
在Start()
函数中,需要一些API调用,比如OpenClipboard, SetClipboardData.
而且你没有设置isopen
为true,所以调试的时候会发现断点无法到达GetClipboardData ()
。
添加以下代码进行测试:
Start(void)
{
const char* output = "Hello World";
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
if (!OpenClipboard(NULL))
{
return;
}
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
this->isopen = TRUE;
}
注意:以上代码使用SetClipboardData
将文本复制到剪贴板,然后使用GetClipboardData
获取文本
您也可以使用剪贴板自己复制文本。 运行 代码并检查 GetClipboardSequenceNumber 函数的 return 值是否已更改。如果发生变化,说明剪贴板内容发生变化或剪贴板被清空
代码示例:
#include <iostream>
#include <windows.h>
#include <cstring>
namespace Diall_ClipBoard_catch
{
class ClipBoard
{
private:
::HANDLE dHDat;
::std::string tmpstringsign;
bool isopen;
char* dHbuffer;
char* dHbuffertemp;
char* dNtoken;
public:
ClipBoard(void)
{
this->dHbuffer = const_cast <char*>("");
this->dHbuffertemp = const_cast <char*>("");
this->tmpstringsign = "dnb_4554_2102";
this->isopen = false;
};
~ClipBoard(void)
{
}
void GetData(void)
{
this->Start();
if (this->isopen)
{
this->dHDat = ::GetClipboardData(CF_TEXT);
if (this->dHDat)
{
this->dHbuffer = (char*)::GlobalLock(this->dHDat);
if (::std::strcmp(this->dHbuffertemp, this->dHbuffer) != 0 && this->dHbuffer != "" && this->dHbuffer != NULL)
{
this->dHbuffertemp = this->dHbuffer;
::std::cout << this->dHbuffer << "\n";
}
::GlobalUnlock(this->dHDat);
}
EmptyClipboard();
CloseClipboard();
this->isopen = FALSE;
::Sleep(1000);
}
}
private:
void Start(void)
{
if (!OpenClipboard(NULL))
{
return;
}
this->isopen = TRUE;
}
};
}
int main()
{
::Diall_ClipBoard_catch::ClipBoard* clipboard = new Diall_ClipBoard_catch::ClipBoard();
int temp1 = 0, temp2 = 0;
EmptyClipboard();
while (1)
{
temp1 = GetClipboardSequenceNumber();
if (temp1!= temp2)
{
clipboard->GetData();
}
temp2 = temp1;
}
return 0;
}
调试: