1

我正在开发一个不允许用户从几个外部网站复制文本的项目,我能够设置一个 ClipboarViewer 并且它正在中断剪贴板,现在我正在尝试获取剪贴板的所有者,然后是标题/ 窗口的标题,下面是我尝试过的,但它总是返回空

    protected override void WndProc(ref Message m)
        {
            switch ((User32.Message) m.Msg)
            {
                case User32.Message.WM_DRAWCLIPBOARD:
                {
                    ClipboardChanged();

                    User32.SendMessage(_nextClipboardViewer, m.Msg, m.WParam, m.LParam);

                    string title = User32.GetWindowTitle(User32.GetClipboardOwner());
                }
                break;

                case User32.Message.WM_CHANGECBCHAIN:
                {
                    if (m.WParam == _nextClipboardViewer)
                    {
                        _nextClipboardViewer = m.LParam;
                    }
                    else
                    {
                        User32.SendMessage(_nextClipboardViewer, m.Msg, m.WParam, m.LParam);
                    }
                }
                break;

                case User32.Message.WM_CLIPBOARDUPDATE:
                {
                    ClipboardChanged();

                    string title = User32.GetWindowTitle(User32.GetClipboardOwner());
                }
                break;

                default:
                {
                    base.WndProc(ref m);
                }
                break;
            }
        }


public static string GetWindowTitle(IntPtr hWnd)
{
       StringBuilder Caption = new StringBuilder(256);

       //hWnd = GetActiveWindow();
       GetWindowText(hWnd, Caption, Caption.Capacity);

      return Caption.ToString();
}

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetClipboardOwner();

[DllImport("user32", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);

[DllImport("user32", CharSet = CharSet.Auto)]
public static extern IntPtr GetActiveWindow();

如果我将处理程序更改为 GetActiveWindow() 那么它会工作,这很奇怪,因为 GetClipboardOwner() 确实返回一个不为空的值。

4

1 回答 1

1
  1. 使用 OpenClipboard 时,应用程序无需定义窗口句柄。所以你必须意识到有足够的机会你永远不会得到结果。
  2. 如果它是拥有剪贴板的子窗口,则您可以始终使用 GetParent 回退窗口堆栈,直到不再有父窗口。

顺便说一句:我在这里提到的功能是 WinApi 功能......

于 2013-09-26T10:43:38.093 回答