Results 1 to 23 of 23

Thread: [Resolved]I don't like the colour Grey

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46

    Unhappy [Resolved]I don't like the colour Grey

    Hi ALL,

    I am looking for some help with the following. I sure hope this is the
    right place. :-)

    I have used the following code to create myself a STATIC control
    only the background of the control is in grey. This would not
    normally be a problem I know, but I just don’t like the colour grey
    and would prefer if the STATIC control's background could be
    red. It was also be nice if I could make that font larger so that I
    could put my magnifier away. Any help/solutions to these
    problems will be gratefully received.

    Code:
    hwndc = CreateWindowEx(
              NULL,                  /* extended style, not needed */
              "STATIC",              /* class identifier */
              "A CONTROL",           /* window title */
              WS_CHILD | SS_CENTER,  /* parameters */
              0, 0, 264, 30,         /* initial position, size */
              hwnd,                  /* handle to parent */
              NULL,                  /* handle to menu (none) */
              hInstance,             /* application instance handle */
              NULL );                /* who needs it? */
    
    ShowWindow(hwndc, SW_SHOW);
    Thanks in advance,

    ISquishWorms
    Last edited by ISquishWorms; Oct 8th, 2002 at 10:01 AM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    wcl.hbrBackground = CreateSolidBrush(RGB(255,0,0));
    You should have a line somewhere with hbrBackground in it. Just change it to look like the above.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks The Hobo for your quick responce. Whilst I have registered a class for my main Window using
    the following code, I did not bother doing the same for my STATIC control. Do I need to register a
    class for my control as well?

    Code:
    	WNDCLASSEX winmainClass;	/* declare structure variable */
    	HWND hwnd;
    	MSG Msg;
    
    	HWND hwndc;
    
    	/* Step 1: Registering the Window Class */
    	winmainClass.cbSize		=	sizeof(WNDCLASSEX);			/* always use this! */
    	winmainClass.style		=	CS_DBLCLKS | CS_OWNDC |
    						CS_HREDRAW | CS_VREDRAW;		/* standard settings */
    	winmainClass.lpfnWndProc	=	WndProc;				/* we need to write this! */
    	winmainClass.cbClsExtra		=	0;					/* extra class info, not used */
    	winmainClass.cbWndExtra		=	0;					/* extra window info, not used */
    	winmainClass.hInstance		=	hInstance;				/* parameter passed to WinMain() */
    	winmainClass.hIcon		=	LoadIcon(NULL, IDI_WINLOGO);		/* Windows logo */
    	winmainClass.hCursor		=	LoadCursor(NULL, IDC_ARROW);		/* standard cursor */
    	winmainClass.hbrBackground	=	(HBRUSH)GetStockObject(BLACK_BRUSH);	/* a simple black brush */
    	winmainClass.lpszMenuName	=	NULL;					/* no menu */
    	winmainClass.lpszClassName	=	"WinMain Class";			/* class name */
    	winmainClass.hIconSm		=	LoadIcon(NULL, IDI_WINLOGO);		/* Windows logo again */
    
        if(!RegisterClassEx(&winmainClass))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    Thanks

    ISquishWorms

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Just add this line:

    Code:
    winmainClass.hbrBackground = CreateSolidBrush(RGB(255,0,0));
    Under the "winmainClass.hIconSm" line.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks again for replying ‘The Hobo’.

    I added the line of code as you suggested under the
    "winmainClass.hIconSm" line. It appears to
    work except that it seems to change the colour of my
    main application window and not my STATIC control which is a
    WS_CHILD of my application window. I believe this is because I
    use ‘winmainClass’ for my application window and not the
    WS_CHILD STATIC control. So what I am asking is do I have to
    duplicate ‘winmainClass’ (obviously give the class a different
    name) for My STATIC control and then add the line to that?

    Thanks,

    ISquishWorms

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Oh. I completely missed what you were talking about.

    Honestly, I really don't know.

    There's probably a way using SendMessage, but I'm not sure.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks ‘The Hobo’ for spending the time to try and help anyway, it
    was much appreciated. Just to let you know that your suggested
    solution was not a waste of time as I now know how to colour
    my main application window, so I have learnt something thanks.

    Is there anyone else who can help me with my original question(s)?

    Thanks

    ISquishWorms

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Static controls send their parent windows a WM_CTLCOLORSTATIC message every time they repaint. Catch this message and manipulate the colors of the HDC that is passed with it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks CornedBee for the advice, I will give it ago and let you know.

    ISquishWorms

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    OK, this is what I have. Now how do I actually change the colour
    of my STATIC control? I have tried using SetBkColor with
    no success. I will also have more than one STATIC control on my
    application window how do I distinguish between them?

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CTLCOLORSTATIC:
            // What do I need to put in here please help, thanks.
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    Thanks,

    ISquishWorms

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static HBRUSH brStaticBack = NULL;
        if(!brStaticBack)
            brStaticBack = CreateSolidBrush(RGB(255,0,0));
        switch(msg)
        {
            case WM_CTLCOLORSTATIC:
                {
                    HDC hdcStatic = (HDC)wParam;
                    SelectObject(hdcStatic, brStaticBack);
                }
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                DeleteObject(brStaticBack);
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks for the example code ' CornedBee'. I have copied it into
    my project but must be doing something stupid (I am fairly new to
    programming in C++ for windows although fairly experienced in C
    for DOS/Console) as my STATIC control remains its grey colour. I
    have used breakpoints to ensure that the new code is being
    executed and it appears to be, but the colour still remains grey. I
    would be extremely grateful if you could point out where I am
    being stupid.

    Thanks

    ISquishWorms

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I haven't verified my code, it may be faulty. Make sure to call SetBkColor as well. If that doesn't work I will do additional testing (as I hate it when my answers don't work ).

    As a rule in forum posts you write nicknames as if they were real names, don't put them between quotes, and nobody cares if you abbreviate them. So for exmaple you might write CornedBee, Corned, Bee or CB.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, and in your profile settings you can add a signature, so you don't have to write it yourself everytime.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    Thanks for the forum tips and your reply CornedBee. I have updated my profile so hopefully I will not need to type my signature again, well at least until I wish to update it.

    As for the code although you say you have not verified it I am sure that it works and that it is more likely a mistake that I have made being new to this windows programming lark. I copied your example exactly and so did not include the SetBkColor call. I will now revisit the code that I have and try and work out where I need to place this call and what parameters to give it.

    Thanks again, I will let you know if I have any success. :-/
    ISquishWorms

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    I have the following code now and yet still I can not get my STATIC control to change colour. Sorry to be a pain but I would be grateful if you or someone can scan over it and tell me what I have done wrong.

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static HBRUSH brStaticBack = NULL;
        
        if(!brStaticBack)
            brStaticBack = CreateSolidBrush(RGB(255,0,0));
    
        switch(msg)
        {
            case WM_CTLCOLORSTATIC:
            {
                HDC hdcStatic = (HDC)wParam;
                SelectObject(hdcStatic, brStaticBack);
                SetBkColor(hdcStatic, RGB(255,0,0));
            }
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    Thanks.
    ISquishWorms

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    My fault. Instead of SelectObject, do

    return (LRESULT)brStaticBack;
    This must be the last line of that particular message handler (instead of the break)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46

    Thumbs up

    YES!! – I am now one happy little worm. (I know simple things, but hey I am a simple little critter.)

    Thanks for your patience and time CornedBee, your provided solution works a treat. I now have a nice red STATIC control instead of a horrible grey one. I wonder if you would care to spend a little more time and explain/comment your lines of code for me, as I am new to windows programming and could do with every bit of help I can get. Once I have fully understood your solution I am off to investigate how I can change the font size and type of my STATIC control.
    ISquishWorms

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No problem. I also changed the layout of the WndProc to my style, it has a few advantages over yours.
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    // A static variable keeps it's value between
    // function calls, which is what we want here
        static HBRUSH brStaticBack = NULL;
        
    // If the brush was not created yet create it
    // In particular create a solid (no pattern)
    // brush with the color red
        if(!brStaticBack)
            brStaticBack = CreateSolidBrush(RGB(255,0,0));
    
        switch(msg)
        {
    // You must handle WM_PAINT at least with 
    // these few lines, else your window will
    // receive WM_PAINT messages forever
            case WM_PAINT:
            {
                PAINTSTRUCT ps;
                BeginPaint(hwnd, &ps);
                EndPaint(hwnd, &ps);
                return 0;
            }
            case WM_CTLCOLORSTATIC:
            {
    // The HDC the control uses to paint
    // is passed in wParam. Manipulate it
    // at will.
                HDC hdcStatic = (HDC)wParam;
    // Set the text background color
                SetBkColor(hdcStatic, RGB(255,0,0));
    // Return the brush with which to fill the
    // background. Because the function exits
    // with this statement we have no chance
    // to delete the brush, which is why we create
    // it once and store it in a static variable
                return (LRESULT)brStaticBack;
            }
            case WM_CLOSE:
                DestroyWindow(hwnd);
                return 0;
            case WM_DESTROY:
    // Only now we can get rid of the brush in
    // order to avoid a resource leak.
                DeleteObject(brSaticBack);
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    The layout change has the main advantage that you can handle a message and call DefWindowProc anyway (there are situations where you want to do that). Also you can control the return values of the different handlers better.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46
    CornedBee thanks for taking the time to comment/explain your code it was very useful to me being a newbie windows programmer. I have adjusted my WndProc function so that it is like yours as your WndProc function clearly had advantages over mine.

    As you appear to me to be one of the C++ experts around here I wonder if I can ask you just to have a quick look at my WinMain function below. I have a nasty feeling that this could be greatly improved upon too. I am creating my STATIC controls within the WinMain function not sure if this is the best place maybe I should be creating them in [WndProc] after trapping the WM_CREATE message. Also I am creating three STATIC controls all of which are identical except theirwindow title and y positioning., maybe there is a quicker/tidier(cleaner) way to do this, as if I carry on down this route my WinMain function has the potential to become quite messy and large.

    Also I was wondering if you can recommend any books/reference material which may help me get up to speed with windows programming.

    As always thanks, for kindly giving up some of your time to help others (I think I can speak for the many here who you are only happy to help).


    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX winmainClass;	/* declare structure variable */
    	HWND hwnd;
    	MSG Msg;
    
    	HWND hwndCTL1;
    	HWND hwndCTL2;
    	HWND hwndCTL3;
    
    	/* Step 1: Registering the Window Class */
    	winmainClass.cbSize		=	sizeof(WNDCLASSEX);		/* always use this! */
    	winmainClass.style		=	CS_DBLCLKS | CS_OWNDC |
    						CS_HREDRAW | CS_VREDRAW;	/* standard settings */
    	winmainClass.lpfnWndProc	=	WndProc;			/* we need to write this! */
    	winmainClass.cbClsExtra		=	0;				/* extra class info, not used */
    	winmainClass.cbWndExtra		=	0;				/* extra window info, not used */
    	winmainClass.hInstance		=	hInstance;			/* parameter passed to WinMain() */
    	winmainClass.hIcon			=LoadIcon(NULL, IDI_WINLOGO);	/* Windows logo */
    	winmainClass.hCursor		=	LoadCursor(NULL, IDC_ARROW);	/* standard cursor */
    	winmainClass.hbrBackground	=	(HBRUSH)GetStockObject(BLACK_BRUSH);	/* a simple black brush */
    	winmainClass.lpszMenuName	=	NULL;					/* no menu */
    	winmainClass.lpszClassName	=	"WinMain Class";			/* class name */
    	winmainClass.hIconSm		=	LoadIcon(NULL, IDI_WINLOGO);		/* Windows logo again */
    
    	if(!RegisterClassEx(&winmainClass))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	/* Step 2: Creating the Window */
    	hwnd = CreateWindowEx(	NULL,				/* extended style, not needed */
    				"WinMain Class",		/* class identifier */
    				"Control Example",		/* window title */
    				WS_OVERLAPPED | WS_VISIBLE,	/* parameters */
    				0, 0, 270, 160,			/* initial position, size */
    				NULL,				/* handle to parent (the desktop) */
    				NULL,				/* handle to menu (none) */
    				hInstance,			/* application instance handle */
    				NULL	);			/* who needs it? */
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	hwndCTL1 = CreateWindowEx(	NULL,			/* extended style, not needed */
    					"STATIC",		/* class identifier */
    					"STATIC CTL 1",		/* window title */
    					WS_CHILD | SS_CENTER,	/* parameters */
    					0, 0, 264, 30,		/* initial position, size */
    					hwnd,			/* handle to parent */
    					NULL,			/* handle to menu (none) */
    					hInstance,		/* application instance handle */
    					NULL	);		/* who needs it? */
    
    	SetFont(hwndCTL1, 20, "Arial");
    	ShowWindow(hwndCTL1, SW_SHOW);
    
    	hwndCTL2 = CreateWindowEx(	NULL,			/* extended style, not needed */
    					"STATIC",		/* class identifier */			
    					"STATIC CTL 2",		/* window title */
    					WS_CHILD | SS_CENTER,	/* parameters */
    					0, 30, 264, 30,		/* initial position, size */
    					hwnd,			/* handle to parent */
    					NULL,			/* handle to menu (none) */
    					hInstance,		/* application instance handle */
    					NULL	);		/* who needs it? */
    
    	SetFont(hwndCTL2, 20, "Arial");
    	ShowWindow(hwndCTL2, SW_SHOW);
    
    	hwndCTL3 = CreateWindowEx(	NULL,			/* extended style, not needed */
    					"STATIC",		/* class identifier */
    					"STATIC CTL 3",		/* window title */
    					WS_CHILD | SS_CENTER,	/* parameters */
    					0, 60, 264, 30,		/* initial position, size */
    					hwnd,			/* handle to parent */
    					NULL,			/* handle to menu (none) */
    					hInstance,		/* application instance handle */
    					NULL	);	
    
    	SetFont(hwndCTL3, 20, "Arial");
    	ShowWindow(hwndCTL3, SW_SHOW);
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	/* Step 3: The Message Loop */
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    }
    ISquishWorms

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You indeed should place CreateWindow calls for subwindows in the WM_CREATE handler. I don't know why, but Charles Petzold teaches it this way.

    You don't need to use CreateWindowEx when creating a window without extended styles. CreateWindow works just as well.

    You also usually create child windows with the WS_VISIBLE style and omit the ShowWindow call for them, as ShowWinodw is only needed when you want to pass special arguments (such as the nCmdShow parameter).

    The HMENU parameter has a special meaning when you create a child window. You usually pass a constant integer there, which is the window id, a number that identifies that window (much like the HWND, but it is not necessarily unique and has a meaning only in the context of it's parent window, and it is known, as you pass it). In order to avoid a compilation error you must explicitly cast it:
    (HMENU)ID_STATIC1
    where ID_STATIC1 could be
    const int ID_STATIC1 = 1;
    or
    #define ID_STATIC1 1
    Usually you assign one id to all static controls, and give each active control (such as buttons) a unique id.

    Write the prototype of WinMain like this:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE,
    LPSTR lpCmdLine, int nCmdShow)

    The hPrevInstance parameter is always NULL and never used, so it makes sense not to assign a name to it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    46

    [Resolved]I don't like the colour Grey

    Well thanks once more for all your help CornedBee. All of your answers to my questions have been helpful, concise and well thought out. I guess I know who to ask if I have any further questions about programming in windows.

    Having already taken up quite a bit of your time I will now end this forum topic so that you can use your time elsewhere as I am sure you are a busy person judging by the number of replies made by yourself on this board. No doubt though I will in the fairly near future have some more questions which I hope you won’t mind looking at.

    Cya for now,
    ISquishWorms

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Glad I could help. Actually I'm not very busy, else I would spend less time here
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width