moveable picture in gui? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

moveable picture in gui?

25 Oct 2021, 14:14

how can i make a moveable picture in gui without mousegetpos loop?
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: moveable picture in gui?

25 Oct 2021, 14:58

mikeyww wrote:
25 Oct 2021, 14:32
:arrow: Move
how do you mean?
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: moveable picture in gui?

25 Oct 2021, 15:24

I mean that the picture is a control that could be moved with this command.
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: moveable picture in gui?

25 Oct 2021, 15:46

mikeyww wrote:
25 Oct 2021, 15:24
I mean that the picture is a control that could be moved with this command.
ok but my question was if you can move the picture like a window in the qui. maybe with PostMessage?
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: moveable picture in gui?

25 Oct 2021, 18:02

I see. Others may know. I can only think of doing it by identifying a click on the image, and then following the mouse-- apparently not what you want here. Another idea is to create two GUIs, with the picture representing a single GUI.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: moveable picture in gui?  Topic is solved

25 Oct 2021, 18:07

An example:

Code: Select all

imageUrl := "https://i.imgur.com/26lSSUi.png"
Gui, New, +hwndhParent +LabelParent
Gui, New, +hwndhChild +Parent%hParent% -Caption
Gui, Margin, 0, 0
Gui, Add, Pic,, % "HBITMAP:" . GetHBitmapFromImageURL(imageUrl)
Gui, Show, NA x100 y100
Gui, %hParent%: Show, w460 h460
OnMessage( 0x201, Func("WM_LBUTTONDOWN").Bind(hChild) )
Return

ParentClose() {
   ExitApp
}

WM_LBUTTONDOWN(hChild, wp, lp, msg, hwnd) {
   if (hwnd = hChild)
      PostMessage, WM_NCLBUTTONDOWN := 0xA1, HTCAPTION := 2,,, ahk_id %hChild%
}
   
GetHBitmapFromImageURL(url) {
   Whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
   Whr.Open("GET", url, true)
   Whr.Send()
   Whr.WaitForResponse()
   if (Whr.Status != 200) {
      MsgBox, Failed to load the image!
      ExitApp
   }
   Return (new GDIplus).HBitmapFromStream(Whr.ResponseStream)
}

class GDIplus {
   __New() {
      if !DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("LoadLibrary", "Str", "gdiplus")
      VarSetCapacity(si, 8 + A_PtrSize*2, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", "PtrP", pToken, "Ptr", &si, "Ptr", 0)
      this.token := pToken
   }
   __Delete() {
      DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
      if hModule := DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("FreeLibrary", "Ptr", hModule)
   }
   HBitmapFromStream(Stream) {
      DllCall("gdiplus\GdipCreateBitmapFromStream", "Ptr", ComObjValue(Stream), "PtrP", pBitmap)
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0xFFFFFFFF)
      DllCall("gdiplus\GdipDisposeImage", "Ptr", pBitmap)
      Return hBitmap
   }
}
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: moveable picture in gui?

26 Oct 2021, 07:14

teadrinker wrote:
25 Oct 2021, 18:07
An example:

Code: Select all

imageUrl := "https://i.imgur.com/26lSSUi.png"
Gui, New, +hwndhParent +LabelParent
Gui, New, +hwndhChild +Parent%hParent% -Caption
Gui, Margin, 0, 0
Gui, Add, Pic,, % "HBITMAP:" . GetHBitmapFromImageURL(imageUrl)
Gui, Show, NA x100 y100
Gui, %hParent%: Show, w460 h460
OnMessage( 0x201, Func("WM_LBUTTONDOWN").Bind(hChild) )
Return

ParentClose() {
   ExitApp
}

WM_LBUTTONDOWN(hChild, wp, lp, msg, hwnd) {
   if (hwnd = hChild)
      PostMessage, WM_NCLBUTTONDOWN := 0xA1, HTCAPTION := 2,,, ahk_id %hChild%
}
   
