这个方法是通过寻找本地资源管理器的窗口句柄,再去找地址栏编辑框的句柄,然后地址栏末端加上文件夹名字,最后发送回车消息,完成操作。似乎有几个步骤可以省略,还需要回头完善一下,例如组合框的下拉会被点击,这个操作不知道能不能省略或者让点击事件不那么明显,我禁用了以后就不能执行了,感觉还可以优化一下。因为那一段代码抄的一位大佬的代码,所以没去细细琢磨下每个类和消息产生的作用,但整体思路是正确的。
#include "pch.h" #include <windows.h> #include <stdio.h> #include <wchar.h> #include <locale.h> #include <string.h> #pragma warning (disable:4996) void TcharToChar(const TCHAR * tchar, char * _char) { int iLength; //获取字节长度 iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL); //将tchar值赋给_char WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL); } void CharToTchar(const char * _char, TCHAR * tchar) { int iLength; iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0); MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength); } int main() { TCHAR str[1024] = TEXT("Hello World"); HWND hwnd = NULL; HWND hToolbat; 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); hToolbat = FindWindowEx(hwnd, NULL, TEXT("ToolbarWindow32"), NULL); SendMessage(hToolbat, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(0, 0)); SendMessage(hToolbat, WM_LBUTTONUP, MK_LBUTTON, MAKELONG(0, 0)); SendMessage(hToolbat, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(0, 0)); SendMessage(hToolbat, 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); if (hwnd == NULL) { wprintf(L"%s ", L"error"); return -1; } SendMessage(hwnd, WM_GETTEXT, 1024, (LPARAM)str); char b[1024]; TcharToChar(str,b); strcat(b, "\PanData"); CharToTchar(b,str); //MessageBox(NULL, str, TEXT("Caption"), 0); SendMessage(hwnd, EM_SETSEL, 0, lstrlen(str)); SendMessage(hwnd, EM_REPLACESEL, true, (LPARAM)str); SendMessage(hwnd, WM_KEYDOWN, VK_RETURN, NULL); //SetWindowText(hwnd, str); return 0; }