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

📄 hideitdlg.cpp

📁 一个可以管理当前窗口显示和隐藏的完整程序
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////
// File:	HideItDlg.cpp
// Version:	1.0
// Created:	19-Feb-2003
//
// Author:	Paul S. Vickery
// E-mail:	paul@vickeryhome.freeserve.co.uk
//
// Utility to show or hide top-level windows.
//
// You are free to use or modify this code, with no restrictions, other than
// you continue to acknowledge me as the original author in this source code,
// or any code derived from it.
//
// If you use this code, or use it as a base for your own code, it would be 
// nice to hear from you simply so I know it's not been a waste of time!
//
// Copyright (c) 1997-2003 Paul S. Vickery
//
////////////////////////////////////////////////////////////////////////////
// Version History:
//
// Version 1.0
// ===========
// Initial version. Based on OnTop 3.0.2.
// 
////////////////////////////////////////////////////////////////////////////
// PLEASE LEAVE THIS HEADER INTACT
////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "HideIt.h"
#include "HideItDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define HAS_DIALOG 1

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
  CAboutDlg() : CDialog(IDD_ABOUTBOX) {};
};

/////////////////////////////////////////////////////////////////////////////
// CHideItDlg dialog

/*static*/ BOOL CALLBACK CHideItDlg::ListWindows(HWND hwnd, LPARAM lparam)
{
  BOOL bRetVal = TRUE;
  CHideItDlg* pThis = (CHideItDlg*)lparam;
  char *lpsz = NULL;
  int nTitleLength = ::GetWindowTextLength(hwnd);
  if (nTitleLength != 0)
  {
    nTitleLength++;
    lpsz = new char[nTitleLength];
    nTitleLength = ::GetWindowText(hwnd, lpsz, nTitleLength);
  }

  // tried: IsWindow(hwnd) - didn't filter out any
  // tried: IsWindowEnabled(hwnd) - filtered out threads (OLE etc)
  if (hwnd == pThis->GetSafeHwnd())
  {
    // don't process this window
    //TRACE0("This window!\n");
  }
  else if (nTitleLength == 0)
  {
    // no title!
    TRACE0("Window has no title\n");
  }
  else if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW)
  {
    //TRACE("Tool window (%s)\n", lpsz);
  }
  else if (! pThis->m_bAddToPicker && pThis->m_listExclude.Find(lpsz) != NULL)
  {
    TRACE("Window in exclusion list (%s)\n", lpsz);
  }
  else
  {
    if (lpsz == NULL || nTitleLength == 0)
    {
      // must be an error!
      TRACE0("Error getting window text\n");
      bRetVal = FALSE;
    }
    else
    {
      // find out if the window is visible
      BOOL bIsVisible = ::IsWindowVisible(hwnd);

      if (pThis->m_bAddToPicker)
      {
	BOOL bLeft = pThis->m_listExclude.Find(lpsz) == NULL;
	CStringList slistFound;
	pThis->m_picker.GetListItems(bLeft, slistFound);
	if (slistFound.Find(lpsz) == NULL)
	  pThis->m_picker.AddListItem(bLeft, lpsz, hwnd);
      }
      else
      {
	// add to menu
	UINT nIDMax = pThis->m_nIDMax;
	CMenu* pMenu = bIsVisible ? pThis->m_pMenu : pThis->m_pMenuHidden;
	HMENU hMenu = pMenu->GetSafeHmenu();
	MENUITEMINFO mii;
	mii.cbSize = sizeof(MENUITEMINFO);
	mii.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE;
	mii.wID = nIDMax + 1;
	mii.fType = MFT_STRING;
	mii.dwItemData = (DWORD)hwnd;
	mii.dwTypeData = lpsz;

	// put the windows in in the order that we find them - that way
	// they appear in their z-order - and topmost are all at top of menu
	::InsertMenuItem(hMenu, nIDMax, TRUE, &mii);

	pThis->m_nIDMax++;
      }
    }
  }
  if (lpsz != NULL)
    delete lpsz;
  return bRetVal;
}

#define WM_TRAYICON	(WM_USER + 1)

CHideItDlg::CHideItDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHideItDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHideItDlg)
	//}}AFX_DATA_INIT
  m_bAddToPicker = FALSE;
}

void CHideItDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHideItDlg)
	DDX_Control(pDX, IDC_PICKER, m_picker);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHideItDlg, CDialog)
	//{{AFX_MSG_MAP(CHideItDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_MESSAGE(WM_TRAYICON, OnTrayIconEvent)
	ON_WM_SIZE()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHideItDlg message handlers

