CWindow Release Candidate 27

Started by José Roca, March 19, 2017, 05:26:35 AM

Previous topic - Next topic

José Roca

Added wrappers for the Rich Edit control.

Small modification in a couple of methods of the CTextStream class.

Modified the AfxAddTooltip, AfxSetTooltipText and AfxDeleteTooltip to check if the passed handle is a window or a child control.

David Warner


Paul Squires

Paul Squires
PlanetSquires Software

Marc Giao

Thank you very much Jose... What would we do without you ;)
Marc

José Roca

Weird. The code has been downloaded 22 times and the help file 60!

David Warner

When I downloaded the files the help file download failed and I needed to do it again. Perhaps this accounts for the disparity.

ganlinlao

hi,Jose Roca

I suggest renaming the afxmsg function to the Afxmsgbox function,and add a Title Parameter for non English speaking areas. would it be better than always showing "message" title?
The CWindow framework is a very powerful framework for freebasic use in Windows, you have done a great job, thank you very much!

José Roca

This function is intended as a quick way to display a message box, mainly for debugging purposes. This is why it uses a short name and only needs to pass the text to display. In production code, I always use the Windows API function MessageBox, that allows to specify the parent window, the text to display, the caption and the type.

Andrew Lindsay

Jose,

Thanks so much for this.  I have been mashing around my own RTF stuff for about 18 months.  I had a few glitches that seemed to be a memory leak from somewhere, but as is usually the case, your code bolts in with a minimal amount of work and just does what is says on the box.

Love your work.

José Roca

Thanks for your kind words. I have noticed that I forgot to add PRIVATE to the SUBs. I have added it in the attached file. Not very important, but it will save some bytes.


ganlinlao

hi,Jose Roca

Why not encapsulate an easy-to-use thread class and add it to the CWindow frame?
Please forgive me for my reckless request.

thank you very much!

ganlinlao

José Roca

I don't have expertise with threads. Maybe this thread class posted in the FB forum can be useful:
http://www.freebasic.net/forum/viewtopic.php?t=10053

José Roca

#12
Next version will include a localized Input Box dialog. Localization of the buttons is achieved by extracting the strings from the resource file of user32.dll (works also with 64 bit).

Usage example:

DIM cws AS CWSTR = AfxInputBox(hwnd, 0, 0, "InputBox test", "What's your name?", "My name is Jose")




' ########################################################################################
'                                *** INPUT BOX DIALOG ***
' ########################################################################################

' ========================================================================================
' Input box dialog
' Parameters:
' - hParent = Handle of the parent window
' - x, y = The location on the screen to display the dialog. If both are 0, the dialog
'   is centered on the screen.
' - cwsCaption = Caption of the window
' - cwsPrompt = Prompt string
' - cwsText = Text to edit
' - nLen = [opt] Maximum length of the string to edit (default = 260 characters)
' - bPassword = [opt] TRUE or FALSE. Default FALSE. Displays all characters as an
'     asterisk (*) as they are typed into the edit control.
'   Note: The maximum length is 2048 characters.
' ========================================================================================
' Forward declaration of the callback function
DECLARE FUNCTION AfxInputBoxWindowProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
' ========================================================================================
PRIVATE FUNCTION AfxInputBox (BYVAL hParent AS HWND, BYVAL x AS LONG, BYVAL y AS LONG, _
BYREF cwsCaption AS CWSTR, BYREF cwsPrompt AS CWSTR, BYREF cwsText AS CWSTR, _
BYVAL nLen AS LONG = 260, BYVAL bPassword AS BOOLEAN = FALSE) AS CWSTR

   ' // Create the window
   DIM pInputBox AS CWindow
   DIM dwStyle AS DWORD = WS_VISIBLE OR WS_CAPTION OR WS_POPUPWINDOW
   DIM dwExStyle AS DWORD = WS_EX_DLGMODALFRAME OR WS_EX_CONTROLPARENT
   DIM hInputBox AS HWND = pInputBox.Create(hParent, **cwsCaption, @AfxInputBoxWindowProc, x, y, 326, 142, dwStyle, dwExStyle)
   ' // Center the window
   IF x = 0 AND y = 0 THEN pInputBox.Center(hInputBox, hParent)
   ' // Add a label control
   pInputBox.AddControl("Label", hInputBox, -1, **cwsPrompt, 21, 10, 280, 19)
   ' // Add a TextBox control
   dwStyle = WS_VISIBLE OR WS_TABSTOP OR ES_LEFT OR ES_AUTOHSCROLL
   IF bPassWord THEN dwStyle = dwStyle OR ES_PASSWORD
   DIM hEdit AS HWND = pInputBox.AddControl("Edit", hInputBox, 101, "", 21, 33, 280, 19, dwStyle)
   ' // Add the buttons
   DIM hOkButton AS HWND = pInputBox.AddControl("Button", hInputBox, IDOK, "&Ok", 21, 72, 75, 22)
   DIM hCancelButton AS HWND = pInputBox.AddControl("Button", hInputBox, IDCANCEL, "&Cancel", 226, 72, 75, 22)
   ' // Localized strings. In the resource file of user32.dll, the OK button has
   ' // IDS_OK (801) as the identifier and the Cancel button IDS_CANCEL (801).
   DIM hUser32Instance AS HINSTANCE = GetModuleHandleW("user32.dll")
   DIM wszOk AS WSTRING * 260
   DIM cbLen AS LONG = LoadStringW(hUser32Instance, 800, @wszOk, SIZEOF(wszOk))
   IF cbLen THEN wszOk = "&" & wszOk : SendMessageW(hOkButton, WM_SETTEXT, 0, cast(LPARAM, @wszOk))
   DIM wszCancel AS WSTRING * 260
   cbLen = LoadStringW(hUser32Instance, 801, @wszCancel, SIZEOF(wszCancel))
   IF cbLen THEN wszCancel = "&" & wszCancel : SendMessageW(hCancelButton, WM_SETTEXT, 0, cast(LPARAM, @wszCancel))
   ' // Set the text and the limit
   IF nLen = 0 THEN nLen = 260
   IF nLen < 1 OR nLen > 2048 THEN nLen = 2048
   SendMessageW hEdit, EM_LIMITTEXT, nLen, 0
   IF LEN(cwsText) > nLen THEN cwsText = LEFT(**cwsText, nLen)
   SendMessageW(hEdit, WM_SETTEXT, 0, cast(LPARAM, *cwsText))
   SendMessageW(hEdit, EM_SETSEL, 0, -1)
   ' // Set the focus in the edit control
   SetFocus hEdit
   ' // Pointer to the allocated string to return the result
   DIM wszOut AS WSTRING * 2049
   SendMessageW hInputBox, WM_USER + 1, CAST(WPARAM, @wszOut), 0
   ' // Process Windows messages
   pInputBox.DoEvents
   ' // Enable the parent window
   EnableWindow hParent, CTRUE
   ' // Return the output string
   RETURN wszOut

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

