+ HWND hwnd;
+ if (FAILED(GetSearchResultsFolder(&pShellBrowser, &hwnd, NULL)))
+ {
+ // Open a new search results folder
+ WCHAR szShellGuid[MAX_PATH];
+ const WCHAR shellGuidPrefix[] = L"shell:::";
+ memcpy(szShellGuid, shellGuidPrefix, sizeof(shellGuidPrefix));
+ hr = StringFromGUID2(CLSID_FindFolder, szShellGuid + _countof(shellGuidPrefix) - 1,
+ _countof(szShellGuid) - _countof(shellGuidPrefix));
+ if (FAILED_UNEXPECTEDLY(hr))
+ return hr;
+
+ CComHeapPtr<ITEMIDLIST> findFolderPidl;
+ hr = SHParseDisplayName(szShellGuid, NULL, &findFolderPidl, 0, NULL);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return hr;
+
+ hr = pShellBrowser->BrowseObject(findFolderPidl, 0);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return hr;
+
+ CComPtr<IShellFolder> pShellFolder;
+ hr = GetSearchResultsFolder(*pShellBrowser, &hwnd, &pShellFolder);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return hr;
+
+ // Subscribe to search events
+ DWORD fAdviseCookie;
+ hr = AtlAdvise(pShellFolder, static_cast<IDispatch *>(this), DIID_DSearchCommandEvents, &fAdviseCookie);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return hr;
+ }
+
+ ::PostMessageW(hwnd, WM_SEARCH_START, 0, (LPARAM) pSearchStart.Detach());
+
+ SetSearchInProgress(TRUE);
+
+ return TRUE;
+}
+
+LRESULT CSearchBar::OnStopButtonClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ HWND hwnd;
+ HRESULT hr = GetSearchResultsFolder(NULL, &hwnd, NULL);
+ if (SUCCEEDED(hr))
+ ::PostMessageW(hwnd, WM_SEARCH_STOP, 0, 0);
+
+ return TRUE;
+}
+
+BOOL CSearchBar::GetAddressEditBoxPath(WCHAR (&szPath)[MAX_PATH])
+{
+ HWND hComboboxEx = GetDlgItem(IDC_SEARCH_COMBOBOX);
+ ::GetWindowTextW(hComboboxEx, szPath, _countof(szPath));
+ INT iSelectedIndex = SendMessageW(hComboboxEx, CB_GETCURSEL, 0, 0);
+ if (iSelectedIndex != CB_ERR)
+ {
+ WCHAR szItemText[MAX_PATH];
+ COMBOBOXEXITEMW item = {0};
+ item.mask = CBEIF_LPARAM | CBEIF_TEXT;
+ item.iItem = iSelectedIndex;
+ item.pszText = szItemText;
+ item.cchTextMax = _countof(szItemText);
+ SendMessageW(hComboboxEx, CBEM_GETITEMW, 0, (LPARAM)&item);
+
+ if (!wcscmp(szItemText, szPath) && SHGetPathFromIDListW((LPCITEMIDLIST)item.lParam, szPath))
+ {
+ return TRUE;
+ }
+ }
+
+ DWORD dwAttributes = GetFileAttributesW(szPath);
+ return dwAttributes != INVALID_FILE_ATTRIBUTES
+ && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
+}
+
+LRESULT CSearchBar::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+ INT iWidth = LOWORD(lParam);
+ INT iPadding = 10;
+
+ ((CWindow)GetDlgItem(IDC_SEARCH_LABEL)).SetWindowPos(NULL, 0, 0, iWidth - iPadding, 40, SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
+
+ int inputs[] = { IDC_SEARCH_FILENAME, IDC_SEARCH_QUERY, IDC_SEARCH_COMBOBOX, IDC_SEARCH_BUTTON, IDC_SEARCH_STOP_BUTTON, IDC_PROGRESS_BAR };
+ HDWP hdwp = BeginDeferWindowPos(_countof(inputs));
+ for (SIZE_T i = 0; i < _countof(inputs); i++)
+ {
+ CWindow wnd = (CWindow) GetDlgItem(inputs[i]);
+ RECT rect;
+ wnd.GetWindowRect(&rect);
+ POINT pt = { rect.left, rect.top };
+ ScreenToClient(&pt);
+ hdwp = wnd.DeferWindowPos(hdwp,
+ HWND_TOP,
+ iPadding,
+ pt.y,
+ iWidth - iPadding * 2,
+ rect.bottom - rect.top,
+ SWP_NOZORDER | SWP_NOACTIVATE);
+ }
+ EndDeferWindowPos(hdwp);
+
+ return 0;