On Restore Events

Moderator: Rathinagiri

Post Reply
User avatar
Pablo César
Posts: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

On Restore Events

Post by Pablo César » Thu May 01, 2014 1:12 am

Seems is missing this event in HMG.

I mean restoring a form or window to its original size. To minimize, maximize, and restore a window or form, change the value of the WindowState OnRestore property.

Here is one C++ code
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 1949
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 31 times
Been thanked: 119 times
Contact:

Post by srvet_claudio » Thu May 01, 2014 3:05 pm

See this code:

Code: Select all

#include "hmg.ch"

Function Main()

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      MAIN
      
      @ 70,70 BUTTON Button_1 CAPTION "OK" ACTION MsgInfo ("OK")

   END WINDOW

   CREATE EVENT PROCNAME OnRestore HWND Form_1.HANDLE
   
   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return


#define WM_SYSCOMMAND 274
#define SC_RESTORE 0xF120

FUNCTION OnRestore
   IF EventMSG() == WM_SYSCOMMAND .AND. EventWPARAM() == SC_RESTORE
      MsgInfo ("Restore")
   ENDIF
RETURN NIL


Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

Post by Pablo César » Thu May 01, 2014 3:28 pm

:o Thank you master !! :D Works ok in UCI.

This will be usefull... But what would be ideal for me in this case with FMG_Editor is able to rely on that event were available internally. Just as it has with OnMaximize and OnMinimize.

I also shall adapt this (I do not know now) for using not during the event, shall it be after this happen. Because I need when this event ended (after restored window) to call then the function that I want to insert.

Thank you Claudio for your contribution. You are great ! :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 1949
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 31 times
Been thanked: 119 times
Contact:

Post by srvet_claudio » Thu May 01, 2014 7:05 pm

Pablo César wrote:I also shall adapt this (I do not know now) for using not during the event, shall it be after this happen. Because I need when this event ended (after restored window) to call then the function that I want to insert.

Code: Select all

#include "hmg.ch"

Function Main()

PUBLIC Flag_MinMax  := .F.

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      ON MINIMIZE (Flag_MinMax := .T.);
      ON MAXIMIZE (Flag_MinMax := .T.);
      ON SIZE OnSize();
      MAIN
      
      @ 70,70 BUTTON Button_1 CAPTION "OK" ACTION MsgInfo ("OK")

   END WINDOW

   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return

FUNCTION OnSize
   IF Flag_MinMax == .T.
      MsgInfo ("Restore")
   ENDIF
   Flag_MinMax := .F.
RETURN NIL



Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

Post by Pablo César » Thu May 01, 2014 7:05 pm

I prepare this other example based on your one:

Code: Select all

#include "hmg.ch"

Function Main()

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      MAIN
      
      @ 70,70 BUTTON Button_1 CAPTION "OK" ACTION ( Form_1.Maximize, DrawSnapGrid(5) )

   END WINDOW

   CREATE EVENT PROCNAME OnRestore HWND Form_1.HANDLE
   
   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return


#define WM_SYSCOMMAND 274
#define SC_RESTORE 0xF120

FUNCTION OnRestore
   IF EventMSG() == WM_SYSCOMMAND .AND. EventWPARAM() == SC_RESTORE
      nIndex := EventINDEX ()
      EventSTOP (nIndex, .T.)
	  _RestoreWindow ( "Form_1" )
	  DrawSnapGrid (5)
      EventSTOP (nIndex, .F.) 
   ENDIF
RETURN NIL

Function DrawSnapGrid (nPx)
Local i, j, fHandle, hDC
Local nH, nW

Default nPx := 10

fHandle := GetFormHandle( "Form_1" )
nH := GetProperty("Form_1","Height")
nW := GetProperty("Form_1","Width")
hDC := GetDC( fHandle )

For i := nPx TO nW STEP nPx
    For j := nPx TO nH STEP nPx
        SetPixel( hDC, i, j, RGB( 0, 0, 0 ) )
    Next
Next
ReleaseDC( fHandle, hDC )
Return Nil

#pragma BEGINDUMP

#include <windows.h>

HB_FUNC( SETPIXEL )
{
   hb_retnl( (ULONG) SetPixel( (HDC) hb_parnl( 1 ) ,
				hb_parni( 2 )      ,
				hb_parni( 3 )      ,
			(COLORREF) hb_parnl( 4 ) ) );
}
#pragma ENDDUMP
Claudio, is it correct the way I done ? Or _RestoreWindow ( "Form_1" ) is being executed twice ?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 1949
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 31 times
Been thanked: 119 times
Contact:

Post by srvet_claudio » Thu May 01, 2014 7:28 pm

Pablo César wrote:I prepare this other example based on your one:

Code: Select all

#include "hmg.ch"

Function Main()

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      MAIN
      
      @ 70,70 BUTTON Button_1 CAPTION "OK" ACTION ( Form_1.Maximize, DrawSnapGrid(5) )

   END WINDOW

   CREATE EVENT PROCNAME OnRestore HWND Form_1.HANDLE
   
   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return


#define WM_SYSCOMMAND 274
#define SC_RESTORE 0xF120

FUNCTION OnRestore
   IF EventMSG() == WM_SYSCOMMAND .AND. EventWPARAM() == SC_RESTORE
      nIndex := EventINDEX ()
      EventSTOP (nIndex, .T.)
	  _RestoreWindow ( "Form_1" )
	  DrawSnapGrid (5)
      EventSTOP (nIndex, .F.) 
   ENDIF
RETURN NIL

Function DrawSnapGrid (nPx)
Local i, j, fHandle, hDC
Local nH, nW

Default nPx := 10

