在没有WM_CTLCOLORSTATIC的select-for-folder对话框中着色静态控件,该对话框窗口没有收到

时间:2013-02-05 17:22:12

标签: c++ winapi controls

我设法在此控件中绘制一个矩形,但有两个问题。首先是用鼠标移动选择文件夹对话框后显示白色矩形。我知道这里的问题是连接到正确的消息。我选择了在创建对话框窗口时触发的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);
  }
}

它的样子:

how it looks like

当然应该如何:

how it of course should be

1 个答案:

答案 0 :(得分:0)

知道了。问题是我不知道用于SHBrowseForFolder的回调函数与窗口本身的回调不同。它出来我必须做另一个函数来继承对话框。

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

#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);
  }
}