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

📄 taskswitcherdlg.cpp

📁 EVC做的例程
💻 CPP
字号:
// TaskSwitcherDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TaskSwitcher.h"
#include "TaskSwitcherDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTaskSwitcherDlg dialog

/**************************************************************
*说明:
*	这四个全局变量作用分别是
*	g_iWndCount: 当前系统运行的任务数
*	g_hWndArray[]:保存当前运行任务的窗口句柄
*	g_strWndTextArray[]:保存运行任务的窗口标题
*	g_hWndIconArray[]:保存运行任务的窗口图标句柄
**************************************************************/
#define MAX_TASK_NUMBER			(int)24
extern int g_iWndCount;
extern HWND g_hWndArray[MAX_TASK_NUMBER];
extern CString g_strWndTextArray[MAX_TASK_NUMBER];
extern HICON g_hWndIconArray[MAX_TASK_NUMBER];

/**************************************************************
*函数名称:EnumWindowProc(...),回调函数
*函数功能:枚举当前运行的任务,并获取它们的窗口句柄、窗口标题、
			图标句柄
**************************************************************/
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lparam)
{
	CWnd *pWnd = CWnd::FromHandle(hWnd);
	if(pWnd != NULL && pWnd->IsWindowVisible())
	{
		pWnd->GetWindowText(g_strWndTextArray[g_iWndCount]);
		if(!g_strWndTextArray[g_iWndCount].IsEmpty())
		{
			g_strWndTextArray[g_iWndCount].TrimLeft();
			g_strWndTextArray[g_iWndCount].TrimRight();
			if(g_strWndTextArray[g_iWndCount] == "显示桌面")
			{
				g_hWndArray[MAX_TASK_NUMBER] = hWnd;
				g_hWndIconArray[MAX_TASK_NUMBER] = pWnd->GetIcon(FALSE);
			}
			else
			{
				g_hWndArray[g_iWndCount] = hWnd;
				g_hWndIconArray[g_iWndCount] = pWnd->GetIcon(FALSE);
				g_iWndCount ++;
			}
		}
	}
	return TRUE;
}

CTaskSwitcherDlg::CTaskSwitcherDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTaskSwitcherDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTaskSwitcherDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTaskSwitcherDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTaskSwitcherDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTaskSwitcherDlg, CDialog)
	//{{AFX_MSG_MAP(CTaskSwitcherDlg)
	ON_BN_CLICKED(IDC_BTN_MENU, OnBtnMenu)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTaskSwitcherDlg message handlers

BOOL CTaskSwitcherDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	ShowWindow(SW_SHOWMAXIMIZED);
//	ShowWindow(SW_SHOWMINIMIZED);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTaskSwitcherDlg::OnOK() 
{
	// TODO: Add extra validation here
	if(pListMenu)
	{
		pListMenu = NULL;
		delete pListMenu;
	}
	CDialog::OnOK();
}


/*********************************************************************************
*函数名称:OnBtnMenu()
*函数功能:按钮响应函数,点击后弹出当前运行任务菜单
*输入参数:无
*输出参数:无
*创建者:孙军杰
*创建日期:2007-06-21
*********************************************************************************/
//Initialize task list menu and implement task switch
void CTaskSwitcherDlg::OnBtnMenu() 
{
	// TODO: Add your control notification handler code here
	if(pListMenu)
	{
		pListMenu = NULL;
		delete pListMenu;
	}
	pListMenu = new CMenu;
	if(!pListMenu->CreatePopupMenu())
	{ 
		return;
	}
	int count = pListMenu->GetMenuItemCount();
	g_iWndCount = 0;
	::EnumWindows((WNDENUMPROC)EnumWindowProc, 0);
	for(int i = 0; i < g_iWndCount; i ++)
	{
		pListMenu->InsertMenu(i, MF_BYPOSITION, IDM_TASK0 + i, g_strWndTextArray[i]);
	}
	pListMenu->InsertMenu(g_iWndCount, MF_BYPOSITION, IDM_SHOWDESKTOP, TEXT("显示桌面"));

	pListMenu->TrackPopupMenu(TPM_RIGHTALIGN, 240, 26, AfxGetMainWnd());
}

/*********************************************************************************
*函数名称:OnCmdMsg(...)
*函数功能:在此处用来相应点击菜单后的动作:执行任务切换
*输入参数:见MSDN
*输出参数:见MSDN
*创建者:孙军杰
*创建日期:2007-06-21
*********************************************************************************/
BOOL CTaskSwitcherDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pHandlerInfo == NULL)
	{
		// Filter the commands sent to a text color menu option
		if(nID == IDM_SHOWDESKTOP)
		{
			::SetForegroundWindow(g_hWndArray[MAX_TASK_NUMBER]);
			return TRUE;
		}
		else
		{
			for (int i = 0; i <= g_iWndCount; i++)
			{
				if (nID - IDM_TASK0 == (unsigned)i)
				{
					if (nCode == CN_COMMAND)
					{
						// Handle WM_COMMAND message
						::SetForegroundWindow(g_hWndArray[i]);
					}
					return TRUE;
				}
			}
		}
	}
	
	return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

⌨️ 快捷键说明

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