' ========================================================================================
' Input box callback function.
' ========================================================================================
PRIVATE FUNCTION AfxInputBoxWindowProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

   STATIC pText AS WSTRING PTR   ' // Pointer to the string buffer

   SELECT CASE uMsg
      CASE WM_CREATE
         ' Disable parent window to make popup window modal
         EnableWindow GetParent(hwnd), FALSE
         EXIT FUNCTION
      CASE WM_USER + 1
         ' // Pointer to allocated string to return the result
         IF wParam THEN
            pText = cast(WSTRING PTR, wParam)
            EXIT FUNCTION
         END IF
      CASE WM_COMMAND
         SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
            CASE IDCANCEL
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
            CASE IDOK
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  DIM nLen AS LONG = SendMessageW(GetDlgItem(hwnd, 101), WM_GETTEXTLENGTH, 0, 0)
                  IF nLen > 2048 THEN nLen = 2048
                  nLen = SendMessageW(GetDlgItem(hwnd, 101), WM_GETTEXT, nLen + 1, cast(.LPARAM, pText))
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT
      CASE WM_CLOSE
         ' // Enables parent window keeping parent's zorder
         EnableWindow GetParent(hwnd), CTRUE
         ' // Don't exit; let DefWindowProcW perform the default action
      CASE WM_DESTROY
         ' // Close the main window
         PostQuitMessage(0)
         EXIT FUNCTION
   END SELECT

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

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


ganlinlao

#13
hi,Jose Roca
I use AfxNewCom ("Excel.Application"), but it cannot return  Excel.Application objects

How to call Excel.Application correctly?

I like others, I hope you can retain the COM-related classes in the framework, we can not use IE, we can not use Ocx, but many people have to use the Ms-offIce software, many people uses the Ms-office x64-bit Edition, which requires FreeBASIC to support and compile 64-bit tools, if Ms-office dies, and then removes COM-related classes, otherwise, COM-related classes, There are still many people who need to use it.
Your profound knowledge will bring more simple, easier, more powerful tools, making it easier to use Freebasic  for others.

Please

thanks
ganlinlao

José Roca

#14
Office applications can only be used with COM Automation, i.e. through the Invoke method of the IDispatch interface. Although their type libraries let you think that they expose dual interfaces, it isn't true. If you do calls through the VTable, the methods that should return pointers to other VTable interfaces return pointers to the automation interfaces, and if you use them, your application will crash. Besides, I don't have Office installed since many years ago. I use Libre Office, that is free.

> we can not use IE, we can not use Ocx

You can. My framework includes an OLE container that allows you to embed the WebBrowser control. Many OCXs will also work with it, although not all. I have provided several examples.

> I like others, I hope you can retain the COM-related classes in the framework

Without native support for BSTR and VARIANTs it is difficult. I tried to implement classes to support them, but I was not fully satisfied with the results. There is also the problem that variadic functions don't work with Free Basic 64 bit.