如何在select-for-folder对话框中为静态控件的背景添加颜色?

时间:2013-02-04 00:01:31

标签: c++ winapi styles controls

我的意思是为标题栏下方的描述对话框着色。我成功地改变了字体,但背景完全不同。

当我到处读到它是通过捕获WM_CTLCOLORSTATIC消息来完成的,但是没有人在我应该捕获此消息的地方放置完整的代码,这段代码就像我理解的那样,我把它放到对话框的回调过程中。

问题是WM_CTLCOLORSTATIC永远不会被调用。

#include <windows.h>
#include <iostream>
#include <shlobj.h> //for Shell API, dir dialog
#include <commctrl.h>

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);
      break;
    }
    case WM_CTLCOLORSTATIC: {
      std::cout << "WM_CTLCOLORSTATIC fired!" << std::endl;
      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"ok, now how to make my background... yellow for example ?";
    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);
  }
  system("pause");
}

2 个答案:

答案 0 :(得分:1)

您可以使用SetWindowSubclass函数对对话框窗口进行子类化。

在指定的回调函数中,您将收到WM_CTLCOLORSTATIC条消息。

答案 1 :(得分:0)

MSDN documentation提到您传递给::SHBrowseForFolder的回调程序仅用于接收四种类型的事件:

  • BFFM_INITIALIZED
  • BFFM_IUNKNOWN
  • BFFM_SELCHANGED
  • BFFM_VALIDATEFAILE

这些事件都与WM_CTLCOLORSTATIC消息无关,该消息被发送到父窗口的窗口过程。