• Welcome to PlanetSquires Forums.
 

Getclientrect

Started by Petrus Vorster, May 20, 2018, 01:42:22 PM

Previous topic - Next topic

Petrus Vorster

hi Guys

An old calculation is eluding me.
If i need to set a picturebox centre of a form during form resize how do i go about it again?
I remember it was Getclientrect of the main from and the same with the picturebox, but I cannot find that darn example on how to proceed from there!

_Regards, Pete
-Regards
Peter

Pierre Bellisle

Hey Petrus,
WM_SIZE is your friend...


#Define JumpCmd "<S howInfo>"
#Define JumpCompiler "<D:\Free\32\fbc.exe>"
#Define JumpCompilerCmd "<-s gui -w pedantic "D:\Free\bas\~~Default.rc">"

#Print Control resize
#Print Coded on FreeBASIC 1.05.0
#Ifdef __FB_64BIT__
  #Print  64bit compiler used
#Else
  #Print  32bit compiler used
#EndIf
'_____________________________________________________________________________

#Define unicode

#Include Once "windows.bi"
#Include Once "win\shellapi.bi" 'Extrac icon

#Define AppName    "Control resize"

Const ButtonExit = 101
Const Static01   = 201

Dim Shared As HINSTANCE hInstance : hInstance = GetModuleHandle(NULL)
'_____________________________________________________________________________

Function WndProc(ByVal hWnd As HWND, ByVal uMsg As UINT, ByVal wParam As WPARAM, ByVal lParam As LPARAM) As Integer
Static hFont       As HFONT
Static hStatic     As HWND
Static hButtonExit As HWND

Select Case uMsg

   Case WM_CREATE
     Function = 0

     'Get Windows default font - - - - - - - - - - - - - - - - - - - - - - - -
     Dim As NONCLIENTMETRICS NotClientMetrics : NotClientMetrics.cbSize = SizeOf(NONCLIENTMETRICS)
     SystemParametersInfo(SPI_GETNONCLIENTMETRICS, NotClientMetrics.cbSize, @NotClientMetrics, 0)
     hFont = CreateFontIndirect(@NotClientMetrics.lfMessageFont)

     'Create a static control
     hStatic      = CreateWindowEx(WS_EX_CLIENTEDGE , "Static", "S t a t i c", _
                                   WS_CHILD Or WS_VISIBLE Or SS_CENTER Or WS_TABSTOP Or _
                                   SS_NOTIFY Or BS_TEXT Or SS_CENTERIMAGE, _
                                   50, 25, 200, 30, _
                                   hWnd, Cast(HMENU, ButtonExit), _
                                   hInstance, NULL)
     SendMessage(hStatic, WM_SETFONT, Cast(WPARAM, hFont), TRUE)

     'Create exit button
     hButtonExit  = CreateWindowEx(0, "Button", "E&xit", _
                                   WS_CHILD Or WS_VISIBLE Or BS_CENTER Or WS_TABSTOP Or _
                                   BS_NOTIFY Or BS_TEXT Or BS_VCENTER, _
                                   110, 70, 80, 30, _
                                   hWnd, Cast(HMENU, ButtonExit), _
                                   hInstance, NULL)
     SendMessage(hButtonExit, WM_SETFONT, Cast(WPARAM, hFont), TRUE)

    Case WM_COMMAND
     Select Case LoWord(wParam)
        Case ButtonExit
          If HiWord(wParam) = BN_CLICKED Then
            PostMessage(hWnd, WM_CLOSE, 0, 0)
            Exit Function
          EndIf
     End Select

   Case WM_SIZE
     If wParam <> SIZE_MINIMIZED Then
       Dim As LONG SizeX = LOWORD(LPARAM)
       Dim As LONG SizeY = HIWORD(LPARAM)
       MoveWindow(hStatic, 50, 25, SizeX - 100, SizeY - 80, TRUE)
       MoveWindow(hButtonExit, SizeX / 2 - 50, SizeY - 40, 110, 30, TRUE)
     End IF

   Case WM_DESTROY
     DeleteObject(hFont)
     PostQuitMessage(0)
     Exit Function

End Select

Function = DefWindowProc(hWnd, uMsg, wParam, lParam)

End Function
'_____________________________________________________________________________

Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInst As HINSTANCE, _
                 ByVal CmdLine As WString Ptr, ByVal CmdShow As Integer) As UINT
Dim WinClass   As WNDCLASS
Dim wMsg       As MSG
Dim hWnd       As HWND
Dim hIco       As HICON
Dim WindowSize As SIZEL
Dim wsAppName  As WString * 128

