Drag & Drop files on windows

BlitzMax Forums/BlitzMax GUI Programming/Drag & Drop files on windows

'Drag & Drop files on windows
'Dont Work On Windows Vista
'Code example


Import MaxGUI.Drivers

' file drag & drop example
Strict

Const MENU_EXIT=105
Const MENU_ABOUT=109

'
' A window
Local win:TGadget = CreateWindow("Drag & Drop!",100,100,400,400,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_MENU|WINDOW_STATUS|WINDOW_CLIENTCOORDS|WINDOW_ACCEPTFILES)

'
' A simple menu
Local filemenu:TGadget = CreateMenu("&File",0,WindowMenu(win))
CreateMenu"E&xit",MENU_EXIT,filemenu

Local helpmenu:TGadget = CreateMenu("&Help",0,WindowMenu(win))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu win

'
' A canvas gadget to display the image
Local can:TGadget = CreateCanvas(0,0,400,400,win,1)
SetGadgetLayout can,1,1,1,1

'
' A few bits and pieces
Local image:Timage
Local file:String = "Drag an image file onto window"
SetStatusText win,file


'
' Main loop
While WaitEvent()
	Select EventID()

		Case EVENT_GADGETPAINT
			Select EventSource()
				Case can
					'
					' Draw to the canvas
					SetGraphics CanvasGraphics(can)
					
					'
					' Make sure it has the correct dimensions
					SetViewport 0,0,GadgetWidth(can),GadgetHeight(can)
					
					'
					' Draw the checker background
					SetBlend SOLIDBLEND
					Local a:Int = 1
					Local b:Int = 1
					For Local x = 0 To GadgetWidth(can) Step 16
						b = Not b
						a = b
						For Local y = 0 To GadgetHeight(can) Step 16
							a = Not a
							SetColor 160-a*20,160-a*20,160-a*20
							DrawRect x,y,16,16
						Next
					Next
						
					'
					' Draw the image
					If image
						SetBlend ALPHABLEND
						SetColor 255,255,255
						
						Local scale:Float = Min(1.0,Min(Float(ClientWidth(can))/ImageWidth(image),Float(ClientHeight(can))/ImageHeight(image)))
		
						Local w = ImageWidth(image)*scale
						Local h = ImageHeight(image)*scale	
						Local x = (ClientWidth(can)-w)/2
						Local y = (ClientHeight(can)-h)/2
		
						SetScale scale,scale
						DrawImage image,x,y
		
						SetScale 1,1
						SetBlend SHADEBLEND
						SetColor 170,170,170			
						DrawRect x-1,0,1,ClientHeight(can)
						DrawRect x+w,0,1,ClientHeight(can)
						DrawRect 0,y-1,ClientWidth(can),1
						DrawRect 0,y+h,ClientWidth(can),1
						
						SetStatusText win,file+" @ "+Int(scale*100)+"%"
					EndIf
					
					Flip
			EndSelect
					
		Case EVENT_WINDOWCLOSE
			'
			' Quit
			End
			
		Case EVENT_WINDOWACCEPT
			'
			' A file has been dragged and dropped on the window
			file = EventExtra().tostring()
			
			'
			' Try loading the file as an image
			image = LoadImage(file)
			If image = Null
				file = "Invalid file format!"
			Else
				file = file+"  ("+(FileSize(file)/1024)+"Kb)  "+ImageWidth(image)+"x"+ImageHeight(image)
			EndIf
			SetStatusText win,file
			RedrawGadget can
		
		Case EVENT_MENUACTION
			'
			' Menu stuff
			Select EventData()
				Case MENU_EXIT
					End
				Case MENU_ABOUT
					Notify "File drag & drop example!~nBy Mikkel Fredborg"
			End Select
		
		Default
			'
			' Uncomment this to show what other events occur
			' Print CurrentEvent.toString()
					
	EndSelect
	
Wend


Moderator: Please use the [code ] / [codebox ] tags when posting code samples on the forum. What are the forum codes?

OK, I've had a look at this on my Vista Ultimate (x86) PC and I can't seem to reproduce your problem.

What exactly are you having trouble with? Is it not emitting EVENT_WINDOWACCEPT? Is it failing when loading the image? Is it not drawing on the canvas?

Can you please be more specific as to your problem, post your exact system specs, and post the output given by the following tweaked version of your example when you do try to drop a file onto the window from Explorer:

'Drag & Drop files on windows
'Dont Work On Windows Vista
'Code example

Import MaxGUI.Drivers

Strict

Const MENU_EXIT=105, MENU_ABOUT=109

