分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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

【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

  1. <code class="language-cpp">// 【MoreWindows工作笔记12】WM_DRAWCLIPBOARD 监视剪切板  
  2. // http://blog.csdn.net/morewindows/article/details/17655429  
  3. // By MoreWindows( http://blog.csdn.net/MoreWindows )  
  4. BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)  
  5. {  
  6.   static HWND s_hwnd_new_clipboard_viewer = NULL;  
  7.   static HWND s_hwnd_edit_clipboard_info = NULL;  
  8.   
  9.     switch (message)  
  10.     {  
  11.     case WM_INITDIALOG:  
  12.     // 4-1 Add the window to the clipboard viewer chain.   
  13.     s_hwnd_new_clipboard_viewer = SetClipboardViewer(hDlg);  
  14.     // 把hwnd加入监视链,返回上一个加入的窗口句柄,如果是第一个,则返回值为NULL。新加的窗口在链条头部,成为“当前监视器”  
  15.         CenterWindow(hDlg);  
  16.     s_hwnd_edit_clipboard_info = GetDlgItem(hDlg, IDC_EDIT_CLIPBOARD_INFO);  
  17.         return FALSE;  
  18.   
  19.   // 4-2 Process the WM_CHANGECBCHAIN message.   
  20.   case WM_CHANGECBCHAIN:  
  21.     if ((HWND)wParam == s_hwnd_new_clipboard_viewer)  
  22.       s_hwnd_new_clipboard_viewer = (HWND)lParam;  
  23.     else  
  24.       SendMessage(s_hwnd_new_clipboard_viewer, message, wParam, lParam);  
  25.   
  26.   // 4-3 Process the WM_DRAWCLIPBOARD message.   
  27.   case WM_DRAWCLIPBOARD:  //剪切板内容发生变化  
  28.     if (OpenClipboard(hDlg)) {  
  29.       UINT clipboard_format = EnumClipboardFormats(0);  
  30.       HGLOBAL global_memory = GetClipboardData(clipboard_format);  
  31.       DWORD data_size = GlobalSize(global_memory);  
  32.       CloseClipboard();  
  33.       WCHAR clipboard_info[1024];  
  34.       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);  
  35.       if (clipboard_format == CF_UNICODETEXT) {  
  36.         LPCWSTR clipboard_data = (LPCWSTR)GlobalLock(global_memory);  
  37.         if (clipboard_data != NULL) {  
  38.           wcscat(clipboard_info, L"\r\nData: \r\n");  
  39.           WCHAR buffer[1024];  
  40.           DWORD data_size = GlobalSize(global_memory);  
  41.           for (size_t i = 0; i < data_size; i++)  
  42.             buffer[i] = clipboard_data[i];  
  43.           buffer[data_size] = L'\0';  
  44.           wcscat(clipboard_info, buffer);  
  45.         }  
  46.       }  
  47.       SetWindowTextW(s_hwnd_edit_clipboard_info, clipboard_info);  
  48.     }  
  49.     return FALSE;  
  50.   
  51.     case WM_COMMAND:  
  52.         switch (LOWORD(wParam))  
  53.         {  
  54.         case IDOK:  
  55.         case IDCANCEL:  
  56.       // 4-4 Remove the window from the clipboard viewer chain before it is destroyed.   
  57.       ChangeClipboardChain(hDlg, s_hwnd_new_clipboard_viewer);  
  58.             EndDialog(hDlg, FALSE);  
  59.             return TRUE;  
  60.         }  
  61.         break;  
  62.     }  
  63.     return FALSE;  
  64. }</code>  
// 【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 = NULLstatic HWND s_hwnd_edit_clipboard_info = NULLswitch (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:史矛革之战】》的下载链接,然后程序会显示:

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



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

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 







           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

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

相关文章:

  • 2021-06-08
  • 2021-12-06
  • 2022-02-01
  • 2022-01-05
  • 2022-02-21
  • 2022-03-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-27
  • 2021-06-17
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-08-30
  • 2021-12-24
相关资源
相似解决方案