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

📄 alttab.cpp

📁 该代码压缩包只为需要研究CE下的任务管理器的实现而提供的。某些通用的头文件没有包含在其中
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			if(GetWindowText(hwndApp, wszBuf, MAX_PATH))
			{ 
				if(LB_ERR != (iIndex = ListBox_AddString(hwndLB, wszBuf)))
				{
					DEBUGMSG(ZONE_TRACETASKMAN, (L"TaskMan: Added (%s) hwnd=%x\r\n", wszBuf, hwndApp));
					ListBox_SetItemData(hwndLB, iIndex, hwndApp);
					iCount++;

					// hwndFG is the window that had the focus before Taskman was activated
					// select it in the listbox
					if(hwndApp == hwndFG)
					{
						DEBUGMSG(ZONE_TRACETASKMAN, (L"TaskMan: SELECTING (%s) hwnd=%x\r\n", wszBuf, hwndApp));
						ListBox_SetCurSel(hwndLB, iIndex);
					}
				}
				else { DEBUGMSG(ZONE_TRACETASKMAN, (L"TaskMan: hwnd=%x. GOT LB_ERR on AddString. GLE=%d\r\n", hwndApp, GetLastError())); }
			}
			else { DEBUGMSG(ZONE_TRACETASKMAN, (L"TaskMan: hwnd=%x. GetWindowText FAILED GLE=%d\r\n", hwndApp, GetLastError())); }
		}
		else { DEBUGMSG(ZONE_TRACETASKMAN, (L"TaskMan: SKIPPING hwnd=%x\r\n", hwndApp)); }
	}
	return iCount;
} 

// This is the "Really Kill?" confirmation dialog, called when trying to terminate
// an app that is hung or not responding. LPARAM on WM_INITDIALOG is a 
// killinfo struct with the HWND & window name of the offending app
LRESULT CALLBACK TaskMan_ReallyKill(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	KILLTARGETINFO* pki;
	DWORD dwProcessId;
	HANDLE hProc;

	switch (msg) {
		case WM_INITDIALOG:
			// save the killinfo struct
			SetWindowLong(hwnd, DWL_USER, (LONG)lp);
			pki = (KILLTARGETINFO*)lp;
			// set our title to that of the app being killed
			SetWindowText(hwnd, pki->pszTitle);
			MessageBeep((UINT)-1);
			break;

		case WM_COMMAND:
			switch (GET_WM_COMMAND_ID(wp,lp)) {
				case IDOK:
					// The user gave us the go ahead to TerminateProcess rudely
					pki = (KILLTARGETINFO*)GetWindowLong(hwnd, DWL_USER);
					GetWindowThreadProcessId(pki->hwnd, &dwProcessId);
					hProc = OpenProcess(PROCESS_ALL_ACCESS,0,dwProcessId);
					TerminateProcess(hProc, (UINT)-1);
					CloseHandle(hProc);

				case IDCANCEL:
					EndDialog(hwnd, GET_WM_COMMAND_ID(wp,lp));
					break;

				default:
					break;
			}
			break;
	}
    return FALSE;

} 

// This is called on process startup to create the Taskman dialog
BOOL TaskMan_Create()
{
	// Check if we have the GetOpenFilename API so we can show/hide the Browse button
	HINSTANCE hinst = LoadLibrary(L"coredll.dll");
	g_pfnGetOpenFileName = NULL;
	UINT iddDialog;
	if(hinst)
	{
		g_pfnGetOpenFileName = (PFNGETOPENFILENAME)GetProcAddress(hinst, L"GetOpenFileNameW");
		DEBUGMSG(ZONE_TRACE, (L"Taskman: GetOpenFilename %s\r\n", (g_pfnGetOpenFileName ? L"PRESENT" : L"MISSING")));
		FreeLibrary(hinst);
	}

	// Choose the dialog template based on screen-dimensions. See details in RC file
	iddDialog = (GetSystemMetrics(SM_CXSCREEN) < GetSystemMetrics(SM_CYSCREEN))
		? IDD_TASK_MANAGER_G : IDD_TASK_MANAGER;
	// Create the window and hide it
	if(g_hwndTaskMan = CreateDialog(g_hInst, MAKEINTRESOURCE(iddDialog), NULL, TaskMan_DlgProc))
	{
		DEBUGMSG(ZONE_TRACE, (TEXT("Created Minshell task-man window.\r\n")));
		ShowWindow(g_hwndTaskMan, SW_HIDE);
		return TRUE;
	}
	else
		return FALSE;
}

// called on process shutdown
void TaskMan_Destroy()
{
	if(g_hwndTaskMan) 
		DestroyWindow(g_hwndTaskMan);
}

// called on user pressing Alt-Tab etc.
void Show_TaskMan(void)
{
	// The Taskman window is created once at startup & stays around, hidden.
	if(g_hwndTaskMan) 
	{
		// We get the current foreground window & squirrel it away in the 
		// dialogs' DWL_USER so we can select it when we populate the listbox.
		HWND hwndFG = GetForegroundWindow();
		if(g_hwndTaskMan != hwndFG)
		{
			DEBUGMSG(ZONE_TRACE2, (TEXT("Show_Taskman: FG hwnd=%x\r\n"), hwndFG));
			SetWindowLong(g_hwndTaskMan, DWL_USER, (LONG)hwndFG);
		}

		// RestoreForegroundWindow is used in case the run dialog is up.  Then SetWindowPos is used
		// to make the window visible without trying to set it to the foreground.
		RestoreForegroundWindow(g_hwndTaskMan);
		SetWindowPos(g_hwndTaskMan, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_SHOWWINDOW);
	}
}

