(maxgui) refresh canvas from another window

BlitzMax Forums/BlitzMax Beginners Area/(maxgui) refresh canvas from another window

Hi !
i've the following problem. when i open the second window and move it on the first, the first canvas is not drawn correctly (see the picture).

How to resolve this ?

Thanks !




Strict

' -------------------------------------------------------------------------------------------

Extern "win32"
	Function GetActiveWindow()
	Function SetWindowPos(hWnd:Int,after:Int,x:Int,y:Int,w:Int,h:Int,flags:Int)
End Extern

Const SWP_NOSIZE:Int = $1
Const SWP_NOMOVE:Int = $2
Const HWND_TOPMOST:Int = -1
Const HWND_NOTOPMOST:Int = - 2

Function alwaysOnTop(hwnd:Int)
	SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE )
End Function

Function notAlwaysOnTop(hwnd:Int)
	SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE )
End Function
		

' -------------------------------------------------------------------------------------------


Global win1 : Tgadget = CreateWindow ("Win1",100,100,400,300,Null,WINDOW_TITLEBAR)
Global Canvas1 : Tgadget = CreateCanvas (10,40,350,200,Win1)
Global Button1 : Tgadget = CreateButton ("Open Win2",10,10,100,20,Win1)
HideGadget Win1

Global win2 : Tgadget = CreateWindow ("Win2",200,200,400,300,Null,WINDOW_TITLEBAR)
Global Canvas2 : Tgadget = CreateCanvas (10,40,350,200,Win2)
HideGadget Win2

Function Draw_First_window ()

	ShowGadget win1
	ActivateWindow(Win1)
	
	Local hwnd:Int = GetActiveWindow()
	alwaysOnTop(hwnd)

	CreateTimer 60

	While WaitEvent()
		Print CurrentEvent.ToString()
		Select EventID()
		
			Case EVENT_TIMERTICK
			
				SetGraphics CanvasGraphics(Canvas1)
				Cls
				
				For Local i=0 To GadgetWidth (Canvas1) Step 32
					DrawLine i,0,i,GadgetHeight(Canvas1)
				Next
				
				SetColor 0,40,0
				For Local i=0 To GadgetHeight (Canvas1) Step 32
					DrawLine 0,i,GadgetWidth(Canvas1),i
				Next				

				Flip
				
			Case EVENT_GADGETACTION
				
				If EventSource() = Button1 Then
					Draw_second_window()
				End If
		
			Case EVENT_WINDOWCLOSE
				Exit
				
		End Select
	Wend
	
	HideGadget Win1

End Function

Function Draw_second_window ()

	ShowGadget win2
	ActivateWindow(Win2)
	
	Local hwnd:Int = GetActiveWindow()
	alwaysOnTop(hwnd)

	CreateTimer 60

	While WaitEvent()
	
		Select EventID()
		
			Case EVENT_TIMERTICK
			
				SetGraphics CanvasGraphics(Canvas2)
				Cls
				
				For Local i=0 To GadgetWidth (Canvas2) Step 32
					DrawLine i,0,i,GadgetHeight(Canvas2)
				Next
				
				SetColor 0,40,0
				For Local i=0 To GadgetHeight (Canvas2) Step 32
					DrawLine 0,i,GadgetWidth(Canvas2),i
				Next
				Flip
				
			Case EVENT_WINDOWCLOSE
				Exit
		End Select
	Wend
	
	HideGadget Win1

End Function

Draw_First_Window()



I think you will need to have the GL driver for now ... DX + multiple canvas does have some problems

When the second window is open, the first canvas doesn't get drawn as you move to another loop (in Draw_second_window()) where you only draw the second canvas.

smattbac : I've already added the fellowing code to the
Draw_second_window()) into the 'case event_timertick' to redraw the canvas. but I've the similar problem.

			
				SetGraphics CanvasGraphics(Canvas1)
				Cls
				
				For Local i=0 To GadgetWidth (Canvas1) Step 32
					DrawLine i,0,i,GadgetHeight(Canvas1)
				Next
				
				SetColor 0,40,0
				For Local i=0 To GadgetHeight (Canvas1) Step 32
					DrawLine 0,i,GadgetWidth(Canvas1),i
				Next
 


Have you tried using eventhooks?
see tutorial 15 here

many thanks assari, eventhooks seems the best method.

You probably need to act on the GADGETPAINT event via a hook.

I've now another problem. When the smallwindow is active i don't want that the user could access (an close) the first window. For example if the user open the second window he can clic into the first window close button... and close the second window ! i've tried to disable the first window (disablegadget MyWindow) into the Show_smallwindow() function... not work !

Thanks for your help !

Strict

' -------------------------------------------------------------------------------------------

Extern "win32"
	Function GetActiveWindow()
	Function SetWindowPos(hWnd:Int,after:Int,x:Int,y:Int,w:Int,h:Int,flags:Int)
End Extern

Const SWP_NOSIZE:Int = $1
Const SWP_NOMOVE:Int = $2
Const HWND_TOPMOST:Int = -1
Const HWND_NOTOPMOST:Int = - 2

Function alwaysOnTop(hwnd:Int)
	SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE )
End Function

Function notAlwaysOnTop(hwnd:Int)
	SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE )
End Function
		

' -------------------------------------------------------------------------------------------


Local x:Int=(GadgetWidth(Desktop())-400)/2
Local y:Int=(GadgetHeight(Desktop())-400)/2

Global MyWindow:TGadget=CreateWindow("EventHook Example", x,y,400,400)
Global MyCanvas:TGadget=CreateCanvas(0,50,380,300,MyWindow)
Global MyButton:TGadget=CreateButton ("Open SmallWindow",10,10, 120,30,MyWindow)

Global SmallWindow:TGadget=CreateWindow("Move Me", x+125, y+150, 150, 100, MyWindow, WINDOW_TITLEBAR)
HideGadget SmallWindow

AddHook EmitEventHook, MyHook

Repeat
  WaitEvent()
  Select EventID()
  Case EVENT_WINDOWCLOSE
     End
  Case EVENT_GADGETPAINT   
    UpDateCanvas()

  Case EVENT_GADGETACTION
	If EventSource() = MyButton Then Show_SmallWindow()

  End Select
Forever
End

Function Show_smallwindow()

ShowGadget SmallWindow
Local hwnd:Int = GetActiveWindow()
alwaysOnTop(hwnd)

disablegadget MyWindow ' not work

Repeat
  WaitEvent()
  Select EventID()

  Case EVENT_WINDOWCLOSE
     Exit

   End Select

Forever

HideGadget SmallWindow
'enablegadget MyWindow ' not work !

End Function


Function MyHook:Object(iId:Int,tData:Object,tContext:Object)
  Local Event:TEvent=TEvent(tData)

  If Event.source=MyCanvas And Event.ID=EVENT_GADGETPAINT
     UpdateCanvas()
     Return Null
  EndIf

  Return tData
End Function

Function UpdateCanvas:Int()
     SetGraphics CanvasGraphics(MyCanvas)
     Cls
     SetColor Rnd(255),Rnd(255),Rnd(255)
     DrawRect 10,10,100,150
     Flip
End Function