CWindow RC10

Started by José Roca, June 10, 2016, 04:20:13 PM

Previous topic - Next topic

José Roca

Don't know which version are you using. There is not a length property, but a LEN operator.



' ========================================================================================
' Returns the length of the BSTR in characters
' ========================================================================================
OPERATOR Len (BYREF pCBStr AS CBStr) AS INTEGER
   OPERATOR = SysStringLen(pCBStr.Handle)
END OPERATOR
' ========================================================================================


Usage:


DIM cbs AS CBStr = "test string"
print len(cbs)


James Fuller

Jose,
  I don't see it in CBStr.inc in RC10?

James

José Roca

It is at the end of the file.

James Fuller

Duh...
I'm getting too old for this :)
I added PROPERTY Length any way!!!!
James

José Roca

You need to increase the DPI to get bigger fonts :)

James Fuller

Jose,
  Why do you default to a sizable window with CWindow?

James

José Roca

Just a matter of preference.

James Fuller

Quote from: Jose Roca on June 20, 2016, 04:23:10 PM
Just a matter of preference.
Jose,
  I question it because very seldom have I seen you use layout code with any of your examples.
It would stand to reason (by me anyway) that the window would/should not be sizable unless layout code is used.
And yes I know I can .... but I'm lazy and I want it to be the default :)
James



José Roca

> I question it because very seldom have I seen you use layout code with any of your examples.

These are examples to demonstrate how to create the controls. It doesn't matter if they are resizable or not.

I'm tired of seeing fixed size popup dialogs whose bottom goes outside of my monitor. I want them to be resizable and scrollable. Check if the height of your dialog would be greater than the height of the monitor and adjust it and make the contents scrollable.

I fyou want to code as you did more than ten years ago, it's your choice.


James Fuller

Jose,
  I understand what your saying but I am using YOUR DPI aware CWindow so hopefully these issues will not surface. Not all windows warrant a full control layout.
How about checking for a CW_DEFAULT_STYLE #define and use that if it is defined?

James

José Roca

If you want to make it not resizable use


   pWindow.Create(NULL, "CWindow with a button", @WndProc)
   AfxRemoveWindowStyle(pWindow.hWindow, WS_THICKFRAME)


José Roca

#26
I have been working in a class, CScrollWindow, to make windows scrollable. Tomorrow I will do more tests.

The idea is:

1. Create our window.

2. Set the needed client size to fit all the controls.

3. When the program starts, check the size of the working area.

4. If our window fits in the working area, do nothing.

5. If it is bigger, create an instance of the class passing the handle of our window.

6. Shrink the size of the window to not exceed the size of the working area.

Et voilà, the window will be scrollable. No more Ok, Apply or Cancel buttons unclickable because they're outside the working area.

James Fuller

#27
Quote from: Jose Roca on June 20, 2016, 07:47:11 PM
If you want to make it not resizable use


   pWindow.Create(NULL, "CWindow with a button", @WndProc)
   AfxRemoveWindowStyle(pWindow.hWindow, WS_THICKFRAME)


That will work just fine. Thank You.

Edit: Woops still has a max button. This works"

AfxRemoveWindowStyle(pWindow.hWindow, WS_THICKFRAME OR WS_MAXIMIZEBOX)


James

José Roca

#28
A new class for our framework. SCrollWindow allows to make a window or popup dialog scrollable.

A littlt test:


' ########################################################################################
' Microsoft Windows
' File: CW_ScrollWindow.fbtpl
' Contents: Scrollable window
' Compiler: Free Basic
' Copyright (c) 2016 Jose Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#define unicode
#INCLUDE ONCE "Afx/CWindow.inc"
#INCLUDE ONCE "Afx/AfxCtl.inc"
#INCLUDE ONCE "Afx/CScrollWindow.inc"
USING Afx.CWindowClass

#define IDC_LISTBOX 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)

DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) 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
   DIM hwndMain AS HWND = pWindow.Create(NULL, "Scrollable window", @WndProc)
   pWindow.ClassStyle = CS_DBLCLKS   ' // Change the window style to avoid flicker
   ' // Set a client size big enough to display all the controls
   pWindow.SetClientSize(320, 335)

   ' // Add a listbox
   DIM hListBox AS HWND
   hListBox = pWindow.AddControl("ListBox", , IDC_LISTBOX)
   pWindow.SetWindowPos hListBox, NULL, 8, 8, 300, 280, SWP_NOZORDER

   ' // Fill the list box
   DIM i AS LONG, wszText AS WSTRING * 260
   FOR i = 1 TO 50
      wszText = "Item " & RIGHT("00" & STR(i), 2)
      ListBox_AddString(hListBox, @wszText)
   NEXT
   ' // Select the first item
   ListBox_SetCursel(hListBox, 0)

   ' // Add a cancel button
   pWindow.AddControl("Button", , IDCANCEL, "&Cancel", 233, 298, 75, 23)

   ' // Create an instance of the CScrollWindow class and attach the main window to it
   DIM pScrollWindow AS CScrollWindow = hwndMain
   SetPropW hwndMain,"CSCROLLWINDOWPTR", @pScrollWindow
   ' // Shrink the client size
   pWindow.SetClientSize(250, 260)
   ' // Center the window
   pWindow.Center

   

   ' // Message pump
   FUNCTION = pWindow.DoEvents(nCmdShow)

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

' ========================================================================================
' Window procedure
' ================================================================e========================
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 LOWORD(wParam)
            ' // If ESC key pressed, close the application sending an WM_CLOSE message
            CASE IDCANCEL
               IF HIWORD(wParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF

         CASE IDC_LISTBOX
            SELECT CASE HIWORD(wParam)
               CASE LBN_DBLCLK
                  ' // Get the handle of the Listbox
                  DIM hListBox AS HWND = GetDlgItem(hwnd, IDC_LISTBOX)
                  ' // Get the current selection
                  DIM curSel AS LONG = ListBox_GetCursel(hListBox)
                  ' // Get the length of the ListBox item text
                  DIM nLen AS LONG = ListBox_GetTextLen(hListBox, curSel)
                  ' // Allocate memory for the buffer
                  DIM pwszText AS WSTRING PTR = CAllocate(nLen + 1, 2)
                  ' // Get the text and display it
                  ListBox_GetText(hListBox, curSel, pwszText)
                  MessageBoxW(hwnd, pwszText, "ListBox test", MB_OK)
                  ' // Deallocate the memory used by the buffer
                  DeAllocate pwszText
                  pwszText = NULL
                  EXIT FUNCTION
            END SELECT

         END SELECT

      CASE WM_SIZE
         DIM pScrollWindow AS CScrollWindow PTR = CAST(CScrollWindow PTR, GetPropW(hwnd, "CSCROLLWINDOWPTR"))
         IF pScrollWindow THEN pScrollWindow->OnSize(wParam, lParam)
         EXIT FUNCTION

      CASE WM_VSCROLL
         DIM pScrollWindow AS CScrollWindow PTR = CAST(CScrollWindow PTR, GetPropW(hwnd, "CSCROLLWINDOWPTR"))
         IF pScrollWindow THEN pScrollWindow->OnVScroll(wParam, lParam)
         EXIT FUNCTION

      CASE WM_HSCROLL
         DIM pScrollWindow AS CScrollWindow PTR = CAST(CScrollWindow PTR, GetPropW(hwnd, "CSCROLLWINDOWPTR"))
         IF pScrollWindow THEN pScrollWindow->OnHScroll(wParam, lParam)
         EXIT FUNCTION

      CASE WM_DESTROY
         ' // Remove the property
         RemovePropW hwnd,"CSCROLLWINDOWPTR"
         ' // End the application
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to DefWindowProc
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

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


José Roca

#29
Its main use could be to make sure that our window or dialog will fit in the dimensions of the user's monitor.

Often, we need to make dialogs with many options, using all the available height. But because monitors have many different sizes and resolutions, and also the user could be using a high dpi setting, we risk that part of it becomes unreachable to the user.

ScrollWindow allows us to make a window or dialog scrollable easily. At runtime, we can check if the size of our dialog will exceed the size of the user monitor and make the window scrollable by adjusting the size of the window or dialog to fit the dimensions of the working area of the monitor.