实现当前目录下开启CMD

我们都知道在WIN7下,可以按shift+鼠标右键->在此处打开命令窗口

或者在

输入cmd,回车


那么,怎样去实现这样的功能呢?

//当前目录下开启CMD

#include <stdio.h>
#include <windows.h>


int main()
{
	TCHAR	pathString[100] = { 0 };
	TCHAR	batString[150] = { 0 };
	TCHAR	regString[200];
	DWORD	dwSize;
	HWND	hwnd = NULL;
	HKEY	hkey;


	lstrcpy(batString, TEXT("/c start /d \""));
	hwnd = FindWindowEx(NULL, NULL, TEXT("CabinetWClass"), NULL);
	if (hwnd == NULL)
	{
		// 读取注册表获取桌面路径
		dwSize = sizeof(regString);
		RegOpenKey(HKEY_CURRENT_USER,
			TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"),
			&hkey);
		RegQueryValueEx(hkey, TEXT("Desktop"), NULL, NULL, (LPBYTE)regString, &dwSize);
		wsprintf(pathString, TEXT("%s"), regString);
	}
	else
	{
		HWND hToolBar;
		hwnd = FindWindowEx(NULL, NULL, TEXT("CabinetWClass"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("WorkerW"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("ReBarWindow32"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("Address Band Root"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("msctls_progress32"), NULL);
		hToolBar = FindWindowEx(hwnd, NULL, TEXT("ToolbarWindow32"), NULL);


		SendMessage(hToolBar, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(0, 0));
		SendMessage(hToolBar, WM_LBUTTONUP, MK_LBUTTON, MAKELONG(0, 0));
		SendMessage(hToolBar, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(0, 0));
		SendMessage(hToolBar, WM_LBUTTONUP, MK_LBUTTON, MAKELONG(0, 0));


		hwnd = FindWindowEx(hwnd, NULL, TEXT("ComboBoxEx32"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("ComboBox"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("Edit"), NULL);

		SendMessage(hwnd, WM_GETTEXT, 100, (LPARAM)pathString);
	}
	lstrcat(batString, pathString);
	lstrcat(batString, TEXT("\""));

	//MessageBox(NULL, batString, TEXT("Caption"), 0);
	ShellExecute(NULL, NULL, TEXT("cmd.exe"), batString, NULL, SW_SHOW);

	return 0;
}


在网络、回收站、控制面板这些窗口下是不能开启cmd的,该程序没有判断这些情况。


当然简单的做法是:

//当前目录下开启CMD

#include <stdio.h>
#include <windows.h>


int main()
{
	TCHAR	pathString[100] = { 0 };
	TCHAR	batString[150] = { 0 };
	TCHAR	regString[200];
	DWORD	dwSize;
	HWND	hwnd = NULL;
	HKEY	hkey;


	lstrcpy(batString, TEXT("/c start /d \""));
	hwnd = FindWindowEx(NULL, NULL, TEXT("CabinetWClass"), NULL);
	if (hwnd == NULL)
	{
		// 读取注册表获取桌面路径
		dwSize = sizeof(regString);
		RegOpenKey(HKEY_CURRENT_USER,
			TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"),
			&hkey);
		RegQueryValueEx(hkey, TEXT("Desktop"), NULL, NULL, (LPBYTE)regString, &dwSize);
		wsprintf(pathString, TEXT("%s"), regString);
	}
	else
	{
		hwnd = FindWindowEx(NULL, NULL, TEXT("CabinetWClass"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("WorkerW"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("ReBarWindow32"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("Address Band Root"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("msctls_progress32"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("Breadcrumb Parent"), NULL);
		hwnd = FindWindowEx(hwnd, NULL, TEXT("ToolbarWindow32"), NULL);

		SendMessage(hwnd, WM_GETTEXT, 100, (LPARAM)pathString);
		lstrcpy(pathString, pathString + 4);

	}
	lstrcat(batString, pathString);
	lstrcat(batString, TEXT("\""));

	//MessageBox(NULL, batString, TEXT("Caption"), 0);
	//wprintf(TEXT("%s"), batString);
	ShellExecute(NULL, NULL, TEXT("cmd.exe"), batString, NULL, SW_SHOW);

	return 0;
}


简单点做法是:


在标题栏显示完整路径


则可以直接使用GetWindowText


posted @ 2016-06-29 13:45  N3verL4nd  阅读(285)  评论(0编辑  收藏  举报