' A window
Global win:TGadget = CreateWindow("Drag & Drop!",100,100,400,400,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_MENU|WINDOW_STATUS|WINDOW_CLIENTCOORDS|WINDOW_ACCEPTFILES)

' A simple menu
Global filemenu:TGadget = CreateMenu("&File",0,WindowMenu(win))
CreateMenu"E&xit",MENU_EXIT,filemenu

Global helpmenu:TGadget = CreateMenu("&Help",0,WindowMenu(win))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu win

' A canvas gadget to display the image
Global can:TGadget = CreateCanvas(0,0,400,400,win,1)
SetGadgetLayout can,1,1,1,1

' A few bits and pieces
Global image:Timage
Global file:String = "Drag an image file onto window"
SetStatusText win,file

AddHook EmitEventHook, eventHandler

Repeat;WaitSystem();Forever

' Main loop
Function eventHandler:Object( pID%, pData:Object, pContext:Object)
	
	Local pEvent:TEvent = TEvent(pData)
	If Not pEvent Then Return pData
	
	If pEvent.source <> can Then Print pEvent.ToString()
	
	Select pEvent.id
		
		Case EVENT_WINDOWCLOSE,EVENT_APPTERMINATE;End
		
		Case EVENT_GADGETPAINT
		
			Select pEvent.source
				
				Case can
					
					' Draw to the canvas
					SetGraphics CanvasGraphics(can)
					
					' Make sure it has the correct dimensions
					SetViewport 0,0,GadgetWidth(can),GadgetHeight(can)
					
					' Draw the checker background
					SetBlend SOLIDBLEND
					Local a:Int = 1
					Local b:Int = 1
					For Local x = 0 To GadgetWidth(can) Step 16
						b = Not b
						a = b
						For Local y = 0 To GadgetHeight(can) Step 16
							a = Not a
							SetColor 160-a*20,160-a*20,160-a*20
							DrawRect x,y,16,16
						Next
					Next
					
					' Draw the image
					If image
						SetBlend ALPHABLEND
						SetColor 255,255,255
						
						Local scale:Float = Min(1.0,Min(Float(ClientWidth(can))/ImageWidth(image),Float(ClientHeight(can))/ImageHeight(image)))
		
						Local w = ImageWidth(image)*scale
						Local h = ImageHeight(image)*scale	
						Local x = (ClientWidth(can)-w)/2
						Local y = (ClientHeight(can)-h)/2
		
						SetScale scale,scale
						DrawImage image,x,y
		
						SetScale 1,1
						SetBlend SHADEBLEND
						SetColor 170,170,170			
						DrawRect x-1,0,1,ClientHeight(can)
						DrawRect x+w,0,1,ClientHeight(can)
						DrawRect 0,y-1,ClientWidth(can),1
						DrawRect 0,y+h,ClientWidth(can),1
						
						SetStatusText win,file+" @ "+Int(scale*100)+"%"
					EndIf
					
					Flip
			EndSelect
			
		Case EVENT_WINDOWACCEPT
			
			' A file has been dragged and dropped on the window
			file = pEvent.extra.tostring()
			
			' Try loading the file as an image
			image = LoadImage(file)
			If image = Null Then
				file = "Invalid file format!"
				Print "Loading Error:"
				Print "~q" + file + "~q (" + FileType(file) + ")"
			Else
				file:+"  ("+(FileSize(file)/1024)+"Kb)  "+ImageWidth(image)+"x"+ImageHeight(image)
			EndIf
			
			SetStatusText win,file
			RedrawGadget can
		
		Case EVENT_MENUACTION
			
			' Menu stuff
			Select pEvent.data
				Case MENU_EXIT;End
				Case MENU_ABOUT;Notify "File drag & drop example!~nBy Mikkel Fredborg"
			EndSelect
					
	EndSelect
	
EndFunction


EVENT_WINDOWACCEPT - never occur ???
Is not emitted


WindowActivate: data=0, mods=0, x=0, y=0, extra=""
AppSuspend: data=0, mods=0, x=0, y=0, extra=""


BlitzMax v1.28 with MaxGUI
HP Pavilion dv6420la Notebook
On Windows Vista Home Premium

Amd Turion64x2 -> HD 160 gb -> Ram 2 gb -> Nvidia Gforce go 6150

-------------------------------------
test it on my WinXP work fine :-)

