⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowfinder.cpp

📁 teamviewer source code vc++
💻 CPP
字号:
//  Copyright (C) 2006 Teamviewer GmbH. All Rights Reserved.
//
//  This file is part of the TeamViewer system.
//
//  TeamViewer is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
//  If the source code for TeamViewer is not available from the place 
//  whence you received this file, check http://www.teamviewer.com

//  This class is an adaption of Lim Bio Liongs WindowFinder code
//  http://www.codeproject.com/dialog/windowfinder.asp

#include "stdhdrs.h"
#include "WindowFinder.h"

WindowFinder::WindowFinder(HWND dialog)
{ 
	hwndDialog = dialog;
	g_bStartSearchWindow = FALSE;

	g_hInst = hAppInstance;
	g_hCursorSearchWindow = LoadCursor(g_hInst, MAKEINTRESOURCE(IDC_CURSOR1));
	g_hRectanglePen = CreatePen(PS_SOLID, 8, RGB(119, 190, 29));
	g_hwndFoundWindow = NULL;
}

WindowFinder::~WindowFinder()
{
	DeleteObject(g_hRectanglePen);
}

//// Synopsis :
//// 1. This function checks a hwnd to see if it is actually the "Search Window" Dialog's or Main Window's
//// own window or one of their children. If so a FALSE will be returned so that these windows will not
//// be selected. 
////
//// 2. Also, this routine checks to see if the hwnd to be checked is already a currently found window.
//// If so, a FALSE will also be returned to avoid repetitions.
BOOL WindowFinder::CheckWindowValidity (HWND hwndToCheck)
{
  HWND ShellWnd = GetShellWindow();
  HWND DefView = FindWindowEx(ShellWnd, 0, "SHELLDLL_DefView", 0);
  HWND FolderView = FindWindowEx(DefView, 0, "SysListView32", 0);

  return  hwndToCheck != NULL &&				// The window must not be NULL.
	      IsWindow(hwndToCheck) != FALSE &&		// It must also be a valid window as far as the OS is concerned.
		  hwndToCheck != g_hwndFoundWindow &&   // not current found window 
		  hwndToCheck != hwndDialog &&			// not search window
		  hwndToCheck != GetParent(hwndDialog) &&			// or its parent
		  hwndToCheck != FindWindow("Shell_TrayWnd","") &&	// not taskbar
		  hwndToCheck != ShellWnd &&						// neither shell,
		  hwndToCheck != DefView &&							// 
		  hwndToCheck != FolderView;						// nor the desktop
			
}

// Synopsis :
// 1. This is the handler for WM_MOUSEMOVE messages sent to the "Search Window" dialog proc.
//
// 2. Note that we do not handle every WM_MOUSEMOVE message sent. Instead, we check to see 
// if "g_bStartSearchWindow" is TRUE. This BOOL will be set to TRUE when the Window
// Searching Operation is actually started. See the WM_COMMAND message handler in 
// SearchWindowDialogProc() for more details.
//
// 3. Because the "Search Window" dialog immediately captures the mouse when the Search Operation 
// is started, all mouse movement is monitored by the "Search Window" dialog box. This is 
// regardless of whether the mouse is within or without the "Search Window" dialog. 
//
// 4. One important note is that the horizontal and vertical positions of the mouse cannot be 
// calculated from "lParam". These values can be inaccurate when the mouse is outside the
// dialog box. Instead, use the GetCursorPos() API to capture the position of the mouse.
long WindowFinder::DoMouseMove 
(
  UINT message, 
  WPARAM wParam, 
  LPARAM lParam
)
{
  POINT		screenpoint;
  HWND		hwndFoundWindow = NULL;
  long		lRet = 0;

  // Must use GetCursorPos() instead of calculating from "lParam".
  GetCursorPos (&screenpoint);  

  // Determine the window that lies underneath the mouse cursor.
  hwndFoundWindow = WindowFromPoint (screenpoint);

  // Search parent window
  HWND parent;
  while((parent=GetParent(hwndFoundWindow)) != NULL)
		hwndFoundWindow=parent;

  // Check first for validity.
  if (CheckWindowValidity (hwndFoundWindow))
  {
    // We have just found a new window.

    // If there was a previously found window, we must instruct it to refresh itself. 
	// This is done to remove any highlighting effects drawn by us.
    if (g_hwndFoundWindow)
    {
      RefreshWindow (g_hwndFoundWindow);
    }

    // Indicate that this found window is now the current global found window.
    g_hwndFoundWindow = hwndFoundWindow;

    // We now highlight the found window.
    HighlightFoundWindow (g_hwndFoundWindow);
  }

  return lRet;
}

