【MoreWindows工作笔记12】WM_DRAWCLIPBOARD 监视剪切板

有很多程序会监视剪贴板,比如迅雷,这样当你复制一个迅雷下载链接时,迅雷就可以自动启动并创建一个新的下载任务。下面我们就来看看如何实现这个技术。


实现这个技术一共需要4步:

第一步:Add the window to the clipboard viewer chain.

通过SetClipboardViewer()传入窗口句柄,所有监视剪贴板的窗口句柄会组成一个链表(后来者靠前)。这样当剪贴板内容发生变化时,Windows系统给给这些窗口发生消息。


第二步:Process the WM_CHANGECBCHAIN message.

当这个监视剪贴板的窗口句柄链表发生变化时,会收到这个消息。每个窗口都应该给下一个窗口传递消息。

这个消息的wParamlParam说明如下:

wParam表示

A handle to the window being removedfrom the clipboard viewer chain

lParam表示

A handle to the next window in thechain following the window being removed. This parameter is NULL if thewindow being removed is the last window in the chain


第三步:Process the WM_DRAWCLIPBOARD message.

当剪贴板数据发送变化时,窗口会依次收到这个消息。


第四步:Remove the window from the clipboard viewer chain before itis destroyed.

当窗口关闭时,应该调用ChangeClipboardChain()来通知Windows系统将自己从监视剪贴板的窗口句柄链表中移除。


代码如下(下载地址:http://download.csdn.net/download/morewindows/6793027

// 【MoreWindows工作笔记12】WM_DRAWCLIPBOARD 监视剪切板
// http://blog.csdn.net/morewindows/article/details/17655429
// By MoreWindows( http://blog.csdn.net/MoreWindows )
BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  static HWND s_hwnd_new_clipboard_viewer = NULL;
  static HWND s_hwnd_edit_clipboard_info = NULL;

	switch (message)
	{
	case WM_INITDIALOG:
    // 4-1 Add the window to the clipboard viewer chain. 
    s_hwnd_new_clipboard_viewer = SetClipboardViewer(hDlg);
    // 把hwnd加入监视链,返回上一个加入的窗口句柄,如果是第一个,则返回值为NULL。新加的窗口在链条头部,成为“当前监视器”
		CenterWindow(hDlg);
    s_hwnd_edit_clipboard_info = GetDlgItem(hDlg, IDC_EDIT_CLIPBOARD_INFO);
		return FALSE;

  // 4-2 Process the WM_CHANGECBCHAIN message. 
  case WM_CHANGECBCHAIN:
    if ((HWND)wParam == s_hwnd_new_clipboard_viewer)
      s_hwnd_new_clipboard_viewer = (HWND)lParam;
    else
      SendMessage(s_hwnd_new_clipboard_viewer, message, wParam, lParam);

  // 4-3 Process the WM_DRAWCLIPBOARD message. 
  case WM_DRAWCLIPBOARD:  //剪切板内容发生变化
    if (OpenClipboard(hDlg)) {
      UINT clipboard_format = EnumClipboardFormats(0);
      HGLOBAL global_memory = GetClipboardData(clipboard_format);
      DWORD data_size = GlobalSize(global_memory);
      CloseClipboard();
      WCHAR clipboard_info[1024];
      swprintf(clipboard_info, L"Clipboard\r\n Data Format = %x\r\n Data Address = 0x%x\r\n Data Size = %d", clipboard_format, global_memory, data_size);
      if (clipboard_format == CF_UNICODETEXT) {
        LPCWSTR clipboard_data = (LPCWSTR)GlobalLock(global_memory);
        if (clipboard_data != NULL) {
          wcscat(clipboard_info, L"\r\nData: \r\n");
          WCHAR buffer[1024];
          DWORD data_size = GlobalSize(global_memory);
          for (size_t i = 0; i < data_size; i++)
            buffer[i] = clipboard_data[i];
          buffer[data_size] = L'\0';
          wcscat(clipboard_info, buffer);
        }
      }
      SetWindowTextW(s_hwnd_edit_clipboard_info, clipboard_info);
    }
    return FALSE;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
		case IDCANCEL:
      // 4-4 Remove the window from the clipboard viewer chain before it is destroyed. 
      ChangeClipboardChain(hDlg, s_hwnd_new_clipboard_viewer);
			EndDialog(hDlg, FALSE);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

运行程序,复制《【霍比特人2:史矛革之战】》的下载链接,然后程序会显示:



最后再列一下目录,方便大家查看。

1.《【MoreWindows工作笔记9OleGetClipboard 访问剪切板的文本内容》

http://blog.csdn.net/morewindows/article/details/17655053

2.《【MoreWindows工作笔记10OleGetClipboard 访问剪切板上的文件信息》

http://blog.csdn.net/morewindows/article/details/17655057

3.《【MoreWindows工作笔记11EnumClipboardFormats剪切板内容的数据格式》

http://blog.csdn.net/morewindows/article/details/17655299

4.《【MoreWindows工作笔记12WM_DRAWCLIPBOARD 监视剪切板》

http://blog.csdn.net/morewindows/article/details/17655429


本文地址:http://blog.csdn.net/morewindows/article/details/17655429   转载请标明出处,谢谢。

欢迎关注微博:http://weibo.com/MoreWindows 







  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值