我正在开发一个项目,该项目不允许用户从少数外部网站复制文本,我能够设置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()确实返回的值不是null。
答案 0 :(得分:1)
BTW:我在这里提到的功能是WinApi功能......