0

我设法在此控件中绘制了一个矩形,但有两个问题。首先是在用鼠标移动选择文件夹对话框后显示白色矩形。我知道这里的问题是连接到正确的信息。我选择了在创建对话框窗口时触发的 WM_ERASEBKGND,但它没有任何效果,必须在之前未显示的控件部分返回屏幕时调用它,所以我必须将窗口拖到边缘,所以部分控件不可见并将其拖回,然后显示白色矩形。但是出现了第二个问题。它还涵盖了控件的文本。

所以这是我的尝试,有什么想法吗?

#include <windows.h>
#include <shlobj.h>

WNDPROC origStaticProc;
LRESULT CALLBACK myStaticProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
  switch (uMsg) {
    case WM_ERASEBKGND: {
      HDC dc = GetDC(hWnd);
      RECT clientRect;
      GetClientRect(hWnd,&clientRect);
      FillRect(dc, &clientRect, (HBRUSH)GetStockObject(WHITE_BRUSH));
      ReleaseDC(hWnd, dc);
      break;
    }
  }
  return CallWindowProc(origStaticProc, hWnd, uMsg, wParam, lParam );
}

int CALLBACK BrowseCallBackProc( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData) {
  switch(uMsg) {
    case BFFM_INITIALIZED: {
      HWND static_control = NULL;
      char szClassName[_MAX_PATH];
      for (HWND hChild = GetWindow(hwnd, GW_CHILD); hChild != NULL; hChild =  GetNextWindow(hChild, GW_HWNDNEXT))
      {
        if ((GetWindowLong(hChild, GWL_STYLE) & WS_VISIBLE) == 0) continue;
        GetClassName(hChild, szClassName, _countof(szClassName));
        if (!strcmp("Static",szClassName)) {
          static_control = hChild;
          break;
        }
      }
      HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Fixedsys"));
      SendMessage(static_control, WM_SETFONT, (WPARAM)hFont, TRUE);
      origStaticProc = ( WNDPROC ) SetWindowLongW( static_control, GWL_WNDPROC,( LONG ) myStaticProc );
      break;
    }
  }
}

int main() {
  using namespace std;
  BROWSEINFOW bi;
  LPITEMIDLIST pidl;
  LPMALLOC pMalloc;
  if (SUCCEEDED (::SHGetMalloc (&pMalloc))) {
    ::ZeroMemory (&bi,sizeof(bi));  
    bi.hwndOwner = NULL;
    bi.lpszTitle = L"I should be visible on a white background. Now you must drag me to edge of the screen and back.";
    bi.pszDisplayName = 0;
    bi.pidlRoot = 0;
    bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_VALIDATE | BIF_USENEWUI | BIF_UAHINT;
    bi.lpfn = BrowseCallBackProc;
    bi.lParam = (LPARAM)L"d:\\";
    pidl = ::SHBrowseForFolderW(&bi);
  }
}

它的样子:

它看起来如何

它当然应该是这样的:

当然应该如何

4

1 回答 1

0

知道了。问题是我不知道用于 SHBrowseForFolder 的回调函数与窗口本身的回调不同。结果我必须做另一个函数来子类化对话框。

这是丑陋和不安全的代码,但它编译并说明了原理。

#include <windows.h>
#include <shlobj.h>

WNDPROC origBffProc;
LRESULT CALLBACK bffProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
  static HBRUSH hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
  switch (uMsg) {
    case WM_CTLCOLORSTATIC: {
      HDC hdcStatic = (HDC) wParam;
      SetTextColor(hdcStatic, RGB(255,0,0));
      SetBkColor(hdcStatic, RGB(255,255,0));
      return (INT_PTR)hBrush;
    }
  }
  return CallWindowProc(origBffProc, hWnd, uMsg, wParam, lParam );
}

int CALLBACK BrowseCallBackProc( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData) {
  switch(uMsg) {
    case BFFM_INITIALIZED: {
      HWND static_control = NULL;
      char szClassName[_MAX_PATH];
      for (HWND hChild = GetWindow(hwnd, GW_CHILD); hChild != NULL; hChild =  GetNextWindow(hChild, GW_HWNDNEXT))
      {
        if ((GetWindowLong(hChild, GWL_STYLE) & WS_VISIBLE) == 0) continue;
        GetClassName(hChild, szClassName, _countof(szClassName));
        if (!strcmp("Static",szClassName)) {
          static_control = hChild;
          break;
        }
      }
      HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Fixedsys"));
      PostMessage(static_control, WM_SETFONT, (WPARAM)hFont, TRUE);
      origBffProc = ( WNDPROC ) SetWindowLongW( hwnd, GWL_WNDPROC,( LONG ) bffProc );
      break;
    }
  }
}
int main() {
  using namespace std;
  BROWSEINFOW bi;
  LPITEMIDLIST pidl;
  LPMALLOC pMalloc;
  if (SUCCEEDED (::SHGetMalloc (&pMalloc))) {
    ::ZeroMemory (&bi,sizeof(bi));  
    bi.hwndOwner = NULL;
    bi.lpszTitle = L"You are about to erase a directory, be careful! (and don't blame me)";
    bi.pszDisplayName = 0;
    bi.pidlRoot = 0;
    bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_VALIDATE | BIF_USENEWUI | BIF_UAHINT;
    bi.lpfn = BrowseCallBackProc;
    bi.lParam = (LPARAM)L"d:\\";
    pidl = ::SHBrowseForFolderW(&bi);
  }
}
于 2013-02-05T23:28:18.477 回答