//
// Thsi is the dlgproc for the Start-Run-like dlg
//
LRESULT CALLBACK RunDlgProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	LPTSTR pszPath;
	switch (msg) {
        case WM_INITDIALOG:
        	CenterWindowSIPAware(hwnd, TRUE);
			ShowWindow(GetDlgItem(hwnd, IDC_BROWSE), (g_pfnGetOpenFileName ? SW_SHOW : SW_HIDE));
			EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
			SetFocus(GetDlgItem(hwnd, IDC_RUNCMD));
			return FALSE;	// prevents windows from setting the focus

		case WM_SETTINGCHANGE:
			// Recenter dialog when SIP goes up (see details in the block 
			// comment at the head of the CenterWindowSIPAware function)
			if(wp==SPI_SETSIPINFO)
				CenterWindowSIPAware(hwnd, FALSE);
			break;

		case WM_COMMAND:
			switch (GET_WM_COMMAND_ID(wp,lp)) 
			{
				case IDC_RUNCMD:
					// disable OK button if zero length string in IDC_RUNCMD
					if (HIWORD(wp) == EN_CHANGE) {
						EnableWindow(GetDlgItem(hwnd, IDOK),
							(GetWindowTextLength(GetDlgItem(hwnd, IDC_RUNCMD)) > 0));
					}
					break;
				
				// Try to run the command
				case IDOK:
				{
					TCHAR szInputBuf[MAX_PATH], szArgsBuf[MAX_PATH];
					LPTSTR lpszArgs = NULL;

					GetDlgItemText(hwnd, IDC_RUNCMD, szInputBuf, MAX_PATH);

					PathRemoveBlanks(szInputBuf);
					lpszArgs = PathGetArgs(szInputBuf);
					if (lpszArgs && lpszArgs[0]) {
						lstrcpy(szArgsBuf, lpszArgs);
						PathRemoveArgs(szInputBuf);
					} else {
						szArgsBuf[0] = 0;
					}

					// All error msgs done inside DoExec if the exec fails
					if(DoExec(hwnd, szInputBuf, szArgsBuf))
						EndDialog(hwnd, 0); // if DoExec succeeded, close Run dlg
					// else FAILED. Stay in dialog box. Errors already reported
				}
				break;
				
				case IDCANCEL:
					EndDialog(hwnd, -1);
					break;

				case IDC_BROWSE:
					if(pszPath = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR)*MAX_PATH))
					{
						pszPath[0] = 0;
						// open Browse (file-open common dialog). Selection
						// is returned in pszPath
						if(DoBrowse(hwnd, pszPath, MAX_PATH) && pszPath[0]) {
							// Check if the returned file-name has spaces in it
							if (wcschr(pszPath, TEXT(' ')) != NULL) {
								// If spaces in filename, we need to add quotes
								int len = wcslen(pszPath);
								DEBUGCHK(len >= 0 && len <= MAX_PATH);
								if (len < MAX_PATH - 2) {
									pszPath[len++] = TEXT('"');
									// NB: inc len so the 0 is copied as well
									pszPath[len++] = 0;
									// NB: memmove handles overlapping memory
									memmove(pszPath + 1, pszPath, len * sizeof(WCHAR));
									pszPath[0] = TEXT('"');
									DEBUGCHK(pszPath[len] == 0);
								} // else {
									// DoBrowse has returned a string
									// which needs quotes but we don't have
									// room to add them, so we do nothing.
								// }
							}
							// Place result of Browse in teh IDC_RUNCMD edit box
							SetDlgItemText(hwnd, IDC_RUNCMD, pszPath);
						}
						LocalFree(pszPath);
					}
					break;

				default:
					break;
			}
			break;
	}
    return FALSE;
}

//
// Call the GetOpenFileName common dialog, if present, return
// result in pszBuf
BOOL DoBrowse(HWND hwndParent, LPTSTR pszBuf, int iBufLen)
{
#define MAX_TITLE 100
	OPENFILENAME ofn;
	int iFilterLen;
	WCHAR szFilter[MAX_PATH];
	WCHAR szTitle[MAX_TITLE];

	if(!g_pfnGetOpenFileName)
		return FALSE;

	LoadString(g_hInst, IDS_BROWSE_TITLE, szTitle, cchsizeof(szTitle));
    iFilterLen = LoadString(g_hInst, IDS_BROWSE_FILTER, szFilter, cchsizeof(szFilter));
	
	// Convert \1 in the string to \0.  We need embedded NULLs because szFilter
	// is a multi_sz string, but loadstring only reads to the first NULL.
	// So in the RC file we use \1 instead of \0
	while( iFilterLen-- ) 
	{
		if( L'\1' == szFilter[iFilterLen] ) 
			szFilter[iFilterLen] = L'\0';
	} 

    pszBuf[0] = '\0';
	memset(&ofn, 0, sizeof(OPENFILENAME));
    ofn.lStructSize       = sizeof(OPENFILENAME);
    ofn.hwndOwner         = hwndParent;
    ofn.lpstrFilter       = szFilter;
    ofn.nFilterIndex      = 1;
    ofn.lpstrFile         = pszBuf;
    ofn.nMaxFile          = iBufLen;
    ofn.lpstrInitialDir   = L"\\Windows";
    ofn.lpstrTitle        = szTitle;
    ofn.Flags             = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
    ofn.lpstrDefExt       = L"exe";

    return g_pfnGetOpenFileName(&ofn);
}


⌨️ 快捷键说明

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