BOOL CHideItDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  
  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

  // Add "About..." menu item to system menu.
  
  // IDM_ABOUTBOX must be in the system command range.
  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  ASSERT(IDM_ABOUTBOX < 0xF000);
  
  CMenu* pSysMenu = GetSystemMenu(FALSE);
  if (pSysMenu != NULL)
  {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
      pSysMenu->AppendMenu(MF_SEPARATOR);
      pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
  }

  // Set the icon for this dialog.  The framework does this automatically
  //  when the application's main window is not a dialog
  SetIcon(m_hIcon, TRUE);			// Set big icon
  SetIcon(m_hIcon, FALSE);		// Set small icon

  m_picker.SetLabels("Windows not excluded:", "Windows excluded:");
  m_picker.SetHorizontal();
  
  GetExclusionList();
  m_bAddToPicker = TRUE;
  GetWindowList();
  m_bAddToPicker = FALSE;

  // if the icon can't be added to the taskbar, then show window
  // this will ensure it is still usable on NT
  m_bTraySuccess = TRUE;
  if (! AddTrayIcon(IDR_MAINFRAME, m_hIcon, "HideIt"))
  {
    AfxMessageBox("Error adding Tray icon!");
    EndDialog(-1);
  }

  // put ourselves on-top, else we may end up disappearing behind
  // windows which we've made top-most
  SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  
  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CHideItDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
  if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
  }
  else
  {
    CDialog::OnSysCommand(nID, lParam);
  }
}

BOOL CHideItDlg::GetWindowList()
{
  BOOL bRetVal = TRUE;
  
  m_nIDMax = 0;
  
  // get all windows
  if (EnumWindows(ListWindows, (LPARAM)this) == FALSE)
  {
    TRACE0("Error getting window handles\n");
    bRetVal = FALSE;
  }
  return bRetVal;
}

void CHideItDlg::OnDestroy() 
{
  DeleteTrayIcon(IDR_MAINFRAME);

  PostQuitMessage(0);
  
  CDialog::OnDestroy();
}

BOOL CHideItDlg::AddTrayIcon(UINT uID, HICON hicon, LPSTR lpszTip)
{
  BOOL res;
  NOTIFYICONDATA tnid;
  
  tnid.cbSize = sizeof(NOTIFYICONDATA);
  tnid.hWnd = GetSafeHwnd();
  tnid.uID = uID;
  tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  tnid.uCallbackMessage = WM_TRAYICON;
  tnid.hIcon = hicon;
  if (lpszTip)
    lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip));
  else
    tnid.szTip[0] = '\0';
  
  res = Shell_NotifyIcon(NIM_ADD, &tnid);
  
  if (hicon)
    DestroyIcon(hicon);
  
  return res;
  
}

BOOL CHideItDlg::DeleteTrayIcon(UINT uID)
{
  BOOL res;
  NOTIFYICONDATA tnid;
  
  tnid.cbSize = sizeof(NOTIFYICONDATA);
  tnid.hWnd = GetSafeHwnd();
  tnid.uID = uID;
  
  res = Shell_NotifyIcon(NIM_DELETE, &tnid);
  
  return res;
  
}

// if anything useful gets put on the dialog, uncomment this
//#define HAS_DIALOG