// Synopsis :
// 1. Handler for WM_LBUTTONUP message sent to the "Search Window" dialog box.
// 
// 2. We restore the screen cursor to the previous one.
//
// 3. We stop the window search operation and release the mouse capture.
HWND WindowFinder::DoMouseUp
(
  UINT message, 
  WPARAM wParam, 
  LPARAM lParam
)
{
  long lRet = 0;

  // If we had a previous cursor, set the screen cursor to the previous one.
  // The cursor is to stay exactly where it is currently located when the 
  // left mouse button is lifted.
  if (g_hCursorPrevious)
  {
    SetCursor (g_hCursorPrevious);
  }

  // If there was a found window, refresh it so that its highlighting is erased. 
  //if (g_hwndFoundWindow)
  //{
  //  RefreshWindow (g_hwndFoundWindow);
  //}

  // Very important : must release the mouse capture.
  ReleaseCapture ();

  // Set the global search window flag to FALSE.
  g_bStartSearchWindow = FALSE;

  return g_hwndFoundWindow;
}

// Synopsis :
// 1. This function starts the window searching operation.
//
// 2. A very important part of this function is to capture 
// all mouse activities from now onwards and direct all mouse 
// messages to the "Search Window" dialog box procedure.
long WindowFinder::SearchWindow()
{
  long lRet = 0;
  g_hwndFoundWindow = NULL;

  // Set the global "g_bStartSearchWindow" flag to TRUE.
  g_bStartSearchWindow = TRUE;

  // Set the screen cursor to the BullsEye cursor.
  if (g_hCursorSearchWindow)
  {
    g_hCursorPrevious = SetCursor(g_hCursorSearchWindow);
  }
  else
  {
    g_hCursorPrevious = NULL;
  }

  // Very important : capture all mouse activities from now onwards and
  // direct all mouse messages to the "Search Window" dialog box procedure.
  SetCapture(hwndDialog);
  
  return lRet;
}

long WindowFinder::RefreshWindow (HWND hwndWindowToBeRefreshed)
{
  long lRet = 0;

  InvalidateRect (hwndWindowToBeRefreshed, NULL, TRUE);
  UpdateWindow (hwndWindowToBeRefreshed);
  RedrawWindow (hwndWindowToBeRefreshed, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);

  return lRet;
}

// Performs a highlighting of a found window.
// Comments below will demonstrate how this is done.
long WindowFinder::HighlightFoundWindow (HWND hwndFoundWindow)
{
  HDC		hWindowDC = NULL;  // The DC of the found window.
  HGDIOBJ	hPrevPen = NULL;   // Handle of the existing pen in the DC of the found window.
  HGDIOBJ	hPrevBrush = NULL; // Handle of the existing brush in the DC of the found window.
  RECT		rect;              // Rectangle area of the found window.
  long		lRet = 0;

  // Get the screen coordinates of the rectangle of the found window.
  GetWindowRect (hwndFoundWindow, &rect);

  // Get the window DC of the found window.
  hWindowDC = GetWindowDC (hwndFoundWindow);

  if (hWindowDC)
  {
    // Select our created pen into the DC and backup the previous pen.
    hPrevPen = SelectObject (hWindowDC, g_hRectanglePen);

    // Select a transparent brush into the DC and backup the previous brush.
    hPrevBrush = SelectObject (hWindowDC, GetStockObject(HOLLOW_BRUSH));

    // Draw a rectangle in the DC covering the entire window area of the found window.
    Rectangle (hWindowDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top);

    // Reinsert the previous pen and brush into the found window's DC.
    SelectObject (hWindowDC, hPrevPen);

    SelectObject (hWindowDC, hPrevBrush);

    // Finally release the DC.
    ReleaseDC (hwndFoundWindow, hWindowDC);
  }

  return lRet;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -