PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: toolbox with strange characters  (Read 265 times)

jermy

  • Junior Member
  • **
  • Posts: 68
toolbox with strange characters
« on: December 21, 2020, 06:07:56 PM »

Dear people

What is wrong in this code causing strange characters on the form?
it also displays half buttons

Code: [Select]
#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

' // Menu identifiers
ENUM   
      IDM_NEW       =  1001   
      IDC_ITEM1     =  1002   
      IDC_ITEM2     =  1003   
END ENUM


 Dim Shared  lpToolbarProc As WNDPROC
 Dim Shared  hTBtoolbar    As HWND

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

' // Forward declaration
declare FUNCTION ToolBox_WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Declare Sub ToolbarAddButton( hwndOwner As HWND, idCommand As UInteger, lpTitle  As LPSTR, dwStyle As Byte  )

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
   AfxSetProcessDPIAware

   ' // Create the main window
DIM pToolBox AS CWindow
    pToolbox.Create( Null, "Toolbox", @ToolBox_WndProc, 125, 20, 150, 450, _
    WS_OVERLAPPEDWINDOW OR WS_CLIPCHILDREN OR WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT OR WS_EX_WINDOWEDGE or WS_EX_TOOLWINDOW)

  '// Create the toolbox
DIM hTBToolBar AS HWND = pToolBox.AddControl( TOOLBARCLASSNAME, pToolBox.hWindow, , , , 350, 150, 650, _
    WS_VISIBLE Or WS_CHILD Or CCS_TOP Or TBSTYLE_LIST Or CCS_ADJUSTABLE Or TBSTYLE_WRAPABLE Or TBSTYLE_FLAT )
   
        ToolbarAddButton(hTBtoolbar, IDC_ITEM1, "Item1", TBSTYLE_CHECKGROUP Or TBSTYLE_AUTOSIZE)
        ToolbarAddButton(hTBtoolbar, IDC_ITEM2, "Item2", TBSTYLE_CHECKGROUP Or TBSTYLE_AUTOSIZE)
         
     ShowWindow(pToolBox.hWindow, nCmdShow)
     UpdateWindow(pToolBox.hWindow)
   ' // Dispatch Windows messages
   FUNCTION = pToolBox.DoEvents(nCmdShow)

END FUNCTION

' ========================================================================================
'
' ========================================================================================
FUNCTION ToolBox_WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

    SELECT CASE uMsg

      CASE WM_COMMAND
         SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
         
            Case IDC_ITEM1 
? "ITEM1"
            Case IDC_ITEM2 
? "ITEM2"
            CASE IDCANCEL
               ' // If ESC key pressed, close the application sending an WM_CLOSE message
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessage hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
            CASE WM_SIZE               
                SendMessage(hTBtoolbar, TB_AUTOSIZE, 0, 0)
                 
         END SELECT

    CASE WM_DESTROY
         ' // End the application by sending an WM_QUIT message
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT
 
 
   ' // Default processing of Windows messages
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION

Sub ToolbarAddButton( Byval hWndToolbar As HWND, idCommand As UInteger, lpTitle  As LPSTR, dwStyle As Byte  )

    Static tbb      As TBBUTTON

    ' send the TB_BUTTONSTRUCTSIZE message, which is required for
    ' backward compatibility
    SendMessageW( hWndToolbar, TB_BUTTONSTRUCTSIZE, Sizeof(TBBUTTON), 0)

    ' fill the TBBUTTON array with button information, and add the  buttons to the toolbar
    tbb.idCommand = idCommand
    tbb.fsState   = TBSTATE_ENABLED
    tbb.fsStyle   = dwStyle
    tbb.dwData    = 0

    tbb.iString   = SendMessage( hWndToolbar, TB_ADDSTRING, 0, Cast(LPARAM,lpTitle) )
    tbb.iBitmap   = -2

    SendMessage ( hWndToolbar, TB_SETBUTTONSIZE, 0, MAKELONG(16, 16))
    SendMessage( hWndToolbar, TB_ADDBUTTONS, 1, Cast(DWORD_PTR, @tbb))
