Wow that is interesting. DrawItem never gets called when SS_RIGHT is
specified. I haven't been able to find anything on this.
As a work-around, you can do the same thing in OnPaint without ownerdraw.
AliR.
"AliR \(VC++ MVP\)" <AliR@online.nospam> wrote:
Can you post the entire CMyStatic class? It looks like the problem is
in there!
I was hoping that was the case, but I don't think that's possible.
Below is code that reproduces the problem. When run, the code draws a
red rectangle, a green rectangle, and that's it. The third one doesn't
draw in blue or black -- it just doesn't draw at all. But the Create()
call does not fail. If I set a breakpoint inside the "case" statement
for SS_RIGHT, it never breaks. The "case" statements for both SS_LEFT
and SS_CENTER are both hit repeatedly.
----- MyStatic.h -----
#pragma once
#include "afxwin.h"
class CMyStatic : public CStatic
{
public:
CMyStatic();
~CMyStatic();
protected:
DWORD m_static_style;
void PreSubclassWindow();
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
----- MyStatic.cpp -----
#include "StdAfx.h"
#include "CMyStatic.h"
CMyStatic::CMyStatic()
{
m_static_style = SS_CENTER;
}
CMyStatic::~CMyStatic()
{
}
void CMyStatic::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
m_static_style = GetStyle() & SS_TYPEMASK;
ModifyStyle(0, SS_OWNERDRAW);
}
void CMyStatic::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);
switch (m_static_style)
{
// If SS_LEFT is specified, draw red
case SS_LEFT:
dc->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255, 0, 0));
break;
// If SS_CENTER is specified, draw green
case SS_CENTER:
dc->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 255, 0));
break;
// If SS_RIGHT is specified, draw blue
case SS_RIGHT:
dc->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 255));
break;
// Anything else, draw black
default:
dc->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 0));
break;
}
}
----- Code in the parent dialog class -----
// The rest of this class is just the automatically generated
// code from the MFC app wizard, aside from the addition of
// the declarations of m_label_1, m_label_2, and m_label_3.
void CMyStaticDlg::OnCreateStatics()
{
m_label_1.Create("1", WS_CHILD | WS_VISIBLE | SS_LEFT,
CRect(0, 10, 100, 25), this);
m_label_2.Create("2", WS_CHILD | WS_VISIBLE | SS_CENTER,
CRect(0, 30, 100, 45), this);
m_label_3.Create("3", WS_CHILD | WS_VISIBLE | SS_RIGHT,
CRect(0, 50, 100, 65), this);
}