get style of a control

Hi,

I'm trying to format an edit control's style type into a nicely formatted string. I am doing something like:


map<CString, DWORD> mStyles;
mStyles["WS_BORDER"] = WS_BORDER;
mStyles["WS_CHILD"] = WS_CHILD;
// etc....

// Make a nice string rep of the style, like "WS_CHILD | WS_BORDER" ... etc
CString MakeStyleString(DWORD dw)
{
     CString strStyle = "";
     for (map<CString, DWORD>::iterator  it = mStyles.begin();
                                                             it != mStyles.end();
                                                             it++)
     {
            if (dw & it->second) {
                 strStyle += it->first + " | ";
            }
     }
    return strStyle;
}

int main()
{
    CEdit edit;
    edit.create(WS_BORDER | WS_CHILD | ES_MULTILINE...);
    AfxMessageBox(MakeStyleString(edit.GetStyle());
}

Ok so the above is a little pseudo-codeish, i know there are a few typos etc. But the function works MOST of the time, except when I start specifying styles like ES_MULTILINE. In this case, my function is saying there are styles in there that I had never put into the edit, like SS_BLACKFRAME and stuff - how did that get into the style? What am I doing wrong?

This is pretty urgent, any help would be great,

Thanks
LVL 7
minnirokAsked:
Who is Participating?
 
RichieHindleCommented:
Not all window styles have unique values - the values are only unique within the set of styles for a given window class.  So within the STATIC styles, SS_CENTER is guaranteed different from SS_RIGHT, and within the button styles, BS_DEFPUSHBUTTON is guaranteed different from BS_CHECKBOX.  But when you look at the actual values, you'll see that BS_DEFPUSHBUTTON == SS_CENTER == 1, and BS_CHECKBOX == SS_RIGHT == 2.

So your code needs to know not only the style of the window, but also its window class, and it needs to know which styles apply to which class.  Rather than a single mStyles map, you need one for each window class.
0
 
minnirokAuthor Commented:
Ahhh curses..

I'll give it a try
0
 
minnirokAuthor Commented:
Ok looks like it's working with specific maps.

But,

still my CStatic controls keep returning true on the following checks:

CStatic static;
static.Create(WS_CHILD | WS_VISIBLE);

if (static.GetStyle() & SS_BLACKFRAME) {
    AfxMessageBox("why....");
}

Any ideas? Is there some default style being applied I'm not aware of?

Thanks
0
 
RichieHindleCommented:
SS_BLACKFRAME is the value of the lower 5 bits of the style, rather than being a single bit.  Its value is 7, so if you have a static control of style SS_CENTER, whose value is 1, then (static.GetStyle() & SS_BLACKFRAME) will be true.  You need to do this:

if ((static.GetStyle() & SS_TYPEMASK) == SS_BLACKFRAME) {
    // It's a black frame
}

It's always helpful to look up the actual values of these styles in winuser.h, because it becomes obvious which ones are single bits, which ones belong in ranges of values, and what the masks are for those ranges.  Both my answers to your questions have come straight out of winuser.h.  8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.