识别锁定剪贴板的进程我有一个特殊的错误,当我的应用程序去处理复制和粘贴操作时,某些进程偶尔会出现使用剪贴板的情况。通过一些重试,我有一个可以接受的解决方案,但我想找到错误再次发生的过程。我将我的解决方案包装成一个易于使用的方法(和一些声明):[DllImport("user32.dll",SetLastError=true)]staticexternIntPtrGetOpenClipboardWindow();[DllImport("user32.dll",SetLastError=true)]staticexternintGetWindowThreadProcessId(IntPtrhWnd,outintlpdwProcessId);privatestaticProcessGetProcessLockingClipboard(){intprocessId;GetWindowThreadProcessId(GetOpenClipboardWindow(),outprocessId);这是一个类似的解决方案,但这会给你一个可以显示给用户的字符串:[System.Runtime.InteropServices.DllImport("user32.dll")]staticexternIntPtrGetOpenClipboardWindow();[System.Runtime.InteropServices.DllImport("user32.dll")]staticexternintGetWindowText(inthwnd,StringBuildertext,intcount);私有字符串getOpenClipboardWindowText(){IntPtrhwnd=GetOpenClipboardWindow();StringBuildersb=newStringBuilder(501);GetWindowText(hwnd.ToInt32(),sb,500);返回sb.ToString();基于JeffRoe的回答,但展示了如何获取文本长度,因此它可能>500。还处理窗口未找到的情况。[System.Runtime.InteropServices.DllImport("user32.dll")]staticexternIntPtrGetOpenClipboardWindow();[System.Runtime.InteropServices.DllImport("user32.dll")]staticexternintGetWindowText(inthwnd,StringBuildertext,intcount);[System.Runtime.InteropServices.DllImport("user32.dll")]privatestaticexternintGetWindowTextLength(inthwnd);私有静态字符串GetOpenClipboardWindowText(){varhwnd=GetOpenClipboardWindow();如果(hwnd==IntPtr.Zero){返回“未知”;}varint32Handle=hwnd.ToInt32();varlen=GetWindowTextLength(int32Handle);varsb=newStringBuilder(len);GetWindowText(int32Handle,sb,len);返回sb.ToString();}要诊断此类问题,我建议从ProcessExplorer开始,http://technet.microsoft.com/en-us/sysinternals/bb896653以上是C#学习教程的全部内容:识别锁定剪贴板的进程ShareIf对大家有用,需要了解更多的C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
