スタティックの背景について

解決


matsu  2006-05-18 01:54:00  No: 61857

OS:WindowsXP Pro SP2
VS.NET 2003

CDialogクラスで背景をグラデーション描画し、配置したスタティックの
文字はフォントを指定しています。グラデーションもテキストも
表示されるのですが、背景が透過しません。スタティックのプロパティを
変えたり、OnCtlColorを使ったりしましたがうまくいきません。
IDC_xxx_STATICのプロパティSimpleをTRUEにしています。
フォントを設定しなければ背景が透過するのですが...
ご指導、よろしくお願いします。
以下、ソースです。
xxx.h
UINT  nID;
CFont m_f;

BOOL CxxxDlg::OnInitDialog()
{
  CDialog::OnInitDialog();

  setFont();

  return TRUE;
}

HBRUSH CxxxDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  nID = pWnd->GetDlgCtrlID(); // IDの取得

  switch (nCtlColor)
  {
  case CTLCOLOR_STATIC:
    pDC->SetBkMode(TRANSPARENT); // 透過モード

    if(nID == IDC_xxx_STATIC)
    {
      pDC->SetTextColor(RGB(255, 0, 0));
    }
  }
  return hbr;
}

void CxxxDlg::OnPaint()
{
    CPaintDC dc(this); // device context for painting

    CRect rect;
    GetClientRect(&rect);

    int r, g, b;
    r = 0;
    g = 0;
    b = 255; // 青色  

    int TopHeight = (rect.bottom - rect.top) / 4;  // 純色とする部分
    int Zone = TopHeight / 10;                     // 色を変化させる単位

    int OrgBottom = rect.bottom;
    int OrgWidth  = rect.right - rect.left;
    int OrgHeight = OrgBottom - rect.top;
    rect.bottom    = rect.top + TopHeight;

    CBrush bluBrush;
    bluBrush.CreateSolidBrush(RGB(r,g, b));
    dc.FillRect(&rect, &bluBrush);

    rect.top    = rect.bottom;
    rect.bottom = rect.top + Zone;
    b   -= 8;        // 青色の濃度を変える
    while (rect.bottom < OrgBottom &&  b > 16)  {
            bluBrush.DeleteObject();

            bluBrush.CreateSolidBrush( RGB(r, g, b));
            dc.FillRect(&rect, &bluBrush); // 矩形の塗り潰し

            rect.top    = rect.bottom;
            rect.bottom = rect.top + Zone;
            b    -= 8; 
    }
    bluBrush.DeleteObject();

    bluBrush.CreateSolidBrush(RGB(r, g, b));
    rect.bottom = OrgBottom;
    dc.FillRect(&rect, &bluBrush); // 残りの矩形を塗り潰す
    bluBrush.DeleteObject();
}

int CxxxDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;

  this->ShowWindow(SW_SHOW);

  return 0;
}

void CxxxDlg::setFont()
{
  CDC* pDC = GetDC();
  CString f = _T("MS UI Gothic");

  m_f.CreateFont(28, 0, 0, 0,
    FW_NORMAL,                     // 太さ
    FALSE,                       // イタリック体
    FALSE,                       // アンダーライン
    FALSE,                       // 取り消し線
    DEFAULT_CHARSET,             // 文字セット
    OUT_DEFAULT_PRECIS,          // 出力精度
    CLIP_DEFAULT_PRECIS,         // クリッピング精度
    PROOF_QUALITY,               // 出力品質
    DEFAULT_PITCH | FF_DONTCARE, // ピッチとフォント・ファミリ
    f                            // フォントの書体
    );

  GetDlgItem(IDC_xxx_STATIC)->SetFont(&m_f);

  ReleaseDC(pDC);
}


matsu  2006-05-18 17:58:17  No: 61858

matsuです。

<OnCreate>
bluBrush.CreateSolidBrush(RGB(r, g, b));
    rect.bottom = OrgBottom;
    dc.FillRect(&rect, &bluBrush); // 残りの矩形を塗り潰す
    bluBrush.DeleteObject();

setFont();

でフォントの問題は解決しました。
また、
<OnCtlColor>
if(nID == IDC_xxx_STATIC)
{
   pDC->SetTextColor(RGB(255, 0, 0));
}
return static_cast<HBRUSH>(::GetStockObject(HOLLOW_BRUSH));

でSimple(FALSE)で透過しました。
スタティックの背景を理解せず投稿してしまい
申し訳ありませんでした。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加