PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Re-use pWindow to create window multiple times  (Read 426 times)

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8613
  • Windows 10
    • PlanetSquires Software
Re-use pWindow to create window multiple times
« on: July 10, 2018, 10:40:08 PM »

Hi Jose,

I encountered a behavior with CWindow that I'd like your opinion on. It appears that the life-cycle of a window created with pWindow (CWindow) can only handle creating the window exactly once. If you DIM pWindow, create the window, destroy the window (and set its hWindow to Null), create the window a second time which will then result in an error. This is because you can not manually set the window handle to null. If you change the code to allow manually setting the window handle to null then it will still fail because CWindow attempts to RegisterClass a second time resulting in the Atom = 0 error. Therefore, it seems that in order to recreate the same window over and over you need to DIM pWindow, create the window, destroy the window, DELETE pWindow (which will unregister the class in the destructor).

In my case, I want to keep the pWindow variable alive through multiple creation/destroy of the window. Basically, re-use the pWindow variable over and over.

I had to make two changes to CWindow to make this happen:

(1) Modify the Set for the hWindow.

Code: [Select]
' =====================================================================================
' Sets the window handle
' =====================================================================================
Private Property CWindow.hWindow (ByVal HWnd As HWnd)
'   IF m_hwnd = NULL THEN m_hwnd = hwnd
   m_hwnd = HWnd
End Property
' =====================================================================================

(2) Modify the Create code to test for an existing Atom.

Code: [Select]
   If m_wAtom = 0 Then
      m_wAtom = CWindow_RegisterClass(m_wszClassName, m_hInstance, lpfnWndProc)
      ' If m_wAtom = 0 Then Exit Function
   End If

What do you think? Would you be okay with changing the code of CWindow to allow the behaviour I've described above?

Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

Josť Roca

  • Moderator
  • Master Member
  • *****
  • Posts: 3103
    • Jos
Re: Re-use pWindow to create window multiple times
« Reply #1 on: July 11, 2018, 12:05:33 AM »

And what about if the window has child controls, or an attached scrollable window class, or an attached accelerator table, or MDI windows, or tab pages, or properties, or extra bytes, or it is subclassed... We will need to destroy the window, clean the resources and reset the variables. Then you should able to create a new window using the same pWindow pointer.

Code: [Select]
IF m_hwnd <> NULL THEN
   ' // Destroy the window
   DestroyWindow m_hwnd
   ' // Free resources
   IF m_hFont THEN .DeleteObject m_hFont
   IF m_hAccel THEN .DestroyAcceleratorTable(m_hAccel)
   IF m_wszClassName <> "" THEN .UnregisterClassW(m_wszClassName, m_hInstance)
   IF m_hRichEditLib THEN FreeLibrary m_hRichEditLib
   IF m_pScrollWindow THEN Delete m_pScrollWindow
   ' // Reset variables and arrays
   m_hFont = NULL
   m_wszClassName = ""
   m_hRichEditLib = NULL
   m_pScrollWindow = NULL
   m_DPI = 96
   m_rx = 1
   m_ry = 1
   m_wAtom = 0
   m_DefaultFontSize = 0
   m_wszDefaultFontName = ""
   ERASE m_rgUserData
#ifdef USEMDI
   m_hwndClient AS HWND = NULL
   DIM m_wszMDIClassName = ""
#endif
END IF
m_hwnd = hwnd
« Last Edit: July 11, 2018, 01:03:38 AM by Josť Roca »
Logged

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8613
  • Windows 10
    • PlanetSquires Software
Re: Re-use pWindow to create window multiple times
« Reply #2 on: July 11, 2018, 11:06:03 AM »

Hmmm... good point. I think it may be better for me to re-evaluate my design rather than change the workings of CWindow as it stands now. Thanks for the info.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8613
  • Windows 10
    • PlanetSquires Software
Re: Re-use pWindow to create window multiple times
« Reply #3 on: July 11, 2018, 09:08:37 PM »

I was able to change my approach in my program.

Jose - is your WinFBX repository on GitHub up to date? I am putting together a winfbe release and want to ensure that I have your latest code.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

Josť Roca

  • Moderator
  • Master Member
  • *****
  • Posts: 3103
    • Jos
Re: Re-use pWindow to create window multiple times
« Reply #4 on: July 11, 2018, 09:35:32 PM »

Yes, it is. The changes that I have made since version 1.0.02 are the following:

10 Jul 2018 - Version 1.0.03
- CXputton: Added support for the BM_CLICK message.

6 Jul 2018 - Version 1.0.03
- CWSTR: Changed CONSTRUCTOR (BYVAL nChars AS UINT, BYVAL nCodePage AS UINT)
to CONSTRUCTOR (BYVAL nChars AS UINT, BYVAL bClear AS BOOLEAN)
The nCodePage parameter was no longer useful and the new bClear parameter allows to specify
if the memory will be intialized (cleared) or not.
- The default constructor now initializes the memory.

4 Jul 2018 - Version 1.0.03
- CWSTR: Changed the [] operator from one-based index to zero-based index.

2 Jul 2018 - Version 1.0.03
- CTextStream.inc: Added OpenForInputA / W, OpenForOutputA / W and OpenForAppendA / W methods.

1 Jul 2018 - Version 1.0.03
- Modified CXpButton.inc to add properties.

29 Jun 2018 - Version 1.0.03
- Removed the 80 characters limitation to the AfxAddTooltip and AfxSetTooltipText functions.
- Added CONST to the string parameters of several procedures.

27 Jun 2018 - Version 1.0.03
- CxpButton: Added DPI awareness to the text and image margins.

24 Jun 2018 - Version 1.0.03
- CWSTR.inc and CVAR.inc: Changed the casting from BYREF AS WSTRING to BYREF AS CONST WSTRING.

22 Jun 2018 - Version 1.0.03
- AfxWin.inc: Added the function AfxCommand.

20 Jun 2018 - Version 1.0.03
- AfxGetWindowLocation: Changed parameters from AS LONG PTR to BYREF AS LONG.

17 Jun 2018 - Version 1.0.03
- Modified CXpButton.inc to allow coloured buttons.

16 Jun 2018 - Version 1.0.03
- Typo: LietView_GetTooltipsFont changed to ListView_GetTooltipsFont.

31 May 2018 - Version 1.0.03
- Modified AfxOpenFileDialog again to check for double double null only if the flag
OFN_ALLOWMULTISELECT is used.
- Modified the FilepPath method of the CFindFile class to use the Root method instead of
the GetFullPathName API function.

28 May 2018 - Version 1.0.03
- Modified the AfxOpenFileDialog and AfxSaveFileDialog functions because they GPFed with
the 64 bit compiler. Also changed an instruction in AfxOpenFileDialog that prevented to work
with non Latin alphabets.

26 May 2018 - Version 1.0.03
- Modified the internal code of several functions that used INSTR with CWSTR variables.

11 May 2018 - Version 1.0.03
- Modified the AfxStrLSet, AfxStrRset and AfxStrCSet functions because they GPFed with
the 64 bit compiler.

6 Jan 2018 - Version 1.0.03
- Added two overloaded AfxStrRemove functions.

30 Dec 2017 - Version 1.0.03
- Added two overloaded AfxStrExtract functions.
« Last Edit: July 11, 2018, 11:09:26 PM by Josť Roca »
Logged

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8613
  • Windows 10
    • PlanetSquires Software
Re: Re-use pWindow to create window multiple times
« Reply #5 on: July 11, 2018, 10:16:25 PM »

Perfect - thanks, I will update my files in the distribution.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor