zoukankan      html  css  js  c++  java
  • 实现当前目录下开启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


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    jquery正则表达式验证:正整数(限制长度)
    H5页面快速搭建之高级字体应用实践
    如何用Python写一个贪吃蛇AI
    HashMap多线程并发问题分析
    为RecyclerView打造通用Adapter 让RecyclerView更加好用
    学好Mac常用命令,助力iOS开发
    使用 Realm 和 Swift 创建 ToDo 应用
    看Facebook是如何优化React Native性能
    利用github搭建个人maven仓库
    Objective-C Runtime之着魔的UIAlertView
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834693.html
Copyright © 2011-2022 走看看