Announcement

Collapse
No announcement yet.

How to clear previous labels in SDK?

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

  • How to clear previous labels in SDK?

    I have added more buttons into my SDK program, so that when each button is clicked, the program will display a new label. However, the results
    is that the previous labels are still present causing an overlap of these labels. How do I clear off the previous labels before displaying the new ones.

    The screen shots of the problematic program are :
    Click image for larger version

Name:	Overlap.png
Views:	1
Size:	10.4 KB
ID:	770774


    The program code are as follows :

    Code:
    ' SDK dialog.bas
    ' https://forum.powerbasic.com/forum/user-to-user-discussions/programming/758500-how-to-make-a-text-box-from-scratch
    #COMPILER PBWIN 10
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32API.inc"
    
    ENUM Equates SINGULAR
       IDC_Button = 500
       IDC_Button2
       IDC_Button3
       LabelFN
    END ENUM
    
    #RESOURCE ICON, 300, "Box wall.ICO"
    
    GLOBAL hWndMain AS DWORD
    GLOBAL Flagclick AS LONG
    GLOBAL hlab AS DWORD
    
    '=============================================
    FUNCTION WINMAIN (BYVAL hInst AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpCmdLine AS ASCIIZ PTR, BYVAL iCmdShow AS LONG) AS LONG
       'arguments to WinMain are provided by Windows - not defined by the programmer
       'hInst             current instance of the application
       'hPrevInstance    previous instance (not used - returns %Null in Win32)
       'lpCmdLine        pointer to command line (whatever the user types in after MyApp.EXE). A null terminated string.
       'iCmdShow         used the first time ShowWindow is called to display the application main window
       LOCAL Msg         AS tagMsg
       LOCAL W           AS WndClassEx
       LOCAL szAppName  AS ASCIIZ * 80
       LOCAL hWndButton AS DWORD
       LOCAL hAccel     AS DWORD
       LOCAL WndStyle   AS LONG
       LOCAL WndStyleX  AS LONG
    
    
       'register window class
       szAppName = "HelloSDK"
       W.cbSize        = SIZEOF(W)
       W.Style         = %CS_HREDRAW OR %CS_VREDRAW   '%WS_OverlappedWindow Or %WS_HScroll Or %WS_VScroll  '
       W.lpfnWndProc   = CODEPTR(WndProc)
       W.cbClsExtra    = 0
       W.cbWndExtra    = 0
       W.hInstance     = hInst
       W.hIcon         = LoadIcon(hInst, BYVAL 300)
       W.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
       W.hbrBackground = createsolidbrush(%RGB_AZURE)         '   %Color_BtnFace+1
       W.lpszMenuName  = %NULL
       W.lpszClassName = VARPTR(szAppName)
       W.hIconSm       = LoadIcon(hInst, BYVAL %IDI_APPLICATION)   'why not %NULL
       RegisterClassEx W
    
       'create window of that class
       WndStyle  = %WS_OVERLAPPEDWINDOW
       WndStyleX = %WS_EX_LEFT
       hWndMain  = CreateWindowEX( _
          WndStyleX,  _    'dwStyle        extended style of window
          szAppName,  _    'lpClassName    pointer to a null-terminated string (any registered name, including predefined system class names).
          "SDK",      _    'lpWindowName   pointer to a null-terminated String (placed in title bar, text of the control, icon name or #identifier)
          WndStyle,   _    'dwStyle        style of window
          300,        _    'x              initial horizontal position (upper left screen, except for child is relative to parent window's client area)
          100,        _    'y              initial vertical position  (upper left screen, except for child is relative to parent window's client area)
          400,        _    'nWidth         width in device units
          200,        _    'nHeight        height in device units
          %Null,      _    'hWndParent     handle to parent window (optional for a pop-up window)
          %Null,      _    'hMenu          handle to a menu
          hInst,      _    'handle to instance of the module to be associated with the window
          BYVAL %Null _    'pointer to CREATESTRUCT structure pointed to by the lParam param of the WM_CREATE message.
          )
    
         CreateButton
         CreateButton2
         CreateButton3
    
    
       ' Label for filename
         hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : " , _
                                  %WS_VISIBLE OR %WS_CHILD OR %ES_LEFT   , _
                                  5,102,260,22, _
                                  hWndMain,%LabelFN,hInst,BYVAL %NULL)
    
    
    
       'predefined lpClassName values:  Button, ComboBox, Edit, ListBox, MDIClient, RichEdit, RichEdit_Class, Scrollbar, Static
       'window not shown automatically. must show it manually
       ShowWindow hWndMain, iCmdShow   'controls how window is to be shown. 1st must use iCmdShow
       UpdateWindow hWndMain           'sends the window it's first WM_Create to display the window on the screen
    
       'message pump - calls WndProc whenever an application-specific message is received
       'WndProc can process the message, or pass it on to a the default window procedure that is built into Windows.
       WHILE GetMessage(Msg, %NULL, 0, 0) > 0
          IF ISFALSE TranslateAccelerator (hWndMain, hAccel, Msg) THEN
             IF ISFALSE ISDialogMessage (hWndMain, Msg) THEN
                TranslateMessage Msg
                DispatchMessage  Msg
             END IF
          END IF
       WEND
    END FUNCTION
    
    
    '=================================================
    FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) EXPORT AS LONG
       LOCAL hDC AS DWORD, pPaint AS PAINTSTRUCT, tRect  AS RECT
       LOCAL pnmhdr AS NMHDR PTR
       LOCAL hInst AS DWORD
    
       SELECT CASE wMsg
           'life-cycle
           CASE %WM_CREATE          'when an application requests that a window be created
                                    'after window is created, but before it becomes visible
    
           CASE %WM_INITDIALOG      'immediately before a dialog box is displayed
                                    'typically used to initialize controls and carry out any other
                                    'initialization tasks that affect the appearance of the dialog box.
    
           CASE %WM_DESTROY         'window is being destroyed
                                    'after windows is removed from screen (children still exist)
              PostQuitMessage 0
              EXIT FUNCTION
    
           'control notifications
           CASE %WM_COMMAND         'user selects a command item from a menu
                                    'control sends a notification message to its parent window
                                    'accelerator keystroke is translated.
             SELECT CASE LO(WORD,wParam)
                CASE %IDC_Button
                   SELECT CASE HI(WORD,wParam)
                      CASE %BN_CLICKED
                         ? "Button was Clicked"
                         FlagClick = 1
                          hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : Test.xcv " , _
                                  %WS_VISIBLE OR %WS_CHILD OR %ES_LEFT   , _
                                  5,102,260,22, _
                                  hWndMain,%LabelFN,hInst,BYVAL %NULL)
    
    
                       '   DrawText hDC, "button was clicked", -1, tRect, %DT_SINGLELINE OR %DT_BOTTOM  OR %DT_CENTER
                        '  InvalidateRect  hWnd, BYVAL %NULL, %TRUE
                       '   UpdateWindow  hWnd
                   END SELECT
    
               CASE %IDC_Button2
                   SELECT CASE HI(WORD,wParam)
                      CASE %BN_CLICKED
                         ? "Button2 was Clicked"
                         FlagClick = 1
                          hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : New2 Test.xcv " , _
                                  %WS_VISIBLE OR %WS_CHILD OR %ES_LEFT   , _
                                  5,102,260,22, _
                                  hWndMain,%LabelFN,hInst,BYVAL %NULL)
    
    
                       '   DrawText hDC, "button2 was clicked", -1, tRect, %DT_SINGLELINE OR %DT_BOTTOM  OR %DT_CENTER
                       '   InvalidateRect  hWnd, BYVAL %NULL, %TRUE
                       '   UpdateWindow  hWnd
                   END SELECT
    
                  CASE %IDC_Button3
                   SELECT CASE HI(WORD,wParam)
                      CASE %BN_CLICKED
                         ? "Button3 was Clicked"
                         FlagClick = 1
                          hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : New3   button3 Test.xcv " , _
                                  %WS_VISIBLE OR %WS_CHILD OR %ES_LEFT   , _
                                  5,102,260,22, _
                                  hWndMain,%LabelFN,hInst,BYVAL %NULL)
    
    
                       '   DrawText hDC, "button3 was clicked", -1, tRect, %DT_SINGLELINE OR %DT_BOTTOM  OR %DT_CENTER
                       '   InvalidateRect  hWnd, BYVAL %NULL, %TRUE
                       '   UpdateWindow  hWnd
                   END SELECT
    
    
             END SELECT
    
          CASE %WM_NOTIFY          'by control to its parent window when an event has occurred
             pnmhdr = lParam
             SELECT CASE @pnmhdr.idFrom       'Control ID
                CASE %IDC_Button
                   SELECT CASE @pnmhdr.code  'Notification code
                      CASE %NM_CLICK         'typical notification code
                   END SELECT
    
               CASE %IDC_Button2
                   SELECT CASE @pnmhdr.code  'Notification code
                      CASE %NM_CLICK         'typical notification code
                   END SELECT
    
              CASE %IDC_Button3
                   SELECT CASE @pnmhdr.code  'Notification code
                      CASE %NM_CLICK         'typical notification code
                   END SELECT
    
             END SELECT
    
          'resize
           CASE %WM_SIZE            'after its size has changed.
           CASE %WM_GETMINMAXINFO   'size/position of window is about to change
    
           'mouse click
           CASE %WM_PARENTNOTIFY    'sent to parent when child window is create/destrory
                                    'sent to parent when user clicks mouse button over child
    
           CASE %WM_SYSCOMMAND      'user choose command from Window menu, max/min/restore/close buttons
    
    
           'mouse movement
           CASE %WM_SETCURSOR       'sent if mouse causes cursor to move
           CASE %WM_MOUSEMOVE       'when cursor moves
    
           'keyboard
           CASE %WM_CHAR            'to window with keyboard focus when WM_KEYDOWN message is translated
                                    'by the TranslateMessage function. contains the character code of the
                                    'key that was pressed.
           CASE %WM_GETDLGCODE      'intercept keyboard input to child control
    
           'application status
           CASE %WM_ACTIVATEAPP     'focus returns to app from another app
           CASE %WM_ACTIVATE        'sent to both the window being activated and the window
                                    'being deactivated.
           CASE %WM_NCACTIVATE      'sent to window when its nonclient area needs to be changed
                                    'to indicate an active or inactive state.
    
    
            CASE %WM_CTLCOLORSTATIC
                 ' this make the background of the Filename label transparent
                 ' lParam is static handle
                IF GetDlgCtrlID(lParam) = %LabelFN THEN
                   SetTextColor(wPARAM, %RGB_BLUE) 'wPARAM id device context DC
                   SetBkMode(wPARAM, %TRANSPARENT)
                   FUNCTION = GetStockObject(%NULL_BRUSH)
                   EXIT FUNCTION
                END IF
    
    
    
           'graphic/drawing events
           CASE %WM_ERASEBKGND      'sent when the window background must be erased (such as resizing)
              hDC = wParam
              DrawGradient hDC              ' Pass the DC of the region to repaint
              FUNCTION = 1
              EXIT FUNCTION
    
           CASE %WM_PAINT           'set when request is made to paint a portion of an application's window.
              hDC = BeginPaint(hWnd, pPaint)
              GetClientRect hWnd, tRect
              SetBkMode hDC, %TRANSPARENT
              SetTextColor hDC,%RGB_BLUE ' %WHITE
              IF Flagclick = 0 THEN
                 DrawText hDC, "", -1, tRect, %DT_SINGLELINE OR %DT_BOTTOM  OR %DT_CENTER ' OR %DT_VCENTER
              ELSE
                 DrawText hDC, "button was clicked", -1, tRect, %DT_SINGLELINE OR %DT_BOTTOM  OR %DT_CENTER
              END IF
              EndPaint hWnd, pPaint
              FUNCTION = 1
              EXIT FUNCTION
    
           'recurring events
           CASE %WM_TIMER           'when a timer expires
    
           'menu activity
           CASE %WM_INITMENUPOPUP   'when pop-up menu is about to become active
           CASE %WM_CONTEXTMENU     'user clicked the right mouse button (right-clicked) in a window.
           CASE %WM_MENUSELECT      'sent when user selects a menu item
    
           'user action
           CASE %WM_PASTE           'application sends a WM_PASTE message to an edit control or combobox
           CASE %WM_DROPFILES       'sent when user drops file on application that's is registered as file recipient
       END SELECT
       FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)   'if not handled above, pass to Windows default message handler.
    END FUNCTION
    
    
    '==========================================
    ' draws with color gradient
    SUB DrawGradient (BYVAL hDC AS DWORD)
       LOCAL rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE, hBrush AS DWORD, lOnBand AS LONG
       GetClientRect WindowFromDC(hDC), rectClient
       fStep = rectClient.nbottom / 200
       FOR lOnBand = 0 TO 199
          SetRect rectFill, 0, lOnBand * fStep, rectClient.nright + 1, (lOnBand + 1) * fStep
          ' paint the background
          hBrush = CreateSolidBrush(RGB(128, 250, 238 - lOnBand))
          Fillrect hDC, rectFill, hBrush
          DeleteObject hBrush
       NEXT
    END SUB
    
    
    '===========================
    SUB CreateButton
       LOCAL WndStyle AS LONG, hWnd,hInstance AS DWORD
       'create window of existing class "Button"
       WndStyle  = %WS_TABSTOP OR %WS_VISIBLE OR %WS_CHILD OR %BS_DEFPUSHBUTTON
       hInstance = GetWindowLong(hWndMain, %GWL_HINSTANCE)
       'DDT version: Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20, Style, XStyle Call ButtonProc
       hWnd= CreateWindow( _
          "Button",   _    'lpClassName    pointer to a null-terminated string (any registered name, including predefined system class names).
          "Button",   _    'lpWindowName   pointer to a null-terminated String (placed in title bar, text of the control, icon name or #identifier)
          WndStyle,   _    'dwStyle        style of window
          10,         _    'x              initial horizontal position (upper left screen, except for child is relative to parent window's client area)
          10,         _    'y              initial vertical position  (upper left screen, except for child is relative to parent window's client area)
          80,         _    'nWidth         width in device units
          20,         _    'nHeight        height in device units
          hWndMain,   _    'hWndParent     handle to parent window (optional for a pop-up window)
          %IDC_Button,_    'hMenu          handle to a menu or child window control ID
          hInstance,  _    'handle to instance of the module to be associated with the window
          BYVAL %Null _    'pointer to CREATESTRUCT structure pointed to by the lParam param of the WM_CREATE message.
          )
    END SUB
    
    
    '===========================
    ' create a second button
    SUB CreateButton2
       LOCAL WndStyle AS LONG, hWnd,hInstance AS DWORD
       'create window of existing class "Button"
       WndStyle  = %WS_TABSTOP OR %WS_VISIBLE OR %WS_CHILD OR %BS_DEFPUSHBUTTON
       hInstance = GetWindowLong(hWndMain, %GWL_HINSTANCE)
       hWnd= CreateWindow( _
          "Button",   _    'lpClassName    pointer to a null-terminated string (any registered name, including predefined system class names).
          "Button 2",   _    'lpWindowName   pointer to a null-terminated String (placed in title bar, text of the control, icon name or #identifier)
          WndStyle,   _    'dwStyle        style of window
          110,         _    'x              initial horizontal position (upper left screen, except for child is relative to parent window's client area)
          10,         _    'y              initial vertical position  (upper left screen, except for child is relative to parent window's client area)
          80,         _    'nWidth         width in device units
          20,         _    'nHeight        height in device units
          hWndMain,   _    'hWndParent     handle to parent window (optional for a pop-up window)
          %IDC_Button2,_    'hMenu          handle to a menu or child window control ID
          hInstance,  _    'handle to instance of the module to be associated with the window
          BYVAL %Null _    'pointer to CREATESTRUCT structure pointed to by the lParam param of the WM_CREATE message.
          )
    END SUB
    
    
    '===========================
    ' create a third button
    SUB CreateButton3
       LOCAL WndStyle AS LONG, hWnd,hInstance AS DWORD
       'create window of existing class "Button"
       WndStyle  = %WS_TABSTOP OR %WS_VISIBLE OR %WS_CHILD OR %BS_DEFPUSHBUTTON
       hInstance = GetWindowLong(hWndMain, %GWL_HINSTANCE)
       hWnd= CreateWindow( _
          "Button",   _    'lpClassName    pointer to a null-terminated string (any registered name, including predefined system class names).
          "Button 3",   _    'lpWindowName   pointer to a null-terminated String (placed in title bar, text of the control, icon name or #identifier)
          WndStyle,   _    'dwStyle        style of window
          210,         _    'x              initial horizontal position (upper left screen, except for child is relative to parent window's client area)
          10,         _    'y              initial vertical position  (upper left screen, except for child is relative to parent window's client area)
          80,         _    'nWidth         width in device units
          20,         _    'nHeight        height in device units
          hWndMain,   _    'hWndParent     handle to parent window (optional for a pop-up window)
          %IDC_Button3,_    'hMenu          handle to a menu or child window control ID
          hInstance,  _    'handle to instance of the module to be associated with the window
          BYVAL %Null _    'pointer to CREATESTRUCT structure pointed to by the lParam param of the WM_CREATE message.
          )
    END SUB

  • #2
    If you recreate a control to update the text, as you did in your code, destroying the previous one before might be good.
    Code:
    DestroyWindow(hLab)
    hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : Test.xcv ", %WS_VISIBLE OR _
                          %WS_CHILD OR %ES_LEFT, 5, 102, 260, 22, hWndMain, %LabelFN, hInst, BYVAL %NULL)
    Another way could also be just to erase the old text via code like...
    Code:
    '_____________________________________________________________________________
    
    SUB RedrawTransparentControl(hWin AS DWORD, hControl AS DWORD, sText AS STRING)
     LOCAL ControlRec AS RECT
    
     GetClientRect(hControl, ControlRec)
     MapWindowPoints(hControl, hWin, VARPTR(ControlRec), 2)
     RedrawWindow(hWin, ControlRec, %NULL, %RDW_ERASE OR %RDW_INVALIDATE)
     SetWindowText(hControl, (sText))
    
    END SUB
    '_____________________________________________________________________________
    
     RedrawTransparentControl(hWnd, hLab, "RedrawTransparentControl")

    Comment


    • #3
      Thanks so much Pierre -- it works well now

      The following is the code for one of the buttons

      Code:
      CASE %IDC_Button
                     SELECT CASE HI(WORD,wParam)
                        CASE %BN_CLICKED
                           ? "Button was Clicked"
                           FlagClick = 1
                           IF hlab THEN
                              DestroyWindow(hLab)
                           END IF
                            hlab = CreateWindowEx(%WS_EX_TRANSPARENT,"STATIC"," Filename : Test.xcv " , _
                                    %WS_VISIBLE OR %WS_CHILD OR %ES_LEFT   , _
                                    5,102,260,22, _
                                    hWndMain,%LabelFN,hInst,BYVAL %NULL)
      
                     END SELECT

      Comment


      • #4
        However when I include the following code and upon compiling it there is an error

        Code:
         SUB RedrawTransparentControl(hWin AS DWORD, hControl AS DWORD, sText AS STRING)
         LOCAL ControlRec AS RECT
        
         GetClientRect(hControl, ControlRec)
         MapWindowPoints(hControl, hWin, VARPTR(ControlRec), 2)
         RedrawWindow(hWin, ControlRec, %NULL, %RDW_ERASE OR %RDW_INVALIDATE)
         SetWindowText(hControl, (sText))
        
        END SUB

        The compilation error is pointed at VARPTR(ControlRec)
        Code:
        Error 426 in C:\PB CWindows\SDK dialog\SDK dialog.bas(280:034):  Variable expected
          Line 280:  MapWindowPoints(hControl, hWin, VARPTR(ControlRec), 2)

        Comment


        • #5
          Try passing it by value...
          Code:
          MapWindowPoints(hControl, hWin, BYVAL VARPTR(ControlRec), 2)

          Comment


          • #6
            Thanks Pierre
            It can compile now, and it clears the old labels using RedrawTransparentControl(hWnd, hLab, "")

            Comment


            • #7
              https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx


              OCBE
              Dale

              Comment


              • #8
                Seriously? You are creating another window every time a button is clicked and not destroying the old one?
                The usual way to handle this is to just update the existing label rather than creating a new one every time.

                Comment


                • #9
                  I'm glad to help a little, Anne.

                  Comment

                  Working...
                  X