///////////////////////////////////////////////////////////////////////////////
// When the user moves the mouse over the icon, the system will display the ToolTip that you provided when you added the icon.
// When the user clicks the icon, your application should display a window with additional information.
// When the user clicks the icon with the right mouse button, your application should display the shortcut menu.
// When the user double-clicks the icon, your application should execute the default shortcut menu command.
LONG CHideItDlg::OnTrayIconEvent(UINT uID, LONG lParam)
{
#ifdef HAS_DIALOG
  // uID is the ID of the icon, lParam is the mouse event
  if ((UINT)lParam == WM_LBUTTONDBLCLK)
  {
    // refill exclusion list
//    GetExclusionList();
    m_picker.GetListCtrl(TRUE).ResetContent();
    m_picker.GetListCtrl(FALSE).ResetContent();
    m_bAddToPicker = TRUE;
    GetWindowList();
    m_bAddToPicker = FALSE;

    // show dialog box
    ShowWindow(SW_RESTORE);
  }
  else
#endif // HAS_DIALOG
  if ((UINT)lParam == WM_LBUTTONDOWN || (UINT)lParam == WM_RBUTTONDOWN)
  {
    // show menu IDR_POPUPS, submenu 0
    CMenu popup;
    if (!popup.LoadMenu(IDR_POPUPS))
      AfxThrowResourceException();
    CMenu* pMenu;
    pMenu = popup.GetSubMenu(0);
    ASSERT(pMenu != NULL);

#ifdef HAS_DIALOG
    // make the options item the default
    pMenu->SetDefaultItem(ID_OPTIONS);
#else
    // remove options, and separator
    pMenu->RemoveMenu(0, MF_BYPOSITION);
    pMenu->RemoveMenu(0, MF_BYPOSITION);
#endif

    // pre-pend some popups
    HMENU hMenuVis = CreatePopupMenu();
    HMENU hMenuHidden = CreatePopupMenu();
    pMenu->InsertMenu(0, MF_BYPOSITION | MF_POPUP, (UINT)hMenuHidden, "Show Window");
    pMenu->InsertMenu(0, MF_BYPOSITION | MF_POPUP, (UINT)hMenuVis, "Hide Window");

    // pre-pend windows
    m_pMenu = CMenu::FromHandle(hMenuVis);
    m_pMenuHidden = CMenu::FromHandle(hMenuHidden);
    m_nIDMax = 0;
    GetWindowList();
    
    CPoint point;
    GetCursorPos(&point);
    
    // bring us to the front, else when user clicks 
    // outside if menu the menu stays up
    SetForegroundWindow();

    int nCmd = pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
						point.x, point.y, this);
    if (nCmd == ID_OPTIONS)
    {
      ShowWindow(SW_RESTORE);
//      ShowWindow(SW_SHOW);
    }
    else if (nCmd == ID_APP_ABOUT)
    {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal();
    }
    else if (nCmd == IDCANCEL)
      DestroyWindow();
    else if (nCmd > 0 && nCmd <= m_nIDMax)
    {
      // get menu item data
      MENUITEMINFO mii;
      mii.cbSize = sizeof(MENUITEMINFO);
      mii.fMask = MIIM_DATA;
      pMenu->GetMenuItemInfo(nCmd, &mii);
      HWND hwnd = (HWND)mii.dwItemData;

      if (hwnd != NULL)
      {
	// find out whether the window is already on top
	BOOL bIsVisible = ::IsWindowVisible(hwnd);

	// set the window's visible state
	::ShowWindow(hwnd, bIsVisible ? SW_HIDE : SW_SHOW);
      }
    }
  }

  return lParam;
}

void CHideItDlg::OnSize(UINT nType, int cx, int cy) 
{
  CDialog::OnSize(nType, cx, cy);
  
  if (nType == SIZE_MINIMIZED)
    ShowWindow(SW_HIDE);
}

void CHideItDlg::OnClose() 
{
  //  CDialog::OnClose();
  ShowWindow(SW_HIDE);
}

void CHideItDlg::OnCancel() 
{
  // we don't want to close the app, so don't end the dialog
  //CDialog::OnCancel();
  ShowWindow(SW_HIDE);
}

void CHideItDlg::OnOK() 
{
  // we don't want to close the app, so don't end the dialog
  //CDialog::OnOK();

  // get the excluded windows from the picker
  m_listExclude.RemoveAll();
  m_picker.GetListItems(FALSE, m_listExclude);

  // save exclusion list to profile
  CWinApp* pApp = AfxGetApp();

  CString sKey;
  int n = 0;
  POSITION pos = m_listExclude.GetHeadPosition();
  while (pos != NULL)
  {
    CString sWindow = m_listExclude.GetNext(pos);
    sKey.Format("Exclusion%d", n);
    pApp->WriteProfileString("Exclusions", sKey, sWindow);
    n++;
  }

  ShowWindow(SW_HIDE);
}

void CHideItDlg::GetExclusionList()
{
  // trawl through the old registry thingy, and get the list
  // of window names to exclude from the menus

  CWinApp* pApp = AfxGetApp();

  m_listExclude.RemoveAll();

  CString sKey;
  int n = 0;
  for (;;)
  {
    sKey.Format("Exclusion%d", n);
    CString sWindow = pApp->GetProfileString("Exclusions", sKey);
    if (sWindow.IsEmpty())
      break;
    m_listExclude.AddTail(sWindow);
    n++;
  }

  if (n == 0) // no entries found - use some defaults
  {
    m_listExclude.AddTail("Program Manager");
    m_listExclude.AddTail("DDE Server Window");
    m_listExclude.AddTail("W");
    m_listExclude.AddTail("WMS Idle");
    m_listExclude.AddTail("PFCoachWnd70");
    m_listExclude.AddTail("PerfectPrint");
    m_listExclude.AddTail("PerfectFont");
  }
}

⌨️ 快捷键说明

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