rootx Posted April 16, 2017 Posted April 16, 2017 (edited) how can I fit the image when the GUI is maximized? I would like to always measure the 50% height and width of the GUI and is always in the bottom right poistion, and does not lose its quality. THX expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Array.au3> #include <WinAPI.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 437, 192, 124,BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_TABSTOP)) _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir&"\img.jpg") $hGraphics = _GDIPlus_GraphicsCreateFromHWND($Form1) $resimg = _GDIPlus_ImageResize($hImage,200,300) _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, 0, 0) GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT") ;GUIRegisterMsg($WM_SIZE, "WM_SIZE") GUISetState(@SW_SHOW,$Form1) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func MY_WM_PAINT($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_UPDATENOW) _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, 300, 0) _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_VALIDATE) Return $GUI_RUNDEFMSG EndFunc ;==>MY_WM_PAINT ;Func WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam) ; #forceref $hWnd, $iMsg, $iwParam, $ilParam ; Local $xClient, $yClient ; $xClient = BitAND($ilParam, 0x0000FFFF) ; $yClient = BitShift($ilParam, 16) ; _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_UPDATENOW) ; _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, $xClient/2, $yClient/2) ; _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_VALIDATE) ; ConsoleWrite($xClient & " "&$yClient&@CR) ; Return $GUI_RUNDEFMSG ;EndFunc Edited April 16, 2017 by rootx
InunoTaishou Posted April 16, 2017 Posted April 16, 2017 Not sure I understand. Do you want the image to be drawn to fit the GUI window or do you want the image to be drawn starting at the exact midpoint of the GUI (width and height)? Making it be drawn in the bottom right corner of the GUI. rootx 1
InunoTaishou Posted April 16, 2017 Posted April 16, 2017 Here's how you would do it if you want the image to fit the width and height of the window expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Array.au3> #include <WinAPI.au3> Global Const $SIZE_RESTORED = 0 Global Const $SIZE_MAXIMIZED = 2 #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 437, 192, 124, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_TABSTOP, $WS_SIZEBOX)) _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\img.jpg") $hGraphics = _GDIPlus_GraphicsCreateFromHWND($Form1) Global $resimg = _GDIPlus_ImageResize($hImage, 615, 437) _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, 0, 0) GUIRegisterMsg($WM_PAINT, MY_WM_PAINT) GUIRegisterMsg($WM_SIZE, WM_SIZE) GUISetState(@SW_SHOW, $Form1) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func MY_WM_PAINT($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, 0, 0) _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_VALIDATE) Return $GUI_RUNDEFMSG EndFunc ;==>MY_WM_PAINT Func WM_SIZE($hWndFrom, $iMsg, $wParam, $lParam) Local $iLoWord = _WinAPI_LoWord($lParam) ; Width Local $iHiWord = _WinAPI_HiWord($lParam) ; Height Switch ($hWndFrom) Case $Form1 Switch ($wParam) Case $SIZE_RESTORED, $SIZE_MAXIMIZED ; The old graphic is based off the size of the old size, destroy it _GDIPlus_GraphicsDispose($hGraphics) ; And recreate it with the size of the new size $hGraphics = _GDIPlus_GraphicsCreateFromHWND($Form1) ; resize the image to the new width/height $resimg = _GDIPlus_ImageResize($hImage, $iLoWord, $iHiWord) ; Implicitly calls MY_WM_PAINT after this, based on the order of window messages received from Windows EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_SIZE And here's how you could draw it in the bottom right quadrant of the GUI expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Array.au3> #include <WinAPI.au3> Global Const $SIZE_RESTORED = 0 Global Const $SIZE_MAXIMIZED = 2 Global $iWidth = 615 Global $iHeight = 437 #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", $iWidth, $iHeight, 192, 124, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_TABSTOP, $WS_SIZEBOX)) _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\img.jpg") $hGraphics = _GDIPlus_GraphicsCreateFromHWND($Form1) Global $resimg = _GDIPlus_ImageResize($hImage, $iWidth / 2, $iHeight / 2) _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, 0, 0) GUIRegisterMsg($WM_PAINT, MY_WM_PAINT) GUIRegisterMsg($WM_SIZE, WM_SIZE) GUISetState(@SW_SHOW, $Form1) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func MY_WM_PAINT($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam _GDIPlus_GraphicsDrawImage($hGraphics, $resimg, $iWidth / 2, $iHeight / 2) _WinAPI_RedrawWindow($Form1, 0, 0, $RDW_VALIDATE) Return $GUI_RUNDEFMSG EndFunc ;==>MY_WM_PAINT Func WM_SIZE($hWndFrom, $iMsg, $wParam, $lParam) Local $iLoWord = _WinAPI_LoWord($lParam) ; Width Local $iHiWord = _WinAPI_HiWord($lParam) ; Height Switch ($hWndFrom) Case $Form1 Switch ($wParam) Case $SIZE_RESTORED, $SIZE_MAXIMIZED $iWidth = $iLoWord $iHeight = $iHiWord ; The old graphic is based off the size of the old size, destroy it _GDIPlus_GraphicsDispose($hGraphics) ; And recreate it with the size of the new size $hGraphics = _GDIPlus_GraphicsCreateFromHWND($Form1) ; resize the image to the new width/height $resimg = _GDIPlus_ImageResize($hImage, $iLoWord / 2, $iHiWord / 2) ; Implicitly calls MY_WM_PAINT after this, based on the order of window messages received from Windows EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_SIZE rootx 1
rootx Posted April 16, 2017 Author Posted April 16, 2017 Thx, me cupla I try to explain you. usually I use GUICtrlSetResizing to have a responsive gui... but with the object PIC the image does not maintain a good quality. Now I'm trying with GDI.
InunoTaishou Posted April 16, 2017 Posted April 16, 2017 (edited) Wait, don't leave yet. Gimme a few minutes and I'll create one better that will be a little less graphics intensive and get rid of that flashing. (Been a while since I've done some GDI+ stuff so I gotta remember how I did this lol) Edited April 16, 2017 by InunoTaishou
InunoTaishou Posted April 16, 2017 Posted April 16, 2017 (edited) This should do it. Won't redraw and refresh while it's being sized but it will be updated after you release the size box and will be updated upon Maximize/restore. Gets rid of the flashing expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Array.au3> #include <WinAPI.au3> _GDIPlus_Startup() #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 437, 192, 124, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_TABSTOP, $WS_SIZEBOX)) Global $picImage = GUICtrlCreatePic("", 615 / 2, 437 / 2, 615 / 2, 437 / 2) GUICtrlSetResizing($picImage, $GUI_DOCKRIGHT + $GUI_DOCKBOTTOM) Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\img.jpg") Global $hBitmap = _GDIPlus_BitmapCreateFromScan0(@DesktopWidth, @DesktopHeight) Global $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap) Resize() GUISetState(@SW_SHOW, $Form1) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_ImageDispose($hImage) Exit Case $GUI_EVENT_RESIZED, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE Resize() EndSwitch WEnd Func Resize() Local $aSize = ControlGetPos($Form1, "", $picImage) _GDIPlus_GraphicsClear($hGraphics, 0x00000000) _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $aSize[2], $aSize[3]) Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($picImage, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap)) EndFunc Been a long time since I've draw graphics in memory, not directly on the HWND, and then set an image from memory. So I had to remember the order of the bitmaps/graphics lol Idk what your project entails but maybe you might want a complex GDI+ intensive project to look at to see how you can manage objects. I started this a long time ago, before I left for basic, and never finished it. It was going to be a puzzle style game. I finished the menu portion of it but never finished the puzzle part of it. I used a lot of structs to hold all of my GDI+ objects, windows, etc. As well as using array of string with the function calls to manage the structs (Why have 20 lines to destroy my GDI+ objects when I could have 1 for loop going through and calling the function needed for the struct specified). It's pretty cool though. Creating a context menu (right click) on the GUI and the options displayed are specific to the image you're over (or no image if you're not over one). http://www.mediafire.com/file/2t37tatmogjg33y/PuzzleIt.7z Edited April 16, 2017 by InunoTaishou rootx 1
InunoTaishou Posted April 16, 2017 Posted April 16, 2017 No problemo. I've only ever shown that to one other person but I'm sure anyone who finds it might find it useful. One cool thing I'd like to point out (it's not in the script because I never made it an option) is it will support just Vertical scrolling and just Horizontal scrolling. If you go down to line 306 SetWorkspaceStruct($tWorkspaceMenu, $tControlMenu, BitOR($SCROLL_VSCROLL, $SCROLL_HSCROLL), 3, 1.0, False) At the moment the menu will scroll left/right and up/down (Ctrl + Mouse wheel will go left/right). If you change this line to this SetWorkspaceStruct($tWorkspaceMenu, $tControlMenu, $SCROLL_HSCROLL, 3, 1.0, False) It will make all of the picture fit vertically and only scroll left/right. The opposite is the same for SetWorkspaceStruct($tWorkspaceMenu, $tControlMenu, $SCROLL_VSCROLL, 3, 1.0, False) So it will fit all of the images horizontally and then scroll up/down Alt + Mouse wheel will zoom in/out. Resizing the menu and making all of the pieces bigger/smaller (Just takes a minute because it's going to resize all of the large images to fit the menu again). There's a lot to go through. I know I didn't comment a lot of things. I planned on finishing it and releasing it but real life got in the way and never had the time go dig through it again.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now