Announcement

Collapse
No announcement yet.

Colouring a Label Control

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Colouring a Label Control

    I wonder if anybody can help me with the following.

    I'm fairly new to PD/DLL and have used the forums to help me
    create a simple program with 20 labels and edit boxes. I have
    set the first label colour using the %WM_CTLCOLORSTATIC message.

    My question is how can I change the colour of any label at any
    point in time? I'm guessing that I need to send the control a
    message but can't find any examples in the forums. I have found a
    similar example that uses a timer to randomly update a set of
    label controls. In this example CONTROL SET TEXT is used to change
    the contents of the label.

    Eternally grateful for any help

    My simple test program is included below:

    #Compile Exe
    #Include "WIN32API.INC"

    Global hDlg As Long

    CallBack Function MainDlgProc()

    Local ControlID As Long
    hWnd&=CbHndl ' The Window Handle
    Msg&=CbMsg ' The Message
    wP&=CbWparam '
    lP&= CbLparam '

    Select Case Msg&
    Case %WM_CTLCOLORSTATIC
    ControlId = GetDlgCtrlID(CbLparam)
    If ControlID = 101 Then
    SetBkMode CbWparam, %TRANSPARENT
    SetTextColor CbWparam, %YELLOW
    Else
    SetBkMode CbWparam, %TRANSPARENT
    SetTextColor CbWparam, %BLACK
    End If
    Function = GetStockObject(%NULL_BRUSH)
    End Select

    If CbCtl=900 Or CbCtl=901 Then Dialog End hDlg

    End Function

    '------------------------------------------------------------------------------

    Function WinMain (ByVal hCurInstance As Long, _
    ByVal hPrevInstance As Long, _
    lpszCmdLine As Asciiz Ptr, _
    ByVal nCmdShow As Long) Export As Long

    ' Create a new dialog template
    Style&=%WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX
    Dialog New 0, "Imprint Test",0 ,0, XX&, YY&, Style&, 0 To hDlg
    Dialog Pixels hDlg,800,600 To Units XX&,YY&
    DU#=XX&/800
    Dialog Set Size hDLG, 800*DU#,600*DU#
    Dialog Set Loc hDlg, 0,0

    ' Add controls to it
    For N&=1 To 20
    ID%=N&:Control Add TextBox, hDlg, ID%, "", 5*8*DU#, (N&*17+3)*DU#, 25*8*DU#, 16*DU#
    ID%=N&+100:Control Add Label, hDlg, ID%, Str$(id%), 8*DU#, (N&*17+3)*DU#, 4*8*DU#, 16*DU#
    Next N&
    ID%=900:Control Add Button, hDlg, ID%, "OK", 705*DU#, 475*DU#, 55*DU#, 20*DU#,0
    ID%=901:Control Add Button, hDlg, ID%, "Cancel", 645*DU#, 475*DU#, 55*DU#, 20*DU#, 0

    'Display Window
    UpdateWindow hDlg
    Dialog Show Modal hDlg Call MainDlgProc To result&

    End Function




    ------------------

  • #2
    Hope this helps...
    Winmain chopped off, but the rest is there..

    Code:
    Dialog New hWnd,"",,, 260,50,%WS_POPUP Or %WS_DLGFRAME To kDlg
    Control Add Label, kDlg, %IDABOUTLABEL1, g_szMine & " is generating a secure encryption key.", 5,10, 250,15
    Control Add Label, kDlg, %IDABOUTLABEL2,"Please move your mouse around the screen" , 5,30, 250,15
    Dialog Send kDlg, %WM_SETICON, %ICON_BIG, hIcon
    Dialog Show Modal kDlg Call GenerateKeyProc To lResult
    Function = lResult
    End Function
    '-----------------------------------------------------------------------------------------
    CallBack Function GenerateKeyProc() As Long
    Static Seedvalue As Long
    Static lTemp     As Long
    Local Lb            As LOGBRUSH
    Local Lb2            As LOGBRUSH
    Local lf            As LOGFONT
    Local lf2            As LOGFONT
    Static hFont1       As Long
    Static hFont2       As Long
    
    
    Select Case CbMsg
        Case %WM_INITDIALOG
            MousePtr 11
            SetTimer CbHndl, %IDT_TIMER1,5000, ByVal %NULL
          '------------------------------------------------------------------
          'COLOR STUFF
          hFont1 = MakeFont("Courier New Bold", 10)
          GetObject hFont1, SizeOf(lf), ByVal VarPtr(lf)
          hFont1 = MakeFont("Courier New Bold", 10)
          GetObject hFont2, SizeOf(lf2), ByVal VarPtr(lf2)
          'Make the text bold font or normal
          lf.lfWeight = %FW_BOLD
          Lb.lbStyle  = %BLACK_BRUSH
          lf2.lfWeight = %FW_BOLD
          Lb2.lbStyle  = %BLACK_BRUSH
          Control Send CbHndl,%IDABOUTLABEL1, %WM_SETFONT, hFont1, %TRUE
          Control Send CbHndl,%IDABOUTLABEL2, %WM_SETFONT, hFont2, %TRUE
          '------------------------------------------------------------------
    
        Case %WM_CTLCOLORSTATIC
             Select Case CbLparam
                    Case GetDlgItem(CbHndl,%IDABOUTLABEL1)
                      SelectObject CbWparam, hFont1
                      SetBkMode CbWparam, %TRANSPARENT
                      Function = GetStockObject(%NULL_BRUSH)
                      SetTextColor CbWparam, %BLUE
                    Case GetDlgItem(CbHndl,%IDABOUTLABEL2)
                      SelectObject CbWparam, hFont2
                      SetBkMode CbWparam, %TRANSPARENT
                      SetTextColor CbWparam, %BLUE
                      Function = GetStockObject(%NULL_BRUSH)
            End Select
    
        Case %WM_DESTROY
             KillTimer CbHndl,  %IDT_TIMER1
             DeleteObject hFont1
             DeleteObject hFont2
    ------------------
    Scott
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      PS, search the source code Forum for "Makefont" for the makefont routine


      Scott

      ------------------
      Scott
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        [QUOTE]Originally posted by Scott Turchin:
        Hope this helps...
        Winmain chopped off, but the rest is there..

        Thanks for the reply Scott. I added your sample code to my
        program and I now have labels in a different font and a different
        colour but I'm still none the wiser about how to change a label
        colour when I want to. Your sample responds to a %WM_CTLCOLORSTATIC
        message as the program first runs. Say I was to put a button on
        the form which when pressed would change the colour of all labels,
        how would I go about doing that? Clearly the program needs to
        respond to a %WM_CTLCOLORSTATIC message but how do I get that
        message to occur?

        Nick



        ------------------

        Comment


        • #5
          Ahhh, I get it now. I have worked out the parameters that are
          required in a %WM_CTLCOLORSELECT message and sent one to a static
          when a button is pressed by using CONTROL SEND. The colour
          changes correctly. I made educated guesses at the parameters
          having seen the message used in the various examples - is their
          an easy way of finding what parameters are required for each
          message?

          ------------------

          Comment


          • #6
            I have not dynamically changed it yet, and I'm just as green as you are at the color thing, actually more green now since you've got this far figured out



            ------------------
            Scott
            Scott Turchin
            MCSE, MCP+I
            http://www.tngbbs.com
            ----------------------
            True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

            Comment


            • #7
              Download the WIN32.HLP file from our DOWNLOADS section... it describes thousands of messages parameters and API functions under Win32. Warning: It is approx 8Mb and installs to around 25Mb.

              WIN32.ZIP http://www.powerbasic.com/files/pub/mstools/win32.zip

              Note that you should not send a %WM_CTLCOLORxxx message yourself (I think that is what your message intimated!)... Window sends these message to the parent dialog callback when a control is about to be drawn or redrawn. You simply need to respond to the messages appropriately.

              Also, be sure to check out the SMTP.BAS example that shipped with PB/DLL for more examples of using a background image and colored controls, etc.

              I hope this helps!


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment


              • #8
                Lance - you are right I have sent the %WM_CTLCOLORSTATIC message
                myself. If this is incorrect then how do I change the colour of
                a label after an event has taken place such as a edit box gaining
                focus or a button being clicked?

                I thought I was making progress for a while

                ------------------

                Comment


                • #9
                  InvalidateRect hWnd, Byval 0&, 1

                  Where hWnd is your control.

                  ------------------
                  hellobasic

                  Comment


                  • #10
                    If you require the redraw to occur immediately after invalidating the control's window, call UpdateWindow() (otherwise the repaint may not occur until the app message queue is emptied, and depending on what you app is doing, this could cause a visible delay before the redraw occurs).

                    Personally, I tend to use RedrawWindow() more often than I use InvalidateRect+UpdateWindow... YMMV.


                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      Thanks Lance and Edwin, that's done the trick. I've used
                      InvalidateRect and UpdateWindow as I wasn't sure about the parameters
                      in RedrawWindow but that can wait for another day I think. I've
                      looked at the message queue and can see that InvalidateRect creates
                      a %WM_CTLCOLORSTATIC message and understand that UpdateWindow
                      forces the repaint immediately.

                      It's early days for me on PBDLL so thanks for your help.... One
                      more (very trivial) question - what does YMMV stand for that you
                      often add to the end of your postings Lance? Am I really showing
                      my ignorance now.....

                      ------------------

                      Comment


                      • #12
                        invalidaterect simply marks a 'region' of a window or control as invalid (may be the whole window or just a part of it), and this is added to any other invalid 'regions' for that window. (remember, even controls are windows, they are just a different class of window.)

                        when the message queue is empty, windows checks if there are any invalid 'regions', and is so, issues a %wm_paint message. this is why %wm_paint is known as a non-queued message.

                        during processing of %wm_paint, most of the standard controls send a %wm_ctlcolorxxx message to their parent callback.

                        therefore, when you call invalidaterect(), you will eventually see a %wm_ctlcolorxxx message in your queue, but there may be a whole bunch of other messages going through the callback in between.

                        nick, i highly recommend that you get yourself a good windows programming book - it will clear up a lot of these types of questions for you by teaching you how message-based windows applications work from the ground up. you can find a list of titles at http://www.powerbasic.com/support/pb...hread.php?t=39

                        finally, ymmv means "your mileage may vary". a 'disclaimer' of sorts!

                        ------------------
                        lance
                        powerbasic support
                        mailto:[email protected][email protected]</a>
                        Lance
                        mailto:[email protected]

                        Comment


                        • #13
                          Thanks Lance, fair point. I'm in the process of reading Petzolds
                          book and know that I presently have a lot to learn. In the meantime
                          I'm under pressure to show some progress to those who pay the bills

                          I appreciate your help.

                          Nick


                          ------------------

                          Comment


                          • #14
                            Hi Nick!

                            I highly recommend doing a search on the net for "b5ms.zip",
                            which is a 17 meg file but very useful. I like it because it
                            doesn't necessarily assume a C++ background and covers dang-near
                            everything connected to windows programming.

                            Try http://ftpsearch.lycos.com/?form=medium
                            and type in "b5ms.zip".

                            I believe it was created by Borland, so it is professionally
                            done. When you unzip it into its own folder, "mm.hlp" is the
                            main starting point.

                            Again, very useful indeed.

                            Take care!
                            Russell Jones

                            ------------------
                            Talk Sense to a Fool and he Calls you Foolish...

                            Comment


                            • #15
                              Thanks Russell. I spent a good day reading yesterday and it's
                              certainly paying off, things are much clearer than they were.
                              I must admit I have got one or two pages still to go.....



                              ------------------

                              Comment

                              Working...
                              X