name22

GDI+ Drawing Example

Jun 29th, 2011
1,175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Misc.au3>
  2. #include <WinAPI.au3>
  3. #include <GDIPlus.au3>
  4. #include <GUIConstants.au3>
  5. #include <EditConstants.au3>
  6. #include <WindowsConstants.au3>
  7.  
  8. ; - Author: name22 (www.autoit.de)
  9.  
  10. Opt("GUIOnEventMode", 1)
  11.  
  12. $vU32Dll = DllOpen("User32.dll")
  13.  
  14. $iGUIColorBG = 0xFFFFFFFF
  15. $iGUIWidth = 800
  16. $iGUIHeight = 600
  17.  
  18. $iGraphicX = 100
  19. $iGraphicY = 5
  20. $iGraphicWidth = $iGUIWidth - $iGraphicX - 5
  21. $iGraphicHeight = $iGUIHeight - $iGraphicY - 5
  22.  
  23. $iRGB_Color = "0x000000"
  24. $iAlphaChannel = 255
  25. $iPenWidth = 2
  26.  
  27. $tRect_Graphic = DllStructCreate($tagRECT)
  28. DllStructSetData($tRect_Graphic, "Left", $iGraphicX)
  29. DllStructSetData($tRect_Graphic, "Top", $iGraphicY)
  30. DllStructSetData($tRect_Graphic, "Right", $iGraphicX + $iGraphicWidth)
  31. DllStructSetData($tRect_Graphic, "Bottom", $iGraphicY + $iGraphicHeight)
  32.  
  33. $tPoint_Mouse = DllStructCreate($tagPOINT)
  34.  
  35. $hWnd = GUICreate("name22's supercooles Zeichenprogramm", $iGUIWidth, $iGUIHeight)
  36. GUICtrlCreateLabel("Pen Settings:", 5, 5, 100, 20)
  37. GUICtrlSetFont(-1, 10)
  38. GUICtrlCreateLabel("Image:", 5, 170, 100, 20)
  39. GUICtrlSetFont(-1, 10)
  40. GUICtrlCreateLabel("Pen Width:", 15, 25, 60, 20)
  41. GUICtrlCreateLabel("Pen Color:", 15, 75, 60, 20)
  42. GUICtrlCreateLabel("Alpha (0-255):", 15, 120, 70, 20)
  43. $cLabel_PreviewColor = GUICtrlCreateLabel("", 70, 78, 10, 10)
  44. GUICtrlSetBkColor(-1, 0x000000)
  45. $cInput_PenWidth = GUICtrlCreateInput($iPenWidth, 15, 40, 50, 20, $ES_NUMBER)
  46. $cUpDown_PenWidth = GUICtrlCreateUpdown($cInput_PenWidth)
  47. $cInput_AlphaChannel = GUICtrlCreateInput(255, 15, 135, 50, 20)
  48. $cButton_Color = GUICtrlCreateButton("Color...", 15, 90, 60, 25)
  49. $cButton_Reset = GUICtrlCreateButton("Reset", 15, 190, 60, 25)
  50. $cButton_Save = GUICtrlCreateButton("Save", 15, 220, 60, 25)
  51. GUISetState()
  52.  
  53. _GDIPlus_Startup()
  54.  
  55. $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
  56. $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iGraphicWidth, $iGraphicHeight, $hGraphic)
  57. $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
  58. _GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
  59.  
  60. _GDIPlus_GraphicsDrawRect($hGraphic, $iGraphicX - 1, $iGraphicY - 1, $iGraphicWidth + 2, $iGraphicHeight + 2)
  61. _GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
  62.  
  63. _ReDraw()
  64.  
  65. $hPen_Main = _GDIPlus_PenCreate(0xFF000000, $iPenWidth)
  66.  
  67. GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
  68. GUISetOnEvent($GUI_EVENT_RESTORE, "_ReDraw")
  69. GUIRegisterMsg($WM_PAINT, "_ReDraw")
  70.  
  71. GUICtrlSetOnEvent($cButton_Color, "_ChoosePenColor")
  72. GUICtrlSetOnEvent($cButton_Reset, "_ResetImage")
  73. GUICtrlSetOnEvent($cButton_Save, "_SaveImage")
  74.  
  75. $aOldPos = "none"
  76.  
  77. While Sleep(10)
  78.     $aCursorInfo = GUIGetCursorInfo($hWnd)
  79.     DllStructSetData($tPoint_Mouse, "X", $aCursorInfo[0])
  80.     DllStructSetData($tPoint_Mouse, "Y", $aCursorInfo[1])
  81.     If _WinAPI_PtInRect($tRect_Graphic, $tPoint_Mouse) And _IsPressed("01", $vU32Dll) Then
  82.         If $aOldPos = "none" Then
  83.             $aOldPos = $aCursorInfo
  84.         Else
  85.             If $aOldPos[0] <> $aCursorInfo[0] Or $aOldPos[1] <> $aCursorInfo[1] Then
  86.                 _GDIPlus_GraphicsDrawLine($hBuffer, $aOldPos[0] - $iGraphicX, $aOldPos[1] - $iGraphicY, $aCursorInfo[0] - $iGraphicX, $aCursorInfo[1] - $iGraphicY, $hPen_Main)
  87.                 _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $iGraphicX, $iGraphicY, $iGraphicWidth, $iGraphicHeight)
  88.                 $aOldPos = $aCursorInfo
  89.             EndIf
  90.             Sleep(10)
  91.         EndIf
  92.     Else
  93.         If IsArray($aOldPos) Then $aOldPos = "none"
  94.     EndIf
  95.     If GUICtrlRead($cInput_AlphaChannel) <> $iAlphaChannel Then
  96.         $iAlphaChannelNew = GUICtrlRead($cInput_AlphaChannel)
  97.         If StringReplace($iAlphaChannelNew, " ", "") = "" Then
  98.             $iAlphaChannelNew = $iAlphaChannel
  99.             GUICtrlSetData($cInput_AlphaChannel, $iAlphaChannelNew)
  100.         EndIf
  101.         $iAlphaChannel = $iAlphaChannelNew
  102.         $iARGB_Color = "0x" & Hex($iAlphaChannel, 2) & StringTrimLeft($iRGB_Color, 2)
  103.         _GDIPlus_PenSetColor($hPen_Main, $iARGB_Color)
  104.     EndIf
  105.     If GUICtrlRead($cInput_PenWidth) <> $iPenWidth Then
  106.         $iPenWidthNew = GUICtrlRead($cInput_PenWidth)
  107.         If StringReplace($iPenWidthNew, " ", "") = "" Then
  108.             $iPenWidthNew = $iPenWidth
  109.             GUICtrlSetData($cInput_PenWidth, $iPenWidth)
  110.         EndIf
  111.         _GDIPlus_PenSetWidth($hPen_Main, $iPenWidthNew)
  112.         $iPenWidthNew = $iPenWidth
  113.     EndIf
  114. WEnd
  115.  
  116. Func _ReDraw()
  117.     _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $iGraphicX, $iGraphicY, $iGraphicWidth, $iGraphicHeight)
  118. EndFunc
  119.  
  120. Func _ChoosePenColor()
  121.     $iRGB_ColorNew = _ChooseColor(2, $iRGB_Color, 2)
  122.     If $iRGB_ColorNew = -1 Then $iRGB_ColorNew = $iRGB_Color
  123.  
  124.     $iARGB_Color = "0x" & Hex($iAlphaChannel, 2) & StringTrimLeft($iRGB_ColorNew, 2)
  125.     _GDIPlus_PenSetColor($hPen_Main, $iARGB_Color)
  126.     GUICtrlSetBkColor($cLabel_PreviewColor, $iRGB_ColorNew)
  127.  
  128.     $iRGB_Color = $iRGB_ColorNew
  129. EndFunc
  130.  
  131. Func _ResetImage()
  132.     _GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
  133.     _ReDraw()
  134. EndFunc
  135.  
  136. Func _SaveImage()
  137.     $sPathDst = FileSaveDialog("Speichern unter", @ScriptDir, "JPEG (*.jpg;*.jpeg)", 16)
  138.     If @error Or StringReplace($sPathDst, " ", "") = "" Then Return
  139.  
  140.     If StringRight($sPathDst, StringLen($sPathDst) - StringInStr($sPathDst, ".", 0, -1)) <> "jpg" Then
  141.         $sPathDst = StringTrimRight($sPathDst, StringLen($sPathDst) - StringInStr($sPathDst, ".", 0, -1) + 1) & ".jpg"
  142.     EndIf
  143.  
  144.     _GDIPlus_ImageSaveToFile($hBitmap, $sPathDst)
  145. EndFunc
  146.  
  147. Func _Exit()
  148.     _GDIPlus_GraphicsDispose($hGraphic)
  149.     _GDIPlus_GraphicsDispose($hBuffer)
  150.     _GDIPlus_BitmapDispose($hBitmap)
  151.     _GDIPlus_PenDispose($hPen_Main)
  152.     _GDIPlus_Shutdown()
  153.     Exit
  154. EndFunc
RAW Paste Data