سلام
توی این پست برنامۀ استفاده از clipboard رو میبینید. این برنامه از تابعهای API استفاده میکنه.
این برنامه رو اجرا کنید:
#include <conio.h>
#undef UNICODE
#define UNICODE
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
void print_error(wchar_t* message)
{
MessageBox(0, message,message, MB_OK);
}
class Clipboard
{
public:
void Empty()
{
EmptyClipboard();
}
void Copy(wstring wstr)
{
Empty();
unsigned n = wstr.size();
void* global_data = GlobalAlloc(GMEM_MOVEABLE,(n + 1) * sizeof(wchar_t));
if (global_data == 0)
print_error(L"Clipboard::Copy()");
wchar_t* str_t = (wchar_t*) GlobalLock(global_data);
for(unsigned i = 0; i < n; i++)
str_t[i] = wstr[i];
str_t[n] = 0;
GlobalUnlock(global_data);
SetClipboardData(CF_UNICODETEXT,global_data);
}
wstring Paste()
{
wchar_t* str_t = L"";
void* hglb = GetClipboardData(CF_UNICODETEXT);
if (hglb != 0)
{
str_t = (wchar_t*)GlobalLock(hglb);
if (str_t != 0)
GlobalUnlock(hglb);
}
return wstring(str_t);
}
Clipboard()
{
if(OpenClipboard(0) == 0)
print_error(L"ClipBoard constructor");
}
~Clipboard()
{
CloseClipboard();
}
};
int main()
{
wcout<< L"Press 'a' to copy 'This is a text'" L"\n";
wcout<< L"Press any key other key to paste" L"\n\n";
if(_getch() == 'a')
{
Clipboard cb;
cb.Copy(L"This is a text");
wcout<< L"text was copied!";
}
else
{
Clipboard cb;
wstring wstr = cb.Paste();
wcout<< wstr;
}
_getch();
}
Output:
Press 'a' to copy 'This is a text'
Press any key other key to paste
text was copied!
وقتی کلید ‘a’ از کیبورد را فشار میدیم، جملۀ ‘This is a text’ کپی میشه. و میتونید اونو هر جایی پیست کنید. اگه کلید ‘b’ رو فشار بدیم هر چی که قبلا کپی شده چاپ میشه. البته هر بار باید از اول برنامه رو اجرا کنید.
No comments:
Post a Comment