wsAppName              = AppName & " - " & SizeOf(UInteger) * 8
WindowSize.cx          = 300
WindowSize.cy          = 150
hIco                   = ExtractIcon(hInstance, "Shell32.dll", 80)
WinClass.style         = CS_HREDRAW Or CS_VREDRAW
WinClass.lpfnWndProc   = ProcPtr(WndProc)
WinClass.cbClsExtra    = 0
WinClass.cbWndExtra    = 0
WinClass.hInstance     = hInstance
WinClass.hIcon         = hIco
WinClass.hCursor       = LoadCursor(NULL, IDC_ARROW)
WinClass.hbrBackground = Cast(HGDIOBJ, COLOR_BTNFACE + 1) 'Default color
WinClass.lpszMenuName  = NULL
WinClass.lpszClassName = @wsAppName

If (RegisterClass(@WinClass)) Then
   hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, _
                         wsAppName, wsAppName, _
                         WS_OVERLAPPED OR WS_CLIPCHILDREN Or WS_DLGFRAME Or WS_BORDER Or _
                         WS_VISIBLE Or WS_CAPTION Or WS_MINIMIZEBOX Or WS_SIZEBOX Or WS_SYSMENU , _
                         (GetSystemMetrics(SM_CXSCREEN) - WindowSize.cx) / 2, _ 'PosH
                         (GetSystemMetrics(SM_CYSCREEN) - WindowSize.cy) / 2, _ 'PosV
                         WindowSize.cx, WindowSize.cy, _ 'Width, height
                         NULL, NULL, hInstance, NULL)

   ShowWindow(hWnd, SW_SHOW)
   UpdateWindow(hWnd)

   While GetMessage(@wMsg, ByVal NULL, 0, 0) > 0
     If IsDialogMessage(hWnd, @wMsg) = 0 Then
       TranslateMessage(@wMsg)
       DispatchMessage(@wMsg)
     End If
   Wend

  End If
  DestroyIcon(hIco)
  Function = wMsg.message

End Function
'_____________________________________________________________________________

End WinMain(hInstance, NULL, Command(), SW_NORMAL) 'Call main() and return the error code to the OS
'_____________________________________________________________________________
'

Pierre Bellisle

I guess that some PowerBASIC code will be more appropriate...


#COMPILE EXE '#Win 9.07#
#DIM ALL
#REGISTER NONE
#INCLUDE "Win32Api.inc"
#RESOURCE "D:\Basic\Bas\~~PbrDefault00.pbr"

GLOBAL hDlg AS DWORD

$AppName  = "Control resize"
%Static01 = 101
%Button01 = 201
'_____________________________________________________________________________

CALLBACK FUNCTION DlgProc

SELECT CASE CBMSG

   CASE %WM_COMMAND
     SELECT CASE CBCTL
       CASE %Button01
         IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
           DIALOG END hDlg
         END IF
     END SELECT

   CASE %WM_SIZE 'Dialog size have changed
     IF CBWPARAM <> %SIZE_MINIMIZED THEN
       LOCAL ClientSizeX AS LONG
       LOCAL ClientSizeY AS LONG

       ClientSizeX = LO(WORD, CBLPARAM)
       ClientSizeY = HI(WORD, CBLPARAM)
       MoveWindow(GetDlgItem(hDlg, %Static01), 25, 10, ClientSizeX - 50, ClientSizeY - 55, %TRUE)
       MoveWindow(GetDlgItem(hDlg, %Button01), ClientSizeX / 2 - 41, ClientSizeY - 35, 85, 30, %TRUE)
       InvalidateRect(hDlg, BYVAL %NULL, %TRUE)
     END IF

  END SELECT

END FUNCTION
'_____________________________________________________________________________

FUNCTION PBMAIN()
LOCAL hIcon AS DWORD

DIALOG FONT "Segoe UI", 9
DIALOG NEW %HWND_DESKTOP, $AppName, , , 200, 65, _
%WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_SIZEBOX OR %WS_SYSMENU, 0 TO hDlg

SetClassLong(hDlg, %GCL_HICON, LoadIcon(GetModuleHandle(BYVAL 0), BYVAL 101)) 'Icon with numeric descriptor from rc file

hIcon = ExtractIcon(GETMODULEHANDLE(""), "%SystemRoot%\system32\PowrProf.dll", 1)
SetClassLong(hDlg, %GCL_HICONSM, hIcon)
SetClassLong(hDlg, %GCL_HICON, hIcon)

CONTROL ADD LABEL, hDlg, %Static01, "Resize control (Yours to do pixel vs DU)", _
25, 5, 150, 30, %SS_CENTER OR %SS_CENTERIMAGE OR %SS_NOTIFY, %WS_EX_CLIENTEDGE

CONTROL ADD BUTTON, hDlg, %Button01, "E&xit", 82, 45, 35, 15

DIALOG SHOW MODAL hDlg CALL DlgProc

DestroyIcon(hIcon)

END FUNCTION
'_____________________________________________________________________________
'

Petrus Vorster

Great Stuff Pierre!!!

I should really keep better records of my examples!!
Thanks for the help man, much appreciated!

-Pete
-Regards
Peter