本文介绍了剪贴板查看器没有收到粘贴通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阻止某些程序窗口中剪贴板的粘贴。我在程序中设置了剪贴板查看器,如下所示:

I need to block pasting from the clipboard in the window of the certain program. I set a clipboard viewer in my program like this:

HWND hwndNextViewer = 0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) {
       case WM_CREATE: {
          hwndNextViewer = SetClipboardViewer(hWnd);
          break;
       }

        default:
            return DefWindowProcW(hWnd, message, wParam, lParam);
  }
  case WM_DRAWCLIPBOARD: {
        if(GetForegroundWindow() == hWndTarget){
            OpenClipboard(hWndTarget);
            EmptyClipboard();
            CloseClipboard();
        }
        SendMessage(hwndNextViewer, message, wParam, lParam);
        break;
   }
    case WM_CHANGECBCHAIN:{

        if ((HWND)wParam == hwndNextViewer)

             hwndNextViewer = (HWND)lParam;

        else if (hwndNextViewer != NULL) {
             SendMessage(hwndNextViewer, message, wParam, lParam);
        }

        break;
    }

 }

}



What I'm doing is I'm checking if the current foreground window is the target program window, and if it is, I empty the clipboard. But this code doesn't detect pasting. for example when I paste inside the target window using the mouse.

如何检测粘贴?

推荐答案

剪贴板查看器和剪贴板格式侦听器(在Vista +中应使用它而不是查看器)仅用于检测剪贴板上存储的数据的更改。没有其他的。当将新数据放入剪贴板时, WM_DRAWCLIPBOARD WM_CLIPBOARDUPDATE 消息将发送给查看者/听众。从剪贴板粘贴数据时,没有消息发送。

A clipboard viewer, and a clipboard format listener (which is what you should be using in Vista+ instead of a viewer), are intended ONLY for detecting changes to the data stored on the clipboard. Nothing else. The WM_DRAWCLIPBOARD and WM_CLIPBOARDUPDATE messages are sent to viewers/listeners when new data is put on the clipboard. There is no message sent when data is being pasted from the clipboard.

您根本无法使用剪贴板查看器/侦听器检测粘贴。

You simply CANNOT detect pastes with a clipboard viewer/listener. That task is not what those APIs are designed for.

您将不得不使用一些全局挂钩来完成您想要的事情。通过 SetWindowsHookEx() RegisterRawInputDevices()的键盘挂钩可以监视击键。通过 SetWindowsHookEx()的消息挂钩可以监视与粘贴相关的窗口消息,例如 WM_PASTE EM_PASTESPECIAL WM_COMMAND 等。但是即使如此,也不能保证检测到所有可能的粘贴操作。应用程序可以根据需要随意实现粘贴。

You are going to have to use some global hooks to do what you want. A keyboard hook via SetWindowsHookEx() or RegisterRawInputDevices() can monitor for keystrokes. A message hook via SetWindowsHookEx() can monitor for window messages related to pasting, like WM_PASTE, EM_PASTESPECIAL, WM_COMMAND, etc. But even so, this is no guarantee of detecting every possible paste operation. Applications are free to implement pastes however they want.

您可能不得不诉诸于将代码注入目标进程,以直接挂钩查询剪贴板的各种Win32 API函数。用于数据。然后,当目标进程尝试在任何类型的粘贴操作期间尝试检索数据时,就可以伪造结果(例如使应用程序认为没有可粘贴的数据):

You will likely have to resort to injecting code into the target process to directly hook the various Win32 API functions that query the clipboard for data. Then you can fake the results when the target process tries to retrieve data during any kind of paste operation (like making the app think there is no data available to paste):


  • CountClipboardFormats()

  • EnumClipboardFormats()

  • IsClipboardFormatAvailable()

  • GetPriorityClipboardFormat()

  • GetUpdatedClipboardFormats()

  • GetClipboardData() OleGetClipboard()

  • CountClipboardFormats()
  • EnumClipboardFormats()
  • IsClipboardFormatAvailable()
  • GetPriorityClipboardFormat()
  • GetUpdatedClipboardFormats()
  • GetClipboardData() and OleGetClipboard()

这篇关于剪贴板查看器没有收到粘贴通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:21