Support Forums > WinFBX - Windows Framework for FreeBASIC

DrawTextW and scaling

(1/1)

jermy:
Hello people,

i am trying to draw with drawtext in a richedit control 'hello world' it works but the text is very small.
I don't know how to scale this correctly.


--- Code: ---#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

CONST IDC_RICHEDIT = 1001

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 WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
Declare FUNCTION RichEdit_SubclassProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM, BYVAL uIdSubclass AS UINT_PTR, BYVAL dwRefData AS DWORD_PTR) AS LRESULT

' ========================================================================================
' 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

   DIM pWindow AS CWindow
   pWindow.Create(NULL, "CWindow with a Rich Edit control", @WndProc)
   pWindow.SetClientSize(500, 320)
   pWindow.Center
   
   DIM hRichEdit AS HWND = pWindow.AddControl("RichEdit", , IDC_RICHEDIT, "", 350, 250, 75, 23, , , , CAST(SUBCLASSPROC, @RichEdit_SubclassProc), IDC_RICHEDIT, CAST(DWORD_PTR, @pWindow))
   SetFocus hRichEdit
   

   ' // Dispatch Windows messages
   FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Processes messages for the subclassed RichEdit window.
' ========================================================================================
FUNCTION RichEdit_SubclassProc ( BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM, BYVAL uIdSubclass AS UINT_PTR, BYVAL dwRefData AS DWORD_PTR ) AS LRESULT

   SELECT CASE uMsg

     Case WM_PAINT
            DIM hDC AS HDC                                                                                           
            Dim As RECT  rect
                                                                                                                 
             DefSubclassProc(hwnd, uMsg, wParam, lParam)         
             
             hdc  = GetDC(hWnd)                                                                                                               
             
             SetBkMode(hdc,TRANSPARENT)
             SendMessageW( hWnd, EM_GETRECT, 0, cast(LPARAM, @rect) ) 
             
             DrawTextW(hdc,"Hello World",-1,@rect,0)
             
             DeleteDC(hdc)                       
         
      CASE WM_DESTROY
         ' // REQUIRED: Remove control subclassing
         RemoveWindowSubclass hwnd, @RichEdit_SubclassProc, uIdSubclass

   END SELECT

   FUNCTION = DefSubclassProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main window callback procedure
' ========================================================================================
FUNCTION 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)
            ' // If ESC key pressed, close the application sending an WM_CLOSE message
            CASE IDCANCEL
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE WM_SIZE
         ' // If the window isn't minimized, resize it
         IF wParam <> SIZE_MINIMIZED THEN
            ' // Resize the controls
            DIM pWindow AS CWindow PTR = AfxCWindowPtr(hwnd)
            IF pWindow THEN
               pWindow->MoveWindow GetDlgItem(hwnd, IDC_RICHEDIT), 100, 50, pWindow->ClientWidth - 200, pWindow->ClientHeight - 150, CTRUE
            END IF
         END IF

    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
' ========================================================================================


--- End code ---

it is possible to inherit the scaling, font, etc. from the RichEdit control so that the text being drawn looks the same as the typed version?

Paul Squires:
I was hoping to be able to get the current font of the control and select it into the DC, but that doesn't seem to work. Maybe because it is a RichEdit control? Maybe because RichEdit controls can have multiple fonts, it does not expose access to the WM_GETFONT message?


--- Code: ---     Case WM_PAINT
            DIM hDC AS HDC                                                                                           
            Dim As RECT  rect
                                                                                                                 
             DefSubclassProc(hwnd, uMsg, wParam, lParam)         
             
             hdc  = GetDC(hWnd)                                                                                                               
             
             dim as long state = SaveDC(hdc)
             
             dim as HFONT hFont1 = GetWindowFont(hWnd)
             
             SelectObject( hdc, hFont1 )
             
             SetBkMode(hdc,TRANSPARENT)
             SendMessageW( hWnd, EM_GETRECT, 0, cast(LPARAM, @rect) )
             
             DrawTextW(hdc,"Hello World",-1,@rect,0)
             
             RestoreDC(hdc, state)
             DeleteDC(hdc)                       

--- End code ---

I guess you could create a scaled CWindow font in WM_PAINT, select it into the DC, do your painting, restore the DC, and finally delete the created font. I haven't tried that but it should definitely work.

jermy:
I have looked at some examples in C-code, what i wanted to do is very complex. and examples are not very comprehensive.
This is still too difficult for me.

Thanks for the effort.

Navigation

[0] Message Index

Go to full version