Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

2 windows with diffrent colors

Hi,

i have two winows in my win32 api application. How can i make them to be of diffrent colors?

Here is the code i tried to use but it doesnt works

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd1 = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd1)
{
return FALSE;
}

SetClassLong(hWnd1, GCL_HBRBACKGROUND, (COLOR_WINDOW-3));
ShowWindow(hWnd1, nCmdShow);
UpdateWindow(hWnd1);

hWnd2 = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd2)
{
return FALSE;
}

SetClassLong(hWnd2, GCL_HBRBACKGROUND, (COLOR_WINDOW+4));
ShowWindow(hWnd2, nCmdShow);
UpdateWindow(hWnd2);


return TRUE;
}

Thanx :-)

Comments

  • MT2002MT2002 Member Posts: 1,444
    : Hi,
    :
    : i have two winows in my win32 api application. How can i make them
    : to be of diffrent colors?

    When a window is created, just get the DC of that window, and call SetBkColor(). This is one method, anyways:
    [code]
    // sets the window, hwnd, background color to black
    SetBkColor (GetDC (hwnd), RGB(0,0,0));
    [/code]

    The other is, when building up the WNDCLASS(EX) structure, set the hbrBackground member to the background brush that you want to use. Or, set it afterwords using SetClassLongPtr() or similar.

    [code]
    // When creating the window class, this sets background color to black
    WNDCLASS w;
    w.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);

    // This sets the same member shown above if the window has already been
    // created. hwnd is the window handle. This also sets it to black

    SetClassLongPtr (GetDC (hwnd), HBRBACKGROUND, GetStockObject (BLACK_BRUSH));
    [/code]

    You can also simply render a filled rectangle to the size of the window :)

    There are many possibilities...
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • bodhi2016bodhi2016 Member Posts: 50
    Well i am new to win32 programming .I haven't learnt muchand i am really confused.I know how to create a single window.Now i am also trying to create two windows of diff. colour.But what i want to ask is that if i want to create two windows should it have two different class name.And what is a TCHAR variable.Like in a book i saw, static TCHAR sz_appname[]="my win class"-should this be different for both the windows.Please i am a learner dont get irritated by my amatuer type questions.





    Thank you
  • MT2002MT2002 Member Posts: 1,444
    Both windows will require their own class name. They can either share the same window procedure, or (if you want them to be treated as two separate windows) their own window procedure.

    TCHAR is of Type Character. it is just a typedef of char data type for portability. Alot of APIs (Like Win32) redefine all basic data types to better support wide characters and data type sizes.

    (ps: Sorry for the late reply. I would have replied sooner if you had posted a separate thread :) I didnt see this until now )

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]
    If both windows are the same functionality - just color is different - simply attach (using SetWindowLong() API) the COLORREF which you will be painting with to the HWND. Then, when WM_ERASEBKGND is received - get the color from HWND (using GetWindowLong() API) and paint background. All this may be done with one window class.
    [/color]
Sign In or Register to comment.