How to detect that window has been resized

Windows specific questions.
Carlos Herrera
Posts: 78
Joined: Nov 28, 2011 13:29
Location: Dictatorship

How to detect that window has been resized

Postby Carlos Herrera » Jun 07, 2019 22:55

Dear All,
Please consider the following program (it uses WinFB library)

Code: Select all

'===============================================================================
' How to detect that window have been resized?
'===============================================================================
#Include "WinGUI.bi"
Dim As HWND Window_Main
Dim As MSG msg
'Create a resizable window
Window_Main = Window_New   (100, 100, 600, 600, "Resizable window")
'Event loop:
Do
   WaitEvent(Window_Main, msg)
   Select Case msg.hwnd
      Case Window_Main
         Select Case msg.message
            Case WM_PAINT
               Print "window painted"
            Case WM_SIZE
               Print "window resized"
            Case Else   
         End Select
      Case Else
   End Select
Loop Until Window_Event_Close(Window_Main, msg)
End

When window is created (or recreated after mini- or maximization) the message "windows painted "
is printed in the console. However, when window in resized, nothing is printed. So, how to detect
a resize event? Thanks in advance,
Carlos
badidea
Posts: 1607
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to detect that window has been resized

Postby badidea » Jun 07, 2019 23:36

The WinFB manual says:

"Windows changes the size of a window when the user chooses window menu commands, such as Size and Maximize, or when the application calls functions, such as the SetWindowPos function. When a window changes size, Windows assumes that the contents of the previously exposed portion of the window are not affected and need not be redrawn. Windows invalidates only the newly exposed portion of the window, which saves time when the eventual WM_PAINT message is processed by the application. In this case, WM_PAINT is not generated when the size of the window is reduced.

For some windows, any change to the size of the window invalidates the contents. For example, a clock application that adapts the face of the clock to fit neatly within its window must redraw the clock whenever the window changes size. To force Windows to invalidate the entire client area of the window when a vertical, horizontal, or both vertical and horizontal change is made, an application must specify the CS_VREDRAW or CS_HREDRAW style, or both, when registering the window class. Any window belonging to a window class having these styles is invalidated each time the user or the application changes the size of the window. "

I hope you understand what this means, because I know nothing of the WinAPI.
jj2007
Posts: 1261
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to detect that window has been resized

Postby jj2007 » Jun 08, 2019 2:46

The reason why you don't see this is another: Somebody decided that the poor FB coder should not see all those overwhelmingly complex messages, and therefore wisely excluded some of them from WaitEvent. You can override this by using a standard template as shown below, but that is Windows only, of course. Compile as console application to see the message.

Code: Select all

#Include "windows.bi"
Dim Shared as Handle hEdit
Function WndProc(hWnd As HWND, msg As  UINT, wParam As WPARAM, lParam As LPARAM) As LRESULT
  Dim As RECT rc
  Dim As PAINTSTRUCT ps
  Dim As HANDLE PtDC
  Select Case msg
  Case WM_CREATE
   hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "Hello, I am an edit control",_
   WS_CHILD Or WS_VISIBLE or ES_MULTILINE, 0, 0, 100, 100, hWnd, 100, 0, 0)
  Case WM_PAINT
   PtDC=BeginPaint(hWnd, @ps)
   TextOut(PtDC, 3, 3, "TextOut in the WM_PAINT handler", 31)
   EndPaint(hWnd, @ps)
  Case WM_KEYDOWN
     if wParam=VK_ESCAPE then SendMessage(hWnd, WM_CLOSE, 0, 0)
  Case WM_SIZE
        print "SIZED"
   GetClientRect(hWnd, @rc)
   MoveWindow(hEdit, 3, 23, rc.right-6, rc.bottom-26, 0)
  Case WM_DESTROY
      PostQuitMessage(0)
  End Select
  return DefWindowProc(hwnd, msg, wParam, lParam)
End Function

Function WinMain(hInstance As HINSTANCE, hPrevInstance As HINSTANCE, lpCmdLine As LPSTR, nShowCmd As Integer) As Integer
   Dim As WNDCLASSEX wc
   Dim As MSG msg
   Dim As string classname="FbGui"
   wc.cbSize = sizeof(WNDCLASSEX)
   wc.hbrBackground = COLOR_BTNFACE+1
   wc.hCursor = LoadCursor(0, IDC_ARROW)
   wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION)
   wc.hIconSm = wc.hIcon
   wc.hInstance = hInstance
   wc.lpfnWndProc = @WndProc
   wc.lpszClassName = StrPtr(classname)
   wc.style = CS_HREDRAW Or CS_VREDRAW
   RegisterClassEx(@wc)

   if CreateWindowEx(0, wc.lpszClassName, "Hello World",_
   WS_OVERLAPPEDWINDOW Or WS_VISIBLE, (GetSystemMetrics(SM_CXSCREEN) / 2) - 150,_
   (GetSystemMetrics(SM_CYSCREEN) / 2) - 150, 300, 300, 0, 0, hInstance, 0)=0 then
          MessageBox(0, "Creating hMain failed miserably", 0, MB_OK)
          return 0
   End If

   While GetMessage(@msg, 0, 0, 0)
      TranslateMessage(@msg)
      DispatchMessage(@msg)
   Wend

   return msg.wParam
End Function
WinMain(GetModuleHandle(NULL), NULL, COMMAND(), SW_NORMAL)
Carlos Herrera
Posts: 78
Joined: Nov 28, 2011 13:29
Location: Dictatorship

Re: How to detect that window has been resized

Postby Carlos Herrera » Jun 08, 2019 8:33

Thank you JJ in the name of miserable FB coders. I will try to squeeze your solution into WinGUI framework.
BTW, when compiling your program I have got the following warnings (1.05).

Code: Select all

..warning 1(1): Passing scalar as pointer, at parameter 10 of CREATEWINDOWEX()
..warning 4(1): Suspicious pointer assignment

Carlos
jj2007
Posts: 1261
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to detect that window has been resized

Postby jj2007 » Jun 08, 2019 9:21

Carlos Herrera wrote:BTW, when compiling your program I have got the following warnings (1.05).

Code: Select all

..warning 1(1): Passing scalar as pointer, at parameter 10 of CREATEWINDOWEX()
..warning 4(1): Suspicious pointer assignment
Yes, we had that discussion before. FreeBasic and Windows aren't the best friends. Just ignore such warnings.
dodicat
Posts: 6012
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to detect that window has been resized

Postby dodicat » Jun 08, 2019 10:48

In windows.bi these two entities causing the warnings are set as pointers.
hbrush and hmenu.
You could cast each in turn to a pointer
cptr(any ptr,COLOR_BTNFACE+1)
cast(any ptr,100)
This will kill the warnings.
If I artificially make the second last parameter of the code below a long pointer (should just be a long) I create a warning from fb.

Code: Select all


declare function msgbox lib "user32.dll" alias "MessageBoxTimeoutA"(as any ptr,as zstring,as zstring,as long,as long ptr,as long)  as long


msgbox(0,"Wait 3 seconds","sorry for the three second warning",0,100,3000)

msgbox(0,"Wait 2 seconds","warning gone",0,cast(any ptr,100),2000)

sleep
 

So it all depends on who wrote the .bi file and the wisdom therein.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 1 guest