获取剪贴板所有者的标题/标题 [英] Get Clipboard owners Title/Caption

查看:64
本文介绍了获取剪贴板所有者的标题/标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目上,该项目不允许用户从几个外部网站复制文本,我能够设置ClipboarViewer并打断剪贴板,现在我正试图获取剪贴板的所有者和然后窗口的标题/标题,这是我尝试过的,但始终返回空

I am working on a project which will not allow users to copy text from few of the external websites, I was able to setup a ClipboarViewer and it is interrupting the clipboard, now I am trying to get the owner of the clipboard and then the title/caption of the windows, below is what I have tried but it always returns empty

    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()确实会返回不为空的值。

if I change the handler to the GetActiveWindow() then it works which is strange because the GetClipboardOwner() does return a value which is not null.

推荐答案


  1. 不需要应用程序定义一个使用OpenClipboard时的窗口句柄。因此,您必须意识到,有足够的机会永远不会得到结果。

  2. 如果它是拥有剪贴板的子窗口,则您可能会一直使用后退窗口堆栈

BTW:我在这里提到的函数是WinApi函数...

BTW: The function I mention here are the WinApi functions...

这篇关于获取剪贴板所有者的标题/标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