Announcement

Collapse
No announcement yet.

Graphic control in resource file

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

  • Graphic control in resource file

    Graphic control in resource file
    i can't add graphic control to a resource file, so i try to change the style of the label control, but i failed.
    this is my code, i use "resed resource editor"(http://www.radasm.com/resed/)

    Code:
    'Test.bas
    #Compile Exe "Test.exe"
    #Dim All
    #Include "Win32API.INC"
    #Resource "Test.pbr"
    %IDC20_GRP1 = 2001
    %IDC20_BTN1 = 2002
    %IDC_GRAPHIC = 2003
    %IDC20_UDC1 = 1001
    Global ghInst, ghDlg As Long
    Global Mehwnd, PassDlg As Long
    Global hWnd, Pid, idThread, hProcess As Dword
    
    'CallBack
    CallBack Function MainWndProc
        Select Case CbMsg
            Case %WM_INITDIALOG
                Mehwnd = CbHndl
                
                Local lStyle, lExStyle As Long
                lStyle = %WS_CHILD Or %WS_VISIBLE Or %WS_BORDER Or %SS_OWNERDRAW
                SetWindowLong GetDlgItem(CbHndl, %IDC20_UDC1), %GWL_STYLE, lStyle
                SetWindowLong GetDlgItem(CbHndl, %IDC20_UDC1), %GWL_EXSTYLE, lExStyle
                
                'Control Add Graphic, CbHndl, %IDC_GRAPHIC, "", 10, 10, 168, 168, %WS_BORDER Or %SS_OWNERDRAW
                'Graphic Attach CbHndl, %IDC_GRAPHIC
            Case %WM_DESTROY
            Case %WM_COMMAND
                Select Case LoWrd(CbWParam)
                    Case %IDC20_BTN1
                        Graphic Attach CbHndl, %IDC20_UDC1
                        Graphic Color %White, %Black
                        Graphic Clear
                        Graphic Line (5, 5)-(100, 100), RGB(255, 255, 255)
                        Graphic Line (100, 100)-(5, 100), RGB(255, 255, 255)
                        'Graphic Save "Test.bmp"
                End Select
            Case %WM_SYSCOMMAND
                Select Case CbWParam
                    Case %SC_CLOSE
                        Dialog End CbHndl
                    Case %SC_MINIMIZE
                End Select
        End Select
    End Function
    Function WinMain (ByVal CurInst As Dword, _
                      ByVal PrvInst As Dword, _
                      ByVal CmdLine As Asciiz Ptr, _
                      ByVal CmdShow As Long) As Long
        ghInst = CurInst
        DialogBox ghInst, "MainDlg", %HWND_DESKTOP, CodePtr(MainWndProc)
    End Function
    
    'Test.rc
    #define IDC20_GRP1 2001
    #define IDC20_BTN1 2002
    #define IDC20_UDC1 1001
    #include "Resource.h"
    MainDlg DIALOGEX 6,5,129,181
    CAPTION "Test"
    FONT 9,"Verdana",400,0,0
    STYLE WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|DS_CENTER
    BEGIN
      CONTROL "None",IDC20_GRP1,"Button",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,3,1,123,24
      CONTROL "Draw",IDC20_BTN1,"Button",WS_CHILD|WS_VISIBLE,43,156,42,17
      CONTROL "",IDC20_UDC1,"Static",WS_CHILD|WS_VISIBLE|WS_BORDER,7,41,111,106
    END
    Yes ICON DISCARDABLE "ms.ico"

  • #2
    PowerBASIC GRAPHIC controls may only be created using the CONTROL ADD GRAPHIC instrinic. GRAPHIC xxxx statements only work against PowerBASIC GRAPHIC controls. CONTROL ADD GRAPHIC only works on windows (dialogs) created with DIALOG NEW.

    Maybe not what you wanted to hear, but them's the facts.

    That said, the "SDK Style" commands to clear areas and draw lines (FillRect and LineTo)are pretty simple to use. ("save" is a bit more complex but there are examples here).

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Evan,
      since you are using a resource editor, I assume you don't have any visual designer such as PBForms? You can hard code the DDT commands and suggest you build your Main dialog with DDT. Since I have PBForms, here is an example and I just added your Graphics commands. Like Michael explained, if you dable with the Win32 API you could use a static control from your resource, but instead use the API functions to do your drawing... I can probably find an example someplace in my junk pile if you need some help.

      Code:
      #PBFORMS CREATED V1.51
      '------------------------------------------------------------------------------
      ' The first line in this file is a PB/Forms metastatement.
      ' It should ALWAYS be the first line of the file. Other
      ' PB/Forms metastatements are placed at the beginning and
      ' end of "Named Blocks" of code that should be edited
      ' with PBForms only. Do not manually edit or delete these
      ' metastatements or PB/Forms will not be able to reread
      ' the file correctly.  See the PB/Forms documentation for
      ' more information.
      ' Named blocks begin like this:    #PBFORMS BEGIN ...
      ' Named blocks end like this:      #PBFORMS END ...
      ' Other PB/Forms metastatements such as:
      '     #PBFORMS DECLARATIONS
      ' are used by PB/Forms to insert additional code.
      ' Feel free to make changes anywhere else in the file.
      '------------------------------------------------------------------------------
      
      #COMPILE EXE
      #DIM ALL
      
      '------------------------------------------------------------------------------
      '   ** Includes **
      '------------------------------------------------------------------------------
      #PBFORMS BEGIN INCLUDES
      #IF NOT %DEF(%WINAPI)
          #INCLUDE "WIN32API.INC"
      #ENDIF
      #PBFORMS END INCLUDES
      '------------------------------------------------------------------------------
      
      '------------------------------------------------------------------------------
      '   ** Constants **
      '------------------------------------------------------------------------------
      #PBFORMS BEGIN CONSTANTS
      %IDC_BUTTON1 = 1001
      %IDC_FRAME1  = 1002
      %IDC_LABEL1  = 1003
      %IDD_DIALOG1 =  101
      #PBFORMS END CONSTANTS
      %IDC_GRAPHIC1  = 200  '<-added after PBForms code generation
      '------------------------------------------------------------------------------
      
      '------------------------------------------------------------------------------
      '   ** Declarations **
      '------------------------------------------------------------------------------
      DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
      DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
      #PBFORMS DECLARATIONS
      '------------------------------------------------------------------------------
      
      '------------------------------------------------------------------------------
      '   ** Main Application Entry Point **
      '------------------------------------------------------------------------------
      FUNCTION PBMAIN()
          ShowDIALOG1 %HWND_DESKTOP
      END FUNCTION
      '------------------------------------------------------------------------------
      
      '------------------------------------------------------------------------------
      '   ** CallBacks **
      '------------------------------------------------------------------------------
      CALLBACK FUNCTION ShowDIALOG1Proc()
      
          SELECT CASE AS LONG CBMSG
              CASE %WM_INITDIALOG
                  ' Initialization handler
      
                  CONTROL ADD GRAPHIC, CBHNDL, %IDC_GRAPHIC1, "", 22, 17, 157, 129
                              
              CASE %WM_COMMAND
                  ' Process control notifications
                  SELECT CASE AS LONG CBCTL
                      CASE %IDC_BUTTON1
                          IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                              
                              GRAPHIC ATTACH CBHNDL, %IDC_GRAPHIC1, REDRAW
                              GRAPHIC COLOR %WHITE, %BLACK
                              GRAPHIC CLEAR
                              GRAPHIC LINE (5, 5)-(100, 100), RGB(255, 255, 255)
                              GRAPHIC LINE (100, 100)-(5, 100), RGB(255, 255, 255)
                              GRAPHIC REDRAW
                              
                          END IF
                  END SELECT
                  
              CASE %WM_NCACTIVATE
                  STATIC hWndSaveFocus AS DWORD
                  IF ISFALSE CBWPARAM THEN
                      ' Save control focus
                      hWndSaveFocus = GetFocus()
                  ELSEIF hWndSaveFocus THEN
                      ' Restore control focus
                      SetFocus(hWndSaveFocus)
                      hWndSaveFocus = 0
                  END IF
                  
          END SELECT
      END FUNCTION
      '------------------------------------------------------------------------------
      
      '------------------------------------------------------------------------------
      '   ** Dialogs **
      '------------------------------------------------------------------------------
      FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          LOCAL lRslt AS LONG
      
      #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
          LOCAL hDlg  AS DWORD
      
          DIALOG NEW hParent, "Dialog1", 178, 105, 201, 196, %WS_POPUP OR _
              %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
              %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR _
              %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
              %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
              %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
          CONTROL ADD BUTTON, hDlg, %IDC_BUTTON1, "DRAW", 72, 160, 50, 15
          CONTROL ADD FRAME,  hDlg, %IDC_FRAME1, "", 16, 8, 168, 144
          CONTROL ADD LABEL,  hDlg, %IDC_LABEL1, "", 19, 15, 162, 134, %WS_CHILD OR _
              %WS_VISIBLE OR %WS_BORDER OR %SS_LEFT, %WS_EX_CLIENTEDGE OR _
              %WS_EX_STATICEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING
      #PBFORMS END DIALOG
      
          DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
      
      #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
      #PBFORMS END CLEANUP
      
          FUNCTION = lRslt
      END FUNCTION
      '------------------------------------------------------------------------------
      Best regards
      Jules
      www.rpmarchildon.com

      Comment


      • #4
        Michael's explanation is very clear, Thank you.
        Jules, I have tried your code,it operating normally, thanks for your help.
        But i would rather prefer to use resource editor, so i will try to use the API function to solve this problem. i am glad that you will give me an example.

        Comment


        • #5
          Evan,

          As promised... glued together code from some of the other guys old posts... (Chris Boss, David L. Morris ) This is one way, there are several other ways to do the same thing.

          First your resource file...
          Code:
          #include "Resource.h"
          
          #define IDC20_GRP1 2001
          #define IDC20_BTN1 2002
          #define IDC20_UDC1 1001
          
          MainDlg DIALOGEX 6,5,129,181
          CAPTION "Test"
          FONT 9,"Verdana",400,0,0
          STYLE WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|DS_CENTER
          BEGIN
            CONTROL "None",IDC20_GRP1,"Button",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,3,1,123,24
            CONTROL "Draw",IDC20_BTN1,"Button",WS_CHILD|WS_VISIBLE,43,156,42,17
            CONTROL "",IDC20_UDC1,"Static",WS_CHILD|WS_VISIBLE|WS_BORDER|SS_BITMAP,7,41,111,106
          END
          Yes ICON DISCARDABLE "interface.ico" //"ms.ico"
          Your demo code as promised...
          Code:
          #COMPILE EXE '"Test.exe"
          #DIM ALL
          #INCLUDE "Win32API.INC"
          #RESOURCE "myresource.pbr"
          
          %IDC20_GRP1  = 2001
          %IDC20_BTN1  = 2002
          %IDC20_UDC1  = 1001
          
          GLOBAL ghInst, ghDlg AS LONG
          GLOBAL Mehwnd, PassDlg AS LONG
          GLOBAL hWnd, Pid, idThread, hProcess AS DWORD
          
          
          GLOBAL hForm1        AS DWORD
          GLOBAL hPicture1hWnd AS DWORD
          GLOBAL hPicture1hDC  AS DWORD
          GLOBAL gUpdate       AS LONG
          GLOBAL hBit          AS DWORD
          GLOBAL memDC         AS DWORD
          
          
          '------------------------------------------------------------------------------
          '
          '------------------------------------------------------------------------------
          SUB PaintGraph(BYVAL myNumber AS STRING)
          
              DIM i AS LONG, gHeight!
              LOCAL Res$, T4$, hDC AS LONG
              LOCAL hBrush AS LONG, hOldBrush AS LONG
              LOCAL hPen AS LONG, hOldPen AS LONG, x AS SINGLE, y AS SINGLE
              LOCAL skip AS SINGLE, r AS rect
              LOCAL nLeftRect AS LONG,nTopRect AS LONG,nRightRect AS LONG, nBottomRect AS LONG
          
              LOCAL rc AS RECT
          
              T4$ = myNumber
          
              CALL GetClientRect(hPicture1hWnd, r)
          
              '*Update the Static Control
              IF gUpdate = %FALSE THEN
                 i = invalidateRect(hPicture1hWnd, r, %FALSE)
                 CALL UpdateWindow(hPicture1hWnd)
                 gUpdate = %TRUE
              END IF
          
              '*Erase the old contents first from the virtual window
              PatBlt memdc,0,0,r.nRight,r.nBottom, %PATCOPY
          
              '*Draw the new stuff on it
              gHeight = VAL(T4$)
              IF gHeight > 100 THEN gHeight = 100
              hBrush = CreateSolidBrush( RGB(255,0,0))        ' Red
              hDc = GetDC(hPicture1hWnd)
              hPen = CreatePen(%PS_SOLID, 1, RGB(0,255,255))  ' Blue School paper lines
              hOldPen = SelectObject(memdc, hPen)
              x = r.nright - r.nleft
              y = r.nbottom - r.ntop
              skip = y / 10
              FOR i = y - skip TO r.ntop + skip STEP - skip '10
                  CALL MoveTo(memdc, r.nleft, i)
                  CALL LineTo(memdc,x,i)
              NEXT
              hOldBrush = SelectObject(memdc, hBrush)
              nLeftRect = x / 3
              nTopRect = y - (y * gHeight / 100)
              nRightrect = nLeftRect * 2
              nBottomRect  = y
          
              rc.nleft   = nLeftRect
              rc.ntop    = nTopRect
              rc.nright  = nRightRect
              rc.nbottom = nBottomRect
          
              CALL FillRect(memdc,rc,hBrush) 'Rectangle with no border
          
              SelectObject memdc, hOldBrush
              DeleteObject hBrush
              SelectObject memdc, hOldPen
              DeleteObject hPen
          
              '*Paint it onto the Real DC
              BitBlt hDC, r.ntop,r.nleft,r.nright,r.nbottom,memdc,0,0,%SRCCOPY
          
              CALL ReleaseDC(hPicture1hWnd, hDc)
          
          END SUB
          
          
          
          '------------------------------------------------------------------------------
          ' Main Dialog procedure...
          '------------------------------------------------------------------------------
          CALLBACK FUNCTION MainWndProc
          
              LOCAL ps AS PAINTSTRUCT, hDc AS LONG
              LOCAL r AS RECT
              LOCAL hBrush AS LONG
          
              '---
              SELECT CASE CBMSG
          
                  '---
                  CASE %WM_INITDIALOG
          
                      hPicture1hWnd = GetDlgItem(CBHNDL,%IDC20_UDC1)
                      CALL GetClientRect(hPicture1hWnd, r)
          
                      '*create a virtual window
                      hdc = GetDC(hPicture1hWnd)
                      memdc = CreateCompatibleDC(hdc)
                      hbit = CreateCompatibleBitmap(hdc, r.nRight, r.nBottom)
                      SelectObject memdc, hbit
                      hbrush = GetStockObject(%WHITE_BRUSH)
                      SelectObject memdc, hbrush
                      DeleteObject hbrush
                      PatBlt memdc,0,0,r.nRight,r.nBottom, %PATCOPY
                      ReleaseDC hPicture1hWnd,hdc
          
          
          
                  '---
                  CASE %WM_COMMAND
                      SELECT CASE LOWRD(CBWPARAM)
                          CASE %IDC20_BTN1
                              '--Draw something...
                              CALL PaintGraph("60")
                      END SELECT
          
                  '---
                  CASE %WM_PAINT
                      '--to make it persistant, draw here...
                      'hDc = BeginPaint(hForm1, ps)
                         CALL PaintGraph("10")
                      'EndPaint hForm1, ps
          
                  '---
                  CASE %WM_CLOSE
                      DestroyWindow CBHNDL
          
                  '---
                  CASE %WM_DESTROY
                      DeleteDC memdc
                      DeleteObject hbit
                      PostQuitMessage 0
          
          
              END SELECT
          END FUNCTION
          
          '------------------------------------------------------------------------------
          ' Main entry point...
          '------------------------------------------------------------------------------
          FUNCTION WINMAIN (BYVAL CurInst AS DWORD, _
                            BYVAL PrvInst AS DWORD, _
                            BYVAL CmdLine AS ASCIIZ PTR, _
                            BYVAL CmdShow AS LONG) AS LONG
          
              '---this dialog is from our resource...
              ghInst = CurInst
              DialogBox ghInst, "MainDlg", %HWND_DESKTOP, CODEPTR(MainWndProc)
          
          
          END FUNCTION
          Last edited by Jules Marchildon; 31 Mar 2008, 09:58 PM.
          Best regards
          Jules
          www.rpmarchildon.com

          Comment


          • #6
            Jules, after modify the code, this issue has been resolved. Thank you.
            This is the code
            Code:
            '---Function, For example call: LineToHdc(GetDlgItem(Mehwnd, %IDC20_IMG1), x1, y1, x2, y2, 1, RGB(0, 0, 0))
            Function LineToHdc(ByVal Hwnd As Dword, ByVal xStart As Dword, ByVal yStart As Dword, ByVal xEnd As Dword, ByVal yEnd As Dword, _
                               ByVal nSize As Dword, ByVal nColor As Dword) As Long
                Local hDC  As Dword
                Local hPen As Dword
                'Local ps   As PAINTSTRUCT
             
                ' Get the Device Context
                hDC  = GetDC(Hwnd)  '<-- hDc = BeginPaint(Hwnd, ps)
                hPen = SelectObject(hDC, CreatePen(%PS_SOLID, nSize, nColor))
             
                ' Create a Line
                MoveTo hDC, xStart, yStart
                LineTo hDC, xEnd, yEnd
             
                ' Cleanup
                DeleteObject hPen
                ReleaseDC(Hwnd, hDC)  '<--- EndPaint(Hwnd, ps)
            End Function
            Last edited by Evan Chau; 1 Apr 2008, 03:34 AM.

            Comment

            Working...
            X