~~~~~~~~~~高手请进~~~~~~~~~~Windows下CTRL+X, CTRL+C, CTRL+V是调用哪些系统API~~~~~~~~~

bbn9435 2005-04-26 05:40:23
兄弟我正在做一个文件防护的冬冬
需要控制用户在Windows下使用剪切,复制,张贴的合法性.
打算用钩子技术实现
目前就是不知道explorer调用哪个API
...全文
296 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
horisly 2005-05-01
  • 打赏
  • 举报
回复
CTRL+X, CTRL+C, CTRL+V 这些是系统默认得快捷键。在《windows程序设计》的剪贴簿这章有详细的说明。

先留着,回头把这本书的内容整理整理
菲斯可儿 2005-04-28
  • 打赏
  • 举报
回复
接分啦。
linuxpgy 2005-04-28
  • 打赏
  • 举报
回复
bbn,帮你顶一下!
我也想知道explorer.exe在复制文件过程“粘贴”的时候调用了哪个API。
大家帮忙啊!
zhjie374 2005-04-27
  • 打赏
  • 举报
回复
楼主的意思是系统使用哪些API来完成。
即:ctrl+c后系统调用什么,ctrl+v后,系统又调用什么。
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
能扼要的说说实现的步骤么??
xuzheng318 2005-04-27
  • 打赏
  • 举报
回复
另外,你可以使用IDataObject接口.
参考《vc++技术内幕》第五版 第26章 统一数据传输:剪贴板传输和ole拖放.
xuzheng318 2005-04-27
  • 打赏
  • 举报
回复
拷贝与粘贴文本

下边的源代码演示了如何将文本(包含在CString对象“source”中)拷贝到剪贴板上。

CString source;
//put your text in source
if(OpenClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}

反过来,下面的代码是用来实现从剪贴板上取得文本的。

char * buffer = NULL;
//open the clipboard
CString fromClipboard;
if ( OpenClipboard() )
{
HANDLE hData = GetClipboardData( CF_TEXT );
char * buffer = (char*)GlobalLock( hData );
fromClipboard = buffer;
GlobalUnlock( hData );
CloseClipboard();
}

--------------------------------------------------------------------------------



拷贝与粘贴WMF(enhanced)数据

你想在你的程序中往剪贴板上“画”以及向剪贴板读取图形吗?请放心,这个――不难!示范代码如下,其实现的是往剪贴板上写一enhanced metafile。

if ( OpenClipboard() )
{
EmptyClipboard();

//create the metafile DC
CMetaFileDC * cDC = new CMetaFileDC();
cDC->CreateEnhanced(GetDC(),NULL,NULL,"the_name");

//call draw routine here that makes GDI calls int cDC

//close meta CMetafileDC and get its handle
HENHMETAFILE handle = cDC->CloseEnhanced();

//place it on the clipboard
SetClipboardData(CF_ENHMETAFILE,handle);
CloseClipboard();

//delete the dc
delete cDC;
}


好啦,该演示反过来怎么做的代码了。我们从剪贴板上取得metafile并将其画到自己的应用程序的客户区DC(设备上下文)上(仅仅是个试验而已,实际上你可能更想将它拷贝一份儿)。

if ( OpenClipboard() )
{
//Get the clipboard data
HENHMETAFILE handle = (HENHMETAFILE)GetClipboardData(CF_ENHMETAFILE);

//play it into a DC (our own DC in this example)
CClientDC dc(this);
CRect client(0,0,200,200);
dc.PlayMetaFile(handle,client);

//close the clipboard
CloseClipboard();
}

--------------------------------------------------------------------------------

拷贝与粘贴一张位图(BitMap)


拷贝和粘贴位图可是需要一些微妙的处理的,不过基本的思想还是一样。请看下面的代码。

if ( OpenClipboard() )
{
EmptyClipboard();
//create some data
CBitmap * junk = new CBitmap();
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
CRect client(0,0,200,200);
junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
dc.SelectObject(junk);

//call draw routine here that makes GDI calls
DrawImage(&dc,CString("Bitmap"));

//put the data on the clipboard
SetClipboardData(CF_BITMAP,junk->m_hObject);
CloseClipboard();

//copy has been made on clipboard so we can delete
delete junk;
}

如下示例代码是从剪贴板上取得一张位图,将它粘贴到客户区DC中。
if ( OpenClipboard() )
{
//Get the clipboard data
HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP);
CBitmap * bm = CBitmap::FromHandle(handle);

CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY);

CloseClipboard();
}

--------------------------------------------------------------------------------



建立并使用你自己定做的数据格式

