实现当前目录下开启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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值