imagedrop.bmx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ' file drag & drop example
  2. Import MaxGui.Drivers
  3. Strict
  4. Const MENU_EXIT=105
  5. Const MENU_ABOUT=109
  6. '
  7. ' A window
  8. Local win:TGadget = CreateWindow("Drag & Drop!",100,100,400,400,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_MENU|WINDOW_STATUS|WINDOW_CLIENTCOORDS|WINDOW_ACCEPTFILES)
  9. '
  10. ' A simple menu
  11. Local filemenu:TGadget = CreateMenu("&File",0,WindowMenu(win))
  12. CreateMenu"E&xit",MENU_EXIT,filemenu
  13. Local helpmenu:TGadget = CreateMenu("&Help",0,WindowMenu(win))
  14. CreateMenu "&About",MENU_ABOUT,helpmenu
  15. UpdateWindowMenu win
  16. '
  17. ' A canvas gadget to display the image
  18. Local can:TGadget = CreateCanvas(0,0,400,400,win,1)
  19. SetGadgetLayout can,1,1,1,1
  20. '
  21. ' A few bits and pieces
  22. Local image:Timage
  23. Local file:String = "Drag an image file onto window"
  24. SetStatusText win,file
  25. '
  26. ' Main loop
  27. While WaitEvent()
  28. Select EventID()
  29. Case EVENT_GADGETPAINT
  30. Select EventSource()
  31. Case can
  32. '
  33. ' Draw to the canvas
  34. SetGraphics CanvasGraphics(can)
  35. '
  36. ' Make sure it has the correct dimensions
  37. SetViewport 0,0,GadgetWidth(can),GadgetHeight(can)
  38. '
  39. ' Draw the checker background
  40. SetBlend SOLIDBLEND
  41. Local a:Int = 1
  42. Local b:Int = 1
  43. For Local x = 0 To GadgetWidth(can) Step 16
  44. b = Not b
  45. a = b
  46. For Local y = 0 To GadgetHeight(can) Step 16
  47. a = Not a
  48. SetColor 160-a*20,160-a*20,160-a*20
  49. DrawRect x,y,16,16
  50. Next
  51. Next
  52. '
  53. ' Draw the image
  54. If image
  55. SetBlend ALPHABLEND
  56. SetColor 255,255,255
  57. Local scale:Float = Min(1.0,Min(Float(ClientWidth(can))/ImageWidth(image),Float(ClientHeight(can))/ImageHeight(image)))
  58. Local w = ImageWidth(image)*scale
  59. Local h = ImageHeight(image)*scale
  60. Local x = (ClientWidth(can)-w)/2
  61. Local y = (ClientHeight(can)-h)/2
  62. SetScale scale,scale
  63. DrawImage image,x,y
  64. SetScale 1,1
  65. SetBlend SHADEBLEND
  66. SetColor 170,170,170
  67. DrawRect x-1,0,1,ClientHeight(can)
  68. DrawRect x+w,0,1,ClientHeight(can)
  69. DrawRect 0,y-1,ClientWidth(can),1
  70. DrawRect 0,y+h,ClientWidth(can),1
  71. SetStatusText win,file+" @ "+Int(scale*100)+"%"
  72. EndIf
  73. Flip
  74. EndSelect
  75. Case EVENT_WINDOWCLOSE
  76. '
  77. ' Quit
  78. End
  79. Case EVENT_WINDOWACCEPT
  80. '
  81. ' A file has been dragged and dropped on the window
  82. file = EventExtra().tostring()
  83. '
  84. ' Try loading the file as an image
  85. image = LoadImage(file)
  86. If image = Null
  87. file = "Invalid file format!"
  88. Else
  89. file = file+" ("+(FileSize(file)/1024)+"Kb) "+ImageWidth(image)+"x"+ImageHeight(image)
  90. EndIf
  91. SetStatusText win,file
  92. RedrawGadget can
  93. Case EVENT_MENUACTION
  94. '
  95. ' Menu stuff
  96. Select EventData()
  97. Case MENU_EXIT
  98. End
  99. Case MENU_ABOUT
  100. Notify "File drag & drop example!~nBy Mikkel Fredborg"
  101. End Select
  102. Default
  103. '
  104. ' Uncomment this to show what other events occur
  105. ' Print CurrentEvent.toString()
  106. EndSelect
  107. Wend