Get Clipboard owners Title/Caption

怎甘沉沦 提交于 2019-12-13 07:40:34

问题


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();

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:


  1. There is no need that an application defines a window handle when using OpenClipboard. So you have to be aware that there are enough chances that you will never get a result.
  2. If it is a child window that owns the clipboard you may walk back the stack of windows always using GetParent until there is no longer a parent.

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



来源:https://stackoverflow.com/questions/19023754/get-clipboard-owners-title-caption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!