CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Dec 2017
    Posts
    41

    Changing the color of elements - Win API

    I have some problems when working with Win32 api.

    1. Changing the text and background color of a button doesn't take effect.
    My code:

    Code:
    	case WM_CTLCOLORBTN:
                COLORREF colorref;
                colorref = RGB(0,0,255);
                HBRUSH hBrushBtn;
                hBrushBtn = CreateSolidBrush(colorref);
                return ((LRESULT)hBrushBtn);
            break;

    2. How do I create selectable text.
    My code:

    Code:
     text = CreateWindow("STATIC","text",WS_VISIBLE | WS_CHILD , 0,0,100,60, hwnd , NULL, NULL, NULL);
    3. How do I create a filled rectangle?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    1. You must use owner/custom draw for the button.
    2. You could use "EDIT" class with the style ES_READONLY instead.
    3. ???
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    I searched the internet , I don't seem to find any examples. Can you show me an example of this

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

  5. #5
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    For creating a filled rectangle I have this code:

    Code:
          hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
            rect = {0, 0, 500,500};
            brush = CreateSolidBrush(RGB(50, 151, 151));
            FillRect(hdc, &rect, brush);
            DeleteObject(brush);
    This goes in "case WM_CREATE:"

    The problem is that the rectangle is created at x = 0, y = 0 relative to the screen instead of the window

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    You can use ScreenToClient and/or ClientToScreen functions to convert between the screen and the window coordinates
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    I created a button with owner draw, but it doesn't display the button

    Code:

    Code:
      button = CreateWindow("BUTTON","Overview",WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 0,20,200,50, hwnd , (HMENU) 2 , NULL, NULL);
    Code:
     case WM_CTLCOLORBTN:
    {
    
                LPDRAWITEMSTRUCT Item;
                    Item = (LPDRAWITEMSTRUCT)lParam;
                    SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial Black"));
                    FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
                    SelectObject(Item->hDC, CreateSolidBrush(0));
                    if (Item->itemState & ODS_SELECTED)
                    {
                        SetTextColor(Item->hDC, 0);
                        SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
                        SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0xFF00));
                    }
                    else
                    {
                        SetTextColor(Item->hDC, 0x00FF00);
                        SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
    
                    }
                    SetBkMode(Item->hDC, TRANSPARENT);
                    RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
                    int len;
                    len = GetWindowTextLength(Item->hwndItem);
                    LPSTR lpBuff;
                    lpBuff = new char[len+1];
                    GetWindowTextA(Item->hwndItem, lpBuff, len+1);
                    DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
                }
            break;

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    You have to handle WM_DRAWITEM message, not a WM_CTLCOLORBTN one.
    See exampe in
    https://www.daniweb.com/programming/...-draw-controls
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    Thank you!

    I'm creating several "EDIT" elements with a for, when I move the window outside the screen and inside again the "EDIT" elements disappear along with a line in the WM_PAINT event


    The "create" event:
    Code:
    	case WM_CREATE:
            for( int i =0; i < 4; i++ ){
                text = CreateWindow("EDIT","text",WS_VISIBLE | WS_CHILD , 0,i*40+100,100,10, hwnd , NULL, NULL, NULL);
            }
    
           // Other elements
    	break;

    The "paint" event:
    Code:
     case WM_PAINT:
            {
                hdc = GetDC(hwnd);
                SetRect(&secondbackground, 0, 70, 800, 500);
                FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,200,200)) );
                ReleaseDC(hwnd, hdc);
    
                hdc = GetDC(hwnd);
                SetRect(&secondbackground, 0, 0, 800, 20);
                FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,100,200)) );
                ReleaseDC(hwnd, hdc);
    
                hdc = BeginPaint( hwnd, &ps );
                MoveToEx(hdc,1,52,NULL);
                LineTo(hdc,100,200);
                EndPaint( hwnd, &ps );
    
                hdc = GetDC(hwnd);
                SetPixel(hdc,300,300,RGB(255,255,255));
                EndPaint( hwnd, &ps );
            }
            break;

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    Quote Originally Posted by binary2 View Post
    Thank you!

    I'm creating several "EDIT" elements with a for, when I move the window outside the screen and inside again the "EDIT" elements disappear along with a line in the WM_PAINT event
    ...
    Perhaps, you paint your rects over the edit controls?
    Victor Nijegorodov

  11. #11
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    The paint event is under the create event

    My Window Procedure is structured like this:

    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)              
        {
            case WM_CREATE:
    
            for( int i =0; i < 4; i++ ){
                text = CreateWindow("EDIT","asd",WS_VISIBLE | WS_CHILD , 0,i*20+100,100,20, hwnd , NULL, NULL, NULL);
            }
    
            break;
    
            case WM_PAINT:
            {
             
                hdc = GetDC(hwnd);
                SetRect(&secondbackground, 0, 0, 800, 500);
                FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(255,255,255)) );
                ReleaseDC(hwnd, hdc);
    
                hdc = BeginPaint( hwnd, &ps );
                MoveToEx(hdc,1,52,NULL);
                LineTo(hdc,100,200);
                EndPaint( hwnd, &ps );
    
                hdc = GetDC(hwnd);
                SetPixel(hdc,300,300,RGB(255,255,255));
                EndPaint( hwnd, &ps );
            
            }
            break;
            case WM_CTLCOLORSTATIC:
               
            break;
            case WM_DRAWITEM:
                
            break;
            case WM_CTLCOLOREDIT:
               
            break;
            case WM_COMMAND:
    
                switch ( LOWORD(wParam) ){
                    case 1:
                     
                    break;
                    case 2:
                  
                    break;
                    case 3:
                      
                    break;
                    case 4:
            
                    break;
                }
    
    
            break;
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    Quote Originally Posted by binary2 View Post
    The paint event is under the create event

    My Window Procedure is structured like this:
    You mean the sequence of commands/operators in your C++ code.
    But it has nothing to with the Windows OS run-time!

    The WM_PAINT message is sent (from MSDN) "... to a window procedure when changes to the window have altered the content of the client area..."
    So every time you "move the window outside the screen and inside again" the WM_PAINT message is sent to a window procedure...
    Last edited by VictorN; July 20th, 2018 at 12:11 PM. Reason: link was added
    Victor Nijegorodov

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Changing the color of elements - Win API

    Code:
     hdc = GetDC(hwnd);
    You don't use GetDC() in WM_PAINT message. Consider

    Code:
                hdc = BeginPaint( hwnd, &ps );
                SetRect(&secondbackground, 0, 0, 800, 500);
                FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(255,255,255)) );
    
                MoveToEx(hdc,1,52,NULL);
                LineTo(hdc,100,200);
    
                SetPixel(hdc,300,300,RGB(255,255,255));
                EndPaint( hwnd, &ps );
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Dec 2017
    Posts
    41

    Re: Changing the color of elements - Win API

    Thank you!

    Is there a way to set a border color and style to a "static" control? I'm interested in applying a dotted border to the top and bottom of the control

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the color of elements - Win API

    Quote Originally Posted by binary2 View Post
    Is there a way to set a border color and style to a "static" control? I'm interested in applying a dotted border to the top and bottom of the control
    Hmm, interesting idea...
    I guess you should handle the WM_NCPAINT message
    Victor Nijegorodov

Page 1 of 2 12 LastLast

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

Featured