fHandle := GetFormHandle( "Form_1" )
nH := GetProperty("Form_1","Height")
nW := GetProperty("Form_1","Width")
hDC := GetDC( fHandle )

For i := nPx TO nW STEP nPx
    For j := nPx TO nH STEP nPx
        SetPixel( hDC, i, j, RGB( 0, 0, 0 ) )
    Next
Next
ReleaseDC( fHandle, hDC )
Return Nil

#pragma BEGINDUMP

#include <windows.h>

HB_FUNC( SETPIXEL )
{
   hb_retnl( (ULONG) SetPixel( (HDC) hb_parnl( 1 ) ,
				hb_parni( 2 )      ,
				hb_parni( 3 )      ,
			(COLORREF) hb_parnl( 4 ) ) );
}
#pragma ENDDUMP
Claudio, is it correct the way I done ? Or _RestoreWindow ( "Form_1" ) is being execute twice ?
This way is better:

Code: Select all

#include "hmg.ch"

Function Main()

PUBLIC flag_Restore := .F.

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      ON PAINT OnPaint();
      MAIN
      
      @ 70,70 BUTTON Button_1 CAPTION "OK" ACTION MsgInfo ("Ok")

   END WINDOW

   CREATE EVENT PROCNAME OnRestore HWND Form_1.HANDLE
   
   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return


#define WM_SYSCOMMAND 274
#define SC_RESTORE 0xF120

FUNCTION OnRestore
   IF EventMSG() == WM_SYSCOMMAND .AND. EventWPARAM() == SC_RESTORE
      flag_Restore := .T.
   ENDIF
RETURN NIL


PROCEDURE OnPaint (nPx)
LOCAL Width  := BT_ClientAreaWidth  ("Form_1")
LOCAL Height := BT_ClientAreaHeight ("Form_1")
LOCAL hDC, BTstruct

Default nPx := 10

IF flag_Restore == .T.
   hDC := BT_CreateDC ("Form_1", BT_HDC_ALLCLIENTAREA, @BTstruct)
      For i := nPx TO Width STEP nPx
          For j := nPx TO Height STEP nPx
             BT_DrawSetPixel (hDC, j, i, BLACK) 
          Next
      Next
   BT_DeleteDC (BTstruct)
ENDIF
flag_Restore := .F.

RETURN

Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

Post by Pablo César » Thu May 01, 2014 8:32 pm

Thank you Claudio. You gave good lesons and more features than I needed.

- I saw event release is also sizing.
- Adding a flag to switch when is minimize, was good idea.
- Using BT_DrawSetPixel OnPaint event, keeps grid permanently as part of form.

In FMG_Editor, I would like to switch by user option when to use grids or not.

But now I learned that making OnPaint event is the best practice to keep grids without without being harmed by the events OnMaximize, OnMinimize and OnRestore.

So, I see now that was not even necessary to control the OnRestore event.

Sorry if I gave work to you, but for me is worth as learning.

I made my final test, now knowing the best way to implement Grid on forms:

Code: Select all

#include "hmg.ch"

Function Main()

PUBLIC flag_Restore := .F.

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      ON PAINT OnPaint();
      MAIN
      
      @ 60,50 BUTTON Button_1 CAPTION "ON" ACTION (flag_Restore:=.T.,OnPaint())
	  
	  @ 90,50 BUTTON Button_2 CAPTION "OFF" ACTION (flag_Restore:=.F.,RedrawWindow(GetFormHandle("Form_1")))

   END WINDOW

   // CREATE EVENT PROCNAME OnRestore HWND Form_1.HANDLE
   
   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return


#define WM_SYSCOMMAND 274
#define SC_RESTORE 0xF120

FUNCTION OnRestore
   IF EventMSG() == WM_SYSCOMMAND .AND. EventWPARAM() == SC_RESTORE
      flag_Restore := .T.
   ENDIF
RETURN NIL


PROCEDURE OnPaint (nPx)
LOCAL Width  := BT_ClientAreaWidth  ("Form_1")
LOCAL Height := BT_ClientAreaHeight ("Form_1")
LOCAL hDC, BTstruct

Default nPx := 10

IF flag_Restore == .T.
   hDC := BT_CreateDC ("Form_1", BT_HDC_ALLCLIENTAREA, @BTstruct)
      For i := nPx TO Width STEP nPx
          For j := nPx TO Height STEP nPx
              BT_DrawSetPixel (hDC, j, i, BLACK) 
          Next
      Next
   BT_DeleteDC (BTstruct)
ENDIF
// flag_Restore := .F.
Return Nil
Perhaps the misunderstanding that happened was because I have not used our native language and the lack of greater expertise from me.

Thank you again Claudio. :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 1949
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 31 times
Been thanked: 119 times
Contact:

Post by srvet_claudio » Thu May 01, 2014 8:58 pm

Pablo César wrote:But now I learned that making OnPaint event is the best practice to keep grids without without being harmed by the events OnMaximize, OnMinimize and OnRestore.
Yes, and this code (BT_HDC_INVALIDCLIENTAREA) is more efficient for this purpose:

Code: Select all

PROCEDURE OnPaint (nPx)
LOCAL Width  := BT_ClientAreaWidth  ("Form_1")
LOCAL Height := BT_ClientAreaHeight ("Form_1")
LOCAL hDC, BTstruct

Default nPx := 10

   hDC := BT_CreateDC ("Form_1", BT_HDC_INVALIDCLIENTAREA, @BTstruct)
      For i := nPx TO Width STEP nPx
          For j := nPx TO Height STEP nPx
             BT_DrawSetPixel (hDC, j, i, BLACK) 
          Next
      Next
   BT_DeleteDC (BTstruct)

RETURN
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

Post Reply