Announcement

Collapse
No announcement yet.

Set edit win color of DateTimePicker

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

  • Set edit win color of DateTimePicker

    Hi there,

    just played around with MS DateTime control. Try to set colors.
    The only way i found was to subclass it and handle WM_Paint.
    Is there a better way to do it ?

    rgds
    Ralph

    Code:
    FUNCTION CtrlProc(BYVAL hCtl AS LONG, _
                      BYVAL Msg AS LONG, _
                      BYVAL wParam AS LONG, _
                      BYVAL lParam AS LONG) EXPORT AS LONG
    
        STATIC lpAddress AS LONG
        STATIC hBrush    AS LONG
    
        LOCAL rc  AS rect
    
        IF hCtl = 0 THEN
           lpAddress = wParam
           HBrush = createsolidBrush ( %cyan )
        END IF
    
        SELECT CASE Msg
          CASE %WM_PAINT
             LOCAL ps  AS PAINTSTRUCT
             GetCLientRect hCtl, rc
             wParam = BeginPaint(hCtl, ps)
             FillRect wParam, rc, HBrush
             ''' hdc passed to default wndProc in wParam
             ''' no end paint
          CASE %WM_Destroy
             deleteObject hBrush
        END SELECT
        
        FUNCTION = CallWindowProc ( lpAddress, hCtl, Msg, wParam, lParam )
    END FUNCTION



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

  • #2
    The only message I know of is the DTM_SETMCCOLOR, but I think it is
    for only the Child calendar window. Since it is an edit contorl, perhaps
    you could use the standard method...
    Code:
     CallBack Function DlgProc
          Static  hBrush As Long
          Select Case CbMsg
             Case %WM_INITDIALOG: hBrush  =  CreateSolidBrush(%CYAN)
             Case %WM_DESTROY: DeleteObject hBrush
             Case %WM_CTLCOLOREDIT
             Select Case GetDlgCtrlId(CbLparam)
                Case %ID_Text
                   SetTextColor CbWparam, %BLACK
                   SetBkColor CbWparam, %CYAN  
                   Function = hBrush: Exit Function
                End Select
          End Select
    HTH
    Jules

    [This message has been edited by Jules Marchildon (edited March 14, 2001).]
    Best regards
    Jules
    www.rpmarchildon.com

    Comment


    • #3
      Jules,

      thanks for the reply. Your code works perfect for edit controls
      but DateTime edit win doesn't send a WM_CTLCOLOREDIT message.

      Handle WM_paint w. BeginPaint but w/o Endpaint makes me a little nervous.
      I don't know wheter there are any GDI problems ?
      At least i don't know why it's working ?

      rgds
      Ralph


      sample code

      Code:
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
      #INCLUDE "COMMCTRL.INC"
      
      %ID_LABEL    = 10
      %ID_CALENDAR = 20
      %ID_DATE     = 21
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Format your output as you like
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      FUNCTION GetTimeandDate(sTime AS SYSTEMTIME) EXPORT AS STRING
          LOCAL tDay  AS ASCIIZ * 64
          LOCAL tTime AS ASCIIZ * 64
          GetDateFormat %LOCALE_SYSTEM_DEFAULT, %DATE_SHORTDATE, sTime, BYVAL %NULL, tDay,  64  'Use this to format the date
          GetTimeFormat %LOCALE_SYSTEM_DEFAULT, %TIME_NOSECONDS, sTime, BYVAL %NULL, tTime, 64  'Use this to format the time
          FUNCTION = tDay + " " + tTime
      END FUNCTION
      
      
      FUNCTION CtrlProc(BYVAL hCtl AS LONG, _
                        BYVAL Msg AS LONG, _
                        BYVAL wParam AS LONG, _
                        BYVAL lParam AS LONG) EXPORT AS LONG
      
          STATIC lpAddress AS LONG
          STATIC hBrush    AS LONG
      
          LOCAL rc  AS rect
      
          IF hCtl = 0 THEN
             lpAddress = wParam
             HBrush = createsolidBrush ( %cyan )
          END IF
      
          SELECT CASE Msg
      
            CASE %WM_PAINT
               LOCAL ps  AS PAINTSTRUCT
               GetCLientRect hCtl, rc
               wParam = BeginPaint(hCtl, ps)
               FillRect wParam, rc, HBrush
               ''' hdc passed to default wndProc in wParam
               ''' no end paint
               
               
      '      CASE %WM_ERASEBKGND
      '         FUNCTION = 1
      '         EXIT FUNCTION
      '         works -  but leaves ugly fractals
      
            CASE %WM_Destroy
               deleteObject hBrush
      
          END SELECT
      
          FUNCTION = CallWindowProc ( lpAddress, hCtl, Msg, wParam, lParam )
      
      END FUNCTION
      
      CALLBACK FUNCTION dateTimeCallback()
         STATIC hTime AS LONG
         STATIC hDate AS LONG
      
      
         SELECT CASE CBMSG
      
              CASE %WM_INITDIALOG
                  LOCAL lpAddress AS LONG
                  CONTROL ADD "SysDateTimePick32", CBHNDL, %ID_CALENDAR, "",  0, 0, 60, 14, %WS_CHILD OR %WS_TABSTOP OR %WS_VISIBLE OR %DTS_TIMEFORMAT
                  CONTROL ADD "SysDateTimePick32", CBHNDL, %ID_Date,     "", 60, 0, 60, 14, %WS_CHILD OR %WS_TABSTOP OR %WS_VISIBLE OR %DTS_SHORTDATEFORMAT OR %DTS_RIGHTALIGN
                  CONTROL HANDLE CBHNDL, %ID_CALENDAR TO hTime
                  CONTROL HANDLE CBHNDL, %ID_Date     TO hDate
                  DateTime_SetMonthCalColor hDate, %MCSC_TEXT, RGB(0, 255, 0)
                  DateTime_SetMonthCalColor hDate, %MCSC_TITLETEXT, RGB(0, 255, 0)
                  ''' subclass
                  lpAddress = SetWindowLong(hDate, %GWL_WNDPROC, BYVAL CODEPTR(CtrlProc) )
                  CallWindowProc CODEPTR(CtrlProc), 0, 0, lpAddress, 0
      
        
              CASE %WM_NOTIFY
               'Use this to detect changes in the calendar
               LOCAL hdrX AS NMDATETIMECHANGE PTR
               hdrX = CBLPARAM
               SELECT CASE @hdrX.hdr.code
                      CASE %DTN_CLOSEUP
                      CASE %DTN_DROPDOWN
                      CASE %DTN_DATETIMECHANGE  'Get selected date/time
                          IF @hdrx.dwFlags <> %GDT_VALID THEN EXIT FUNCTION 'Trust but verify SYSTEMTIME STRUCTURE
                          CONTROL SET TEXT getparent(CBHNDL), %ID_LABEL, "Selected Date: " & GetTimeandDate(@hdrX.st)
               END SELECT
      
      
         END SELECT
      END FUNCTION
      
      
      FUNCTION dateTimeBuild( BYVAL hWnd AS LONG ) AS LONG
      
          LOCAL hDlg AS LONG
          DIALOG NEW hWnd, "",0,0, 120, 20, %WS_CHILD OR %WS_TABSTOP OR %WS_VISIBLE TO hDlg
          DIALOG SHOW MODELESS hDlg CALL dateTimeCallback
          FUNCTION = hdlg
      
      END FUNCTION
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ' The main callback function for all controls
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      CALLBACK FUNCTION DlgCallback()
         LOCAL hdrX AS NMDATETIMECHANGE PTR
      
         SELECT CASE CBMSG
              CASE %WM_COMMAND
               IF CBCTL = %IDCANCEL THEN DIALOG END CBHNDL, (CBCTL = %IDOK)
      
         END SELECT
      END FUNCTION
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ' PBMAIN - build dialog and controls
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      FUNCTION PBMAIN
      
          DIM hDlg    AS LONG
          LOCAL iccex AS INIT_COMMON_CONTROLSEX  'Initialize the calendar
      
          iccex.dwSize = SIZEOF(iccex)
          iccex.dwICC = %ICC_DATE_CLASSES
          InitCommonControlsEx iccex
      
          DIALOG NEW 0, "Calendar demo",,, 196, 60, %WS_SYSMENU TO hDlg
          CONTROL ADD LABEL, hDlg, %ID_LABEL, "Click at the Combo's down arrow.", 6, 30, 180, 14
          CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close", 134, 6, 50, 14
          dateTimeBuild hDlg
          DIALOG SHOW MODAL hDlg CALL DlgCallback
      
      END FUNCTION
      _



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

      Comment


      • #4
        Ralph --
        I don't see any problems with ERASEBKGND under Win2000 (IE 5.50)
        What happends on your PC if to do in %WM_ERASEBKGND
        Code:
           Dim hBrush1 As Long
           hBrush1 = SetClassLong (hCtl, %GCL_HBRBACKGROUND, hBrush)
           Function = CallWindowProc ( lpAddress, hCtl, Msg, wParam, lParam )
           SetClassLong hCtl, %GCL_HBRBACKGROUND, hBrush1
           Exit Function
        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          Semen ;

          thanks for the reply. Seems i was to lazy.
          Doing it your way works fine under NT 4 SP6 too.

          Ralph

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

          Comment


          • #6
            WM_PAINT - once again

            from MS SDK - WM_PAINT
            For some common controls, the default WM_PAINT message processing
            checks the wParam parameter. If wParam is non-NULL, the control
            assumes that the value is an HDC and paints using that device context.
            But how shall i handle this. Is there any additional info
            which common controls check wParam for non-NULL.

            This one fails ( assume HDC is in wParam ) :
            Code:
            CASE %WM_PAINT         
               GetCLientRect hCtl, rc         
               FillRect wParam, rc, HBrush

            This one works - but how ?
            Code:
            CASE %WM_PAINT
               LOCAL ps AS PAINTSTRUCT
               GetCLientRect hCtl, rc
               wParam = BeginPaint(hCtl, ps)
               FillRect wParam, rc, HBrush
            If the 2nd snippet is ok it would be a perfect replacement for
            AFX onPaint(). You need not process any of these nifty
            WM_CTLCOLOR... msgs posted to controls parent window.
            Am i brilliant or perfectly wrong ? pls let me know.

            Ralph



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

            Comment


            • #7
              For WM_PAINT:

              You can execute(return) the callwindowproc first, then do GetDC()
              Not really nice..

              Case WM_PAINT

              Function = CallWindow...

              hDC = getDC( hWnd )

              paint..
              Release..

              Exit Function


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

              Comment


              • #8
                Edwin,

                thanks for the idea. Coloring Edit Win works well now.

                Ralph

                Code:
                FUNCTION CtrlProc(BYVAL hCtl AS LONG, _
                                  BYVAL Msg AS LONG, _
                                  BYVAL wParam AS LONG, _
                                  BYVAL lParam AS LONG) EXPORT AS LONG
                
                    STATIC lpAddress AS LONG
                    STATIC hBrush    AS LONG
                
                    LOCAL rc  AS rect
                
                    IF hCtl = 0 THEN
                       lpAddress = wParam
                       HBrush    = createsolidBrush ( %cyan )
                    END IF
                
                    SELECT CASE Msg
                
                       CASE %WM_PAINT
                         LOCAL hOldObj AS LONG
                         LOCAL lret    AS LONG
                         LOCAL hdc     AS LONG
                         LOCAL szText  AS ASCIIZ * 80
                
                         ' call controls native paint function
                         lret = CallWindowProc( lpAddress, hCtl, Msg, wParam, lParam )
                
                         ' brute force
                         GetClientRect hCtl, rc
                         getwindowText hCtl, szText, SIZEOF( szText )
                         rc.nLeft= rc.nleft + 2
                         rc.nTop = rc.nTop  + 2
                         hdc     = getDC ( hCtl )
                         hOldObj = SelectObject( hdc, hfont )     ' global hfont
                         SetBkColor   hdc, %cyan  ' or SetBkMode hdc, %TRANSPARENT
                         SetTextColor hdc, %RED
                         DrawText     hdc, szText, LEN(szText), rc, %DT_VCENTER
                         SelectObject hdc, hOldObj
                         releaseDC    hdc, hCtl
                         FUNCTION = lret
                         EXIT FUNCTION
                
                
                      CASE %WM_ERASEBKGND
                         LOCAL hBrush1 AS LONG
                         hBrush1  = SetClassLong (hCtl, %GCL_HBRBACKGROUND, hBrush)
                         FUNCTION = CallWindowProc ( lpAddress, hCtl, Msg, wParam, lParam )
                         SetClassLong hCtl, %GCL_HBRBACKGROUND, hBrush1
                         FUNCTION = 1
                         EXIT FUNCTION
                
                      CASE %WM_Destroy
                         deleteObject hBrush
                
                    END SELECT
                
                    FUNCTION = CallWindowProc ( lpAddress, hCtl, Msg, wParam, lParam )
                
                END FUNCTION
                ------------------

                Comment

                Working...
                X