GetHBitmapFromImageURL(url) {
   Whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
   Whr.Open("GET", url, true)
   Whr.Send()
   Whr.WaitForResponse()
   if (Whr.Status != 200) {
      MsgBox, Failed to load the image!
      ExitApp
   }
   Return (new GDIplus).HBitmapFromStream(Whr.ResponseStream)
}

class GDIplus {
   __New() {
      if !DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("LoadLibrary", "Str", "gdiplus")
      VarSetCapacity(si, 8 + A_PtrSize*2, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", "PtrP", pToken, "Ptr", &si, "Ptr", 0)
      this.token := pToken
   }
   __Delete() {
      DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
      if hModule := DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("FreeLibrary", "Ptr", hModule)
   }
   HBitmapFromStream(Stream) {
      DllCall("gdiplus\GdipCreateBitmapFromStream", "Ptr", ComObjValue(Stream), "PtrP", pBitmap)
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0xFFFFFFFF)
      DllCall("gdiplus\GdipDisposeImage", "Ptr", pBitmap)
      Return hBitmap
   }
}
Thank you, this is working :D
garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: moveable picture in gui?

26 Oct 2021, 09:03

@teadrinker thank you
How modify script if want use a picture from computer ? ( like c:\test.png )
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: moveable picture in gui?

26 Oct 2021, 09:50

@teadrinker:

Code: Select all

class GDIplus {
   __New() {
      if !DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("LoadLibrary", "Str", "gdiplus")
      VarSetCapacity(si, 8 + A_PtrSize*2, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", "PtrP", pToken, "Ptr", &si, "Ptr", 0)
      this.token := pToken
   }
   __Delete() {
      DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
      if hModule := DllCall("GetModuleHandle", "Str", "gdiplus", "Ptr")
         DllCall("FreeLibrary", "Ptr", hModule)
   }
   ...
}
IMO, this will free the Gdiplus.dll even if the class did not load it. That might have unwanted side-effects.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: moveable picture in gui?

26 Oct 2021, 11:29

just me wrote: IMO, this will free the Gdiplus.dll even if the class did not load it. That might have unwanted side-effects.
Yeah, you are right, I just copied this code from GDIp.ahk. This is better:

Code: Select all

class GDIplus {
   __New() {
      this.hLib := DllCall("LoadLibrary", "Str", "gdiplus", "Ptr")
      VarSetCapacity(si, 8 + A_PtrSize*2, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", "PtrP", pToken, "Ptr", &si, "Ptr", 0)
      this.token := pToken
   }
   __Delete() {
      DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
      DllCall("FreeLibrary", "Ptr", this.hLib)
   }
   ...
}
Or even like this:

Code: Select all

class GDIplus {
   __New() {
      static Instance := ""
      if Instance.references++
         Return Instance
      this.hLib := DllCall("LoadLibrary", "Str", "gdiplus", "Ptr")
      VarSetCapacity(si, 8 + A_PtrSize*2, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", "UPtrP", pToken, "Ptr", &si, "Ptr", 0)
      this.token := pToken, this.references := 1
      Return Instance := this
   }
   Release() {
      if --this.references
         Return
      DllCall("gdiplus\GdiplusShutdown", "Ptr", this.token)
      DllCall("FreeLibrary", "Ptr", this.hLib)
   }
   ...
}
@garry

Code: Select all

imagePath := "D:\Downloads\Icons\github-logo-icon.png"

Gui, New, +hwndhParent +LabelParent
Gui, New, +hwndhChild +Parent%hParent% -Caption
Gui, Margin, 0, 0
Gui, Add, Pic,, % imagePath
Gui, Show, NA x100 y100
Gui, %hParent%: Show, w460 h460
OnMessage( 0x201, Func("WM_LBUTTONDOWN").Bind(hChild) )
Return

ParentClose() {
   ExitApp
}

WM_LBUTTONDOWN(hChild, wp, lp, msg, hwnd) {
   if (hwnd = hChild)
      PostMessage, WM_NCLBUTTONDOWN := 0xA1, HTCAPTION := 2,,, ahk_id %hChild%
}
garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: moveable picture in gui?

26 Oct 2021, 12:07

@teadrinker , thank you , works fine

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], RussF and 127 guests