如果你要拷贝、粘贴其它格式的数据,可以用RegisterClipboardFormat() API函数先将此格式注册,然后就可以“为所欲为”了。这简直是太有用了,尤其是在我们自己的应用程序中拷贝资料。假设我们有下面的结构:

struct MyFormatData
{
long val1;
int val2;
};


想将此结构的数据拷贝到剪贴板上。可以这样实现:

UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
if(OpenClipboard())
{
//make some dummy data
MyFormatData data;
data.val1 = 100;
data.val2 = 200;

//allocate some global memory
HGLOBAL clipbuffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData));
MyFormatData * buffer = (MyFormatData*)GlobalLock(clipbuffer);

//put the data into that memory
*buffer = data;

//Put it on the clipboard
GlobalUnlock(clipbuffer);
SetClipboardData(format,clipbuffer);
CloseClipboard();
}


想把它从剪贴板上读下来的话,也容易:



//第二次调用时,此格式已经注册过了,读下来就行了
UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
MyFormatData data;
if ( OpenClipboard() )
{
//get the buffer
HANDLE hData = GetClipboardData(format);
MyFormatData * buffer = (MyFormatData *)GlobalLock( hData );

//留一份儿当地拷贝
data = *buffer;

GlobalUnlock( hData );
CloseClipboard();
}



--------------------------------------------------------------------------------



取得剪贴板变化通知(Getting notified of clipboard changes)

一旦剪贴板上的内容发生改变,我们都希望能够获知(经由windows消息),这是很有用的。你可以用函数SetClipboardViewer()来捕获WM_DRAWCLIPBOARD消息。


在你的初始化代码中调用:
SetClipboardViewer(); //add us to clipboard change notification chain

在你的消息映射(message map)中添加:
ON_MESSAGE(WM_DRAWCLIPBOARD, OnClipChange) //clipboard change notification

将其定义为:
afx_msg void OnClipChange(); //clipboard change notification

实现为:
void CDetectClipboardChangeDlg::OnClipChange()
{
//do something here, for example
CTime time = CTime::GetCurrentTime();
SetDlgItemText(IDC_CHANGED_DATE,time.Format("%a, %b %d, %Y -- %H:%M:%S"));

DisplayClipboardText();
}

--------------------------------------------------------------------------------



将数据粘贴到其它应用程序窗口中的方法

我觉得如果能把文本拷贝到剪贴板上(参见上面的代码),然后再在另外一个应用程序中将这些文本粘贴过来,那样才有用。我写了一个很不错的本地应用程序,此程序使用了含有此技术的第三方的语言翻译包。很简单,仅是取得目标窗口的句柄,并向它发送“PASTE”消息就OK了。

SendMessage(m_hTextWnd, WM_PASTE, 0, 0);
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
又一次浪费这么多分
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
自己顶一下
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
up again!
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
自己顶一下
bbn9435 2005-04-27
  • 打赏
  • 举报
回复
TO: zhjie374(zhjie374)

是的



xhzxlqt 2005-04-26
  • 打赏
  • 举报
回复
API?

SendMessage
PostMessage
xhzxlqt 2005-04-26
  • 打赏
  • 举报
回复
你可以拦截这几个消息:
WM_CUT
WM_COPY
WM_PASTE
  • 打赏
  • 举报
回复
做键盘钩子,当有Ctrl+X发生时,剪贴板API判断当前剪贴板中的内容
剪贴板系列API:
Clipboard Functions
The following functions are used with the clipboard.

Function Description
ChangeClipboardChain Removes a specified window from the chain of clipboard viewers.
CloseClipboard Closes the clipboard.
CountClipboardFormats Retrieves the number of different data formats currently on the clipboard.
EmptyClipboard Empties the clipboard and frees handles to data in the clipboard.
EnumClipboardFormats Enumerates the data formats currently available on the clipboard.
GetClipboardData Retrieves data from the clipboard in a specified format.
GetClipboardFormatName Retrieves from the clipboard the name of the specified registered format.
GetClipboardOwner Retrieves the window handle of the current owner of the clipboard.
GetClipboardSequenceNumber Retrieves the clipboard sequence number for the current window station.
GetClipboardViewer Retrieves the handle to the first window in the clipboard viewer chain.
GetOpenClipboardWindow Retrieves the handle to the window that currently has the clipboard open.
GetPriorityClipboardFormat Retrieves the first available clipboard format in the specified list.
IsClipboardFormatAvailable Determines whether the clipboard contains data in the specified format.
OpenClipboard Opens the clipboard for examination and prevents other applications from modifying the clipboard content.
RegisterClipboardFormat Registers a new clipboard format.
SetClipboardData Places data on the clipboard in a specified clipboard format.
SetClipboardViewer Adds the specified window to the chain of clipboard viewers.

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