-------------------------------------
Drag & Drop files on my Windows Vista Home Premium
Dont Work :-(

Can you download and run this Drag n' Drop Text Program?



Drag and drop any text file onto it and tell me whether or not you get any text displayed in the text-area and status bar.

If you still don't get anything, can you tell me whether your mouse cursor changes the Windows drag n' drop cursor when you drag a file onto the window?

test - Drag n' Drop Text Program

text displayed in the text-area
ÿØÿà

status bar
"c:\Users\luis enrique braga\Desktop\Image.jpg" (1)

cursor changes when you drag a file onto the window
yes


work fine :-)

OK, does the following program run: GUI Drop v2?

Do you get a message box when you try to drag n' drop now?

Just out of curiosity, are you using a non-English version of Windows or a non-ASCII filename? Also, can you post the module version of your MaxGUI.Win32MaxGUIEx, e.g. have a look in mod\maxgui.mod\win32maxguiex.mod\win32maxguiex.bmx for the ModuleInfo line at the top. Did you build modules after your last checkout on SVN?

you get a message box when you try to drag n' drop

yes

File Dropped:
"C:\Users\luis enrique braga\Desktop\image.jpg"

it work now :-)


are you using a non-English version of Windows
SPANISH VERSION - I live in mexico :-)


win32maxgui.bmx - info

Rem
bbdoc: MaxGUI/Windows MaxGUI
about:
This module provides a Win32 based Windows driver for #MaxGUI.
End Rem
Module MaxGUI.Win32MaxGUI

Strict

ModuleInfo "Version: 1.36"
ModuleInfo "Author: Mark Sibly and Simon Armstrong"
ModuleInfo "License: Blitz Shared Source Code"
ModuleInfo "Copyright: Blitz Research Ltd"
ModuleInfo "Modserver: BRL"


Ooops - that's MaxGUI.Win32MaxGUI - could you post the version info for MaxGUI.Win32MaxGUIEx?

win32maxguiex.bmx - info

Rem
bbdoc: MaxGUI Windows Driver (Take 2)
End Rem
Module MaxGUI.Win32MaxGUIEx

ModuleInfo "Version: 0.47"
ModuleInfo "Copyright: Armstrong Communications Ltd"
ModuleInfo "Author: Simon Armstrong, Seb Hollington"
ModuleInfo "Modserver: BRL"


Thanks, that's the most recent SVN version so I'm not sure why you are getting the problems. I've sent you the latest dev version which hasn't been committed yet (v0.48) - if you could let me know if it helps.

Cronos: You have e-mail!

lebr_gdl@...

thanks for your help !!!!!!

nice support :-)

OK, I've resent it to your Hotmail address (I had originally sent it to the address in your profile).

i replaced
maxgui.mod\win32maxguiex.mod\win32maxguiex.bmx

maxgui.mod\win32maxguiex.mod\winimports.bmx

rebuild all modules

but "Drag & Drop!" project sample dont work :-(

AppSuspend: data=0, mods=0, x=0, y=0, extra=""
AppResume: data=0, mods=0, x=0, y=0, extra=""
WindowActivate: data=0, mods=0, x=0, y=0, extra=""

-------------------------------------------
your GUIDrop2.exe work fine :-)

i use MinGW-5.1.3 to rebuild all modules

a small youtube video for you see the trouble

http://www.youtube.com/watch?v=s8p6oJlCZzE

the compile example not work :-(
your GUIDrop2.exe work nice :-)

Cronos,

Can you also try the following EXE:
GUI Drop 3

Also, please can you compile the following BMX file and e-mail me the resulting EXE and .bmx folder generated in a single ZIP archive:
GUIDrop3.bmx

...making sure you have only "Build GUI App" checked in the IDE's "Build Options" sub-menu (under the "Program Menu").

Thanks

Don't worry about the EXE size - I use an EXE compressor (called UPX Compression) which results in smaller EXEs - they are inflated at runtime though so should behave in exactly the same way.

I may have an idea... Check your e-mail...

I discovered that

http://www.youtube.com/watch?v=CJGxlSQKtdo

if you closed MaxIDE and run the compiled program it work :-)

sorry never tested it :-)

replaced
maxgui.mod\win32maxguiex.mod\win32maxguiex.bmx
maxgui.mod\win32maxguiex.mod\winimports.bmx
work :-)

never replaced MaxIDE :-) maybe need recompile it

Well that certainly is a strange issue... Not sure why MaxIDE would have any effect on the program, but hey, if it works... ;-)

i replaced

bcc.exe
bmk.exe
libunicows.a

but you ned closed MaxIDE and run the compiled program for it work :-)

but is not important fo me :-)
only need that compiled program works :-)

thanks for your help !!!!