End Sub


Thanks for the effort
Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3294
Re: toolbox with strange characters
« Reply #1 on: December 21, 2020, 06:49:20 PM »

You're happily mixing ansi and unicode.

Code: [Select]
#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

' // Menu identifiers
ENUM   
      IDM_NEW       =  1001   
      IDC_ITEM1     =  1002   
      IDC_ITEM2     =  1003   
END ENUM


 Dim Shared  lpToolbarProc As WNDPROC
 Dim Shared  hTBtoolbar    As HWND

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

' // Forward declaration
declare FUNCTION ToolBox_WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Declare Sub ToolbarAddButton(BYVAL hwndOwner As HWND, BYVAL idCommand As UInteger, BYVAL lpTitle  As LPWSTR, BYVAL dwStyle As Byte  )

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
   AfxSetProcessDPIAware

   ' // Create the main window
DIM pToolBox AS CWindow
    pToolbox.Create( Null, "Toolbox", @ToolBox_WndProc, 125, 20, 150, 450, _
    WS_OVERLAPPEDWINDOW OR WS_CLIPCHILDREN OR WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT OR WS_EX_WINDOWEDGE or WS_EX_TOOLWINDOW)

  '// Create the toolbox
DIM hTBToolBar AS HWND = pToolBox.AddControl( TOOLBARCLASSNAME, pToolBox.hWindow, , , , 350, 150, 650, _
    WS_VISIBLE Or WS_CHILD Or CCS_TOP Or TBSTYLE_LIST Or CCS_ADJUSTABLE Or TBSTYLE_WRAPABLE Or TBSTYLE_FLAT )
   
        ToolbarAddButton(hTBtoolbar, IDC_ITEM1, "Item1", TBSTYLE_CHECKGROUP Or TBSTYLE_AUTOSIZE)
        ToolbarAddButton(hTBtoolbar, IDC_ITEM2, "Item2", TBSTYLE_CHECKGROUP Or TBSTYLE_AUTOSIZE)
         
     ShowWindow(pToolBox.hWindow, nCmdShow)
     UpdateWindow(pToolBox.hWindow)
   ' // Dispatch Windows messages
   FUNCTION = pToolBox.DoEvents(nCmdShow)

END FUNCTION

' ========================================================================================
'
' ========================================================================================
FUNCTION ToolBox_WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

    SELECT CASE uMsg

      CASE WM_COMMAND
         SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
         
            Case IDC_ITEM1
? "ITEM1"
            Case IDC_ITEM2
? "ITEM2"
            CASE IDCANCEL
               ' // If ESC key pressed, close the application sending an WM_CLOSE message
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessage hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
            CASE WM_SIZE               
                SendMessage(hTBtoolbar, TB_AUTOSIZE, 0, 0)
                 
         END SELECT

    CASE WM_DESTROY
         ' // End the application by sending an WM_QUIT message
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT
 
 
   ' // Default processing of Windows messages
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION

Sub ToolbarAddButton( Byval hWndToolbar As HWND, BYVAL idCommand As UInteger, BYVAL lpTitle  As LPWSTR, BYVAL dwStyle As Byte  )

    Static tbb      As TBBUTTON

    ' send the TB_BUTTONSTRUCTSIZE message, which is required for
    ' backward compatibility
    SendMessageW( hWndToolbar, TB_BUTTONSTRUCTSIZE, Sizeof(TBBUTTON), 0)

    ' fill the TBBUTTON array with button information, and add the  buttons to the toolbar
    tbb.idCommand = idCommand
    tbb.fsState   = TBSTATE_ENABLED
    tbb.fsStyle   = dwStyle
    tbb.dwData    = 0

    tbb.iString   = SendMessageW( hWndToolbar, TB_ADDSTRING, 0, Cast(LPARAM,lpTitle) )
    tbb.iBitmap   = -2

    SendMessageW ( hWndToolbar, TB_SETBUTTONSIZE, 0, MAKELONG(16, 16))
    SendMessageW( hWndToolbar, TB_ADDBUTTONS, 1, Cast(DWORD_PTR, @tbb))
End Sub