问题
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:
- 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.
- 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