No announcement yet.

Changing a control's color !

  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing a control's color !


    Anyone could help me ?
    I created a dialog using ResEdit/RC. I've managed to change the text and font of the created static controls in it, ie. labels, but I'm having difficulties
    in changing the color of them.
    I know that I have to use the API functions "SetBkColor" and "SetTextColor", but I do not know how to use them.

    LOCAL hFont AS LONG ' Handle for font
    LOCAL hControl AS LONG ' Handle for control
    LOCAL hWnd AS LONG ' Handle for window
    LOCAL hDC AS DWORD ' Handle for device context
    LOCAL szText AS ASCIIZ * 50
    ' Changing font... Works fine !
    hFont = PBFormsMakeFont("Courier", 9, %FW_EXTRABOLD, %FALSE, %FALSE, %FALSE, %DEFAULT_CHARSET)    		
    SendMessage hControl, %WM_SETFONT, hFont, 0&
    ' Changing caption... Works fine !
    szText = "Test"
    SetWindowText(hControl, szText)
    ' What I'm trying... Didn't work completely right !...
    ' This is the code of my callback procedure
      	SELECT CASE GetDlgCtrlId(lParam)
      	    CASE %IDC_VISOR5
          		SetBkColor wParam, %RED
          		SetTextColor wParam, %WHITE
          		SetBkMode wParam, %OPAQUE
                              ' The objective is to change the background in the hole control to RED ! Now I'm getting only the text to the colors I needed. Not the hole background !
                              ' Don't know which object to use ! Tried BLACK_PEN, BLACK_BRUSH, DC_BRUSH, DC_PEN, neither one worked as I expected.
          		FUNCTION = GetStockObject(%WHITE_PEN) 
          		EXIT FUNCTION
          	END SELECT
    Anyone could point me in the right direction ?
    Thanks !
    Last edited by Gilmar de Paula Fiocca; 29 Sep 2008, 11:03 PM.

  • #2
    Which PBCC compiler version are you using? IOW Doesn't this post belong in the PBWin or Programming forum?
    Rick Angell


    • #3
      I don't know if this is what you are looking for, but here's an example generated by Jaxgui that should work in PBCC.

      #CONSOLE OFF ' Delete if using older than PBCC4.03
       #COMPILE EXE 
       #DIM ALL 
       GLOBAL hInstance, hwnd AS LONG
       FUNCTION ProcessWindow (BYVAL hDlg AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam _ 
                        AS DWORD, BYVAL lParam AS LONG) AS LONG
         STATIC hStBrush1 AS LONG
         LOCAL wmID AS LONG, wmEvent AS LONG 
         LOCAL BrushClr AS LONG        
         LOCAL hDC, hBrush, hPen AS DWORD  
         LOCAL StBgColor1 AS LONG, StFgColor1 AS LONG
         LOCAL Lt1 AS STRING
          STbgColor1 = &H0000FF     ' Selected background color for a Static control
          STfgColor1 = &H80FFFF     ' Selected foreground color for a Static control
           CASE %WM_CREATE         'Create and initialize Controls 
           hSTbrush1  =  CreateSolidBrush(STbgColor1)' Create a logical brush with selected color for a Static/Label control
              Lt1=Lt1 + "Now is the time for all good"
               CreateWindowEx(0, "static", BYVAL STRPTR( LT1 ), _ 
                   %WS_CHILD OR %WS_VISIBLE, _
                  80,  76,  290,  171, _   
                  hdlg,  1001, hInstance, BYVAL %NULL)    
              SetWindowPos hDlg, %HWND_TOP, 0, 0, 0, 0, %SWP_NoSize OR %SWP_NoMove
              IF GetDlgCtrlId(lParam) =  1001 THEN      '  Check for Static controls ID number
                 SetTextColor Wparam, STfgColor1       '  Set the foreground colors for a Static control
                 SetBkColor Wparam, STbgColor1         '  Set the background colors for a Static control
                 ProcessWindow = hStBrush1 : EXIT FUNCTION  '  Apply the colors for a Static control
              END IF 
           CASE %WM_DESTROY 
              DeleteObject hStBrush1                '  Frees all system resources associated with this brush
           CASE %WM_PAINT    ' dialog is to be redrawn
              BeginPaint hDlg, ps  ' get device context to draw in
                 BrushClr = &H0000FF
                 hPen   = CreatePen(0, 1, BrushClr) ' Want border? chg BrushClr here 
                 hPen   = SelectObject(ps.hDC, hPen) ' select it into given dc 
                 hBrush = CreateSolidBrush(BrushClr)  
                 hBrush = SelectObject(ps.hDC, hBrush) 
                 Rectangle ps.hDC, 0, 0,  451,  330
                 DeleteObject SelectObject(hDC, hPen) 
                 DeleteObject SelectObject(hDC, hBrush)
              EndPaint hDlg, ps 
           CASE %WM_CLOSE     ' Message from Clicking on system close Icon
              PostQuitMessage 0 
        END SELECT
        ProcessWindow = DefWindowProc(hdlg, wMsg, wParam, lParam)  
      FUNCTION WINMAIN (BYVAL hInstance     AS LONG, _ 
                        BYVAL hPrevInstance AS LONG, _          
                        BYVAL szCmdLine     AS ASCIIZ PTR, _    
                        BYVAL iCmdShow      AS LONG) AS LONG    
          LOCAL wMsg       AS tagMsg, hMenu AS LONG     
          LOCAL WindClass AS wndclassex  
          DIM szpgmname AS ASCIIZ * 20, STYLE AS LONG 
          'ShowWindow CONSHNDL, %SW_Hide ' Use for older PBCC
          szpgmname = "SDK Samples"
          windclass.cbSize        = SIZEOF(windclass)
          windclass.lpfnWndProc   = CODEPTR(ProcessWindow)  'point to callback
          windclass.hInstance     = hInstance  
          windclass.hIcon         = LoadIcon( hInstance, "MAINICON" ) 
          windclass.hCursor       = LoadCursor( %NULL, BYVAL %IDC_ARROW ) 
          windclass.hbrBackground = %COLOR_3DFACE + 1
          windclass.lpszClassName = VARPTR(szpgmname) 
           RegisterClassEX windclass 
              CreateWindowEx(0, szpgmname, "JaxGUI Example", _
                            STYLE,  200,  114,  459,  355, _ 
                            %HWND_DESKTOP, hMenu, hInstance, BYVAL %NULL) 
           ShowWindow hWnd, iCmdShow     
           UpdateWindow hWnd             
           WHILE (GetMessage(wMsg, %NULL, 0, 0) <> %false ) 
              IF IsDialogMessage(hWnd, wMsg) = %FALSE THEN 'For TabKey,  Comment out if using WM_KEYDOWN
                 TranslateMessage wMsg  
                 DispatchMessage  wMsg  
              END IF                                       'For TabKey,  Comment out if using WM_KEYDOWN
           WINMAIN = wMsg.wParam 


      • #4
        FUNCTION = GetStockObject(%WHITE_PEN)
        This is wrong, you can only return a HBRUSH (brush handle) from the WM_CTLCOLORXX functions. If you need a fixed, non-system color, then create the brush with CreateSolidBrush() in somewhere like WM_INITDIALOG and save the handle in a STATIC variable, then you can return this handle in WM_CTLCOLORSTATIC. The brush can be deleted in WM_DESTROY or wherever you are done with it.

        If you need to use a system color, it's very simple: just use GetSysColorBrush(). | Slam DBMS | PrpT Control | Other Downloads | Contact Me


        • #5
          Good morning,

          Richard, I'm using console compiler, but I needed an Dialog ! Yes you can use dialogs in PBCC !!! The problem is that you have to deal with Windows API !

          It worked ok, with some minors adaptations in Jerry's code, because I had the dialog created in a resource file, not by code.
          After all, the only thing I had to do is create the brush to use in the callback.

          Thanks for your help Jerry and Kev !


          • #6
            Here is another alternative.




            • #7
              FUNCTION = GetStockObject(%WHITE_BRUSH)
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]


              • #8
                Thank you Michael, but using this way the control's background is set to "WHITE", using a created brush solved my problem.


                • #9
                  Richard, I'm using console compiler, but I needed an Dialog ! Yes you can use dialogs in PBCC !!! The problem is that you have to deal with Windows API !
                  Yes, you can ... but I was trying to make sure that is what you were really trying to do. For that matter you can do a "dialog", depending on what is in it, with PB's GRAPHICS in a subclassed GW. Many examples since 8.0 was released years ago. and with 9.0 much easier.
                  Rick Angell


                  • #10

                    I was using GRAPHIC windows, but in some machines they are REALLY slow, the drawing of "natural" Windows dialogs are much faster. I think it is because Directx... Who knows...

                    Sorry, I didn't mean to be rude, just thought you didn't know that in PBCC you could use dialogs !

                    Thanks for helping.