Browse Source

Use ::GetClipboardOwner to track who placed data on the clipboard

Scott Brogden 9 years ago
parent
commit
7bd4fb6840
3 changed files with 29 additions and 7 deletions
  1. 19 2
      ClipboardViewer.cpp
  2. 7 4
      Misc.cpp
  3. 3 1
      Misc.h

+ 19 - 2
ClipboardViewer.cpp

@@ -292,8 +292,25 @@ void CClipboardViewer::OnDrawClipboard()
 
 bool CClipboardViewer::ValidActiveWnd()
 {
-	HWND active = ::GetForegroundWindow();
-	CString activeApp = GetProcessName(active).MakeLower();
+	CString activeApp = _T("");
+
+	HWND owner = ::GetClipboardOwner();
+	if (owner != NULL)
+	{
+		DWORD PID = 0;
+		::GetWindowThreadProcessId(owner, &PID);
+
+		if (PID != 0)
+		{
+			activeApp = GetProcessName(NULL, PID);
+		}
+	}
+
+	if (activeApp == _T(""))
+	{
+		HWND active = ::GetForegroundWindow();
+		activeApp = GetProcessName(active, 0).MakeLower();
+	}
 
 	CString includeApps = CGetSetOptions::GetCopyAppInclude().MakeLower();
 

+ 7 - 4
Misc.cpp

@@ -878,13 +878,16 @@ __int64 GetLastWriteTime(const CString &csFile)
 	return nLastWrite;
 }
 
-CString GetProcessName(HWND hWnd) 
+CString GetProcessName(HWND hWnd, DWORD processId) 
 {
 	DWORD startTick = GetTickCount();
 
-	DWORD Id;
-	CString	strProcessName ;
-	GetWindowThreadProcessId(hWnd, &Id);
+	CString	strProcessName;
+	DWORD Id = processId;
+	if (Id == 0)
+	{		
+		GetWindowThreadProcessId(hWnd, &Id);
+	}
 
 	PROCESSENTRY32 processEntry = { 0 };
 

+ 3 - 1
Misc.h

@@ -120,7 +120,7 @@ CRect CenterRect(CRect startingRect);
 CRect CenterRectFromRect(CRect startingRect, CRect outerRect);
 
 __int64 GetLastWriteTime(const CString &csFile);
-CString GetProcessName(HWND hWnd);
+
 
 //Message to the main window to show icon or not
 #define WM_SHOW_TRAY_ICON		WM_USER + 200
@@ -180,4 +180,6 @@ CString InternetEncode(CString text);
 
 bool WriteCF_DIBToFile(CString csPath, LPVOID data, ULONG size);
 
+CString GetProcessName(HWND hWnd, DWORD processId = 0);
+
 #endif // !defined(AFX_CP_GUI_GLOBALS__FBCDED09_A6F2_47EB_873F_50A746EBC86B__INCLUDED_)