Example code:

HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
                  const RECT& rc,const int id,const ustring& caption)
{
	dwStyle|=WS_CHILD|WS_VISIBLE;
	return CreateWindowEx(0,                            
                      _T("button"),             
                      caption.c_str(),           
                      dwStyle,                 
                      rc.left,                  
                      rc.top,             
                      rc.right,            
                      rc.bottom,            
                      hParent,
                      reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
                      hInst,                        
                      0);                         
}

The part I dont get is:

dwStyle|=WS_CHILD|WS_VISIBLE;

So the question would be why do we use '|' and whats it for? I've seen it being used in many other places, doest it even belong to C++? Something like '||' is obvious but cant figure out '|'.

| is "bitwise OR". |= is bitwise-or and an assignment operator, just like +=, -=, %=, etc.

Very often you'll have "boolean" type flags. My guess is that that is what WS_CHILD and WS_VISIBLE are. "ORing" them sets them as true, so I imagine ORing the WS_VISIBLE option means "make it visible". To make it INvisible, you might AND the complement.

dwStyle |= WS_VISIBLE; // make visible
dwStyle &= ~WS_VISIBLE; // make invisible

// test for visiblity
if(dwStyle & WS_VISIBLE)
{
    // it's visible!
}

Ok, i know i already set this thread as solved but there's acutally one more thing, so now i have an understanding of the bitwise operators but the part i dont fully get again consists out of this line:

dwStyle|=WS_CHILD|WS_VISIBLE;

So we mash all of these binary codes together (according to the | operator ofcourse) or something and then the windows libs can make sense out of it?

I've tryed doing a simple program just to see the results of these operators with just True or False values, but in result i only got True or False, so i figured that its not the same in the code above.

Use the & operator to find out if a certain flag has been set, for example

if( dwStyle & WS_CHILD)
{
   // do something
}

An example might help...

#include <iostream>
using namespace std;


#define MALE_MASK 0x01
#define ADULT_MASK 0x02
#define AMERICAN_MASK 0x04


void print(int flags)
{
    cout << "Flags : " << flags << endl;
    cout << "Male : ";
    if(flags & MALE_MASK)
    {
        cout << "True\n";
    }
    else
    {
        cout << "False\n";
    }
    cout << "Adult : ";
    if(flags & ADULT_MASK)
    {
        cout << "True\n";
    }
    else
    {
        cout << "False\n";
    }
    cout << "American : ";
    if(flags & AMERICAN_MASK)
    {
        cout << "True\n";
    }
    else
    {
        cout << "False\n";
    }
}


int main()
{
	// Set flags to some combinations
    int flags = MALE_MASK | AMERICAN_MASK;
    print(flags);
    flags &= ~MALE_MASK; // female
    print(flags);
    flags |= ADULT_MASK; // Adult
    print(flags);
    flags |= ADULT_MASK; // no change since flag is already set.
    print(flags);
    flags &= ADULT_MASK; // non-American
    print(flags);
    flags = 0; // female, child, non-American
    print(flags);
    flags |= MALE_MASK | ADULT_MASK; // male, adult, still not American.
    print(flags);
    return 0;
}

Note the choice of masks. One of them is 2 to the 0th power, one is 2 to the 1st power, one is 2 to the 2nd power. That way there is no overlap between masks so they can be OR'd and AND'd independently.

Awesome example, it appears that i misunderstood what a flag was, i thought it was a simple boolean. By the way, a quick question, what exactly does the 0x01 and so on part mean? I can see that it acts as a value and they can add up.

>> what exactly does the 0x01 and so on part mean? I can see that it acts as a value and they can add up.

0x01 is 2 to the 0 power, 0x02 is 2 to the 1st power, 0x04 is 2 to the 2nd power.

The fact that I picked 0, 1, and 2 as the powers is irrelevent. You could pick 5, 7, and 12 as the powers just as easily and it would work just the same. What is important is that all of the masks must be non-zero and all must be 2 to some power (2 because this is binary), and all must be different.

The end result is that in a 32-bit system, you can have an integer hold the equivalent of a boolean array of size 32. Thus you could add up to 29 more flags. The example I gave is the equivalent of a boolean array of 3...

bool flags[3];

where flags[0] stores whether the person is male, flags[1] stores whether the person is an adult and flags[2] stores whether the person is American. Note that the index is the exponent of of the mask, as discussed above (0, 1, and 2).

A quick check of MSDN confirms my suspicion that this is what is being done in the example you posted. These are property flags defined by Microsoft, which provides them to you so you can simply manipulate an integer instead of keeping track of a boolean array. It's easier and saves memory.

http://msdn.microsoft.com/en-us/library/system.windows.forms.nativewindow.handle.aspx

// Constant values were found in the "windows.h" header file.
    private const int WS_CHILD = 0x40000000,
                      WS_VISIBLE = 0x10000000,
                      WM_ACTIVATEAPP = 0x001C;

WS_CHILD and WS_VISIBLE are both powers of 2.

WM_ACTIVATEAPP appears to not be a power of 2. My guess that it itself is an ANDing of 0x10, 0x08, and 0x04, and that they are defined somewhere else and mean whatever they mean.

So it's all a matter of combining a bunch of boolean flags into one integer and using the =, &, |, and ~ operators to set, test, and extract what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.