Don't understand returned object syntax for this function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Don't understand returned object syntax for this function

17 Apr 2016, 00:10

I'm using just_me's LV_EX library for listviews
One of his functions in that library is LV_EX_CalcViewSize(HLV, Rows := 0)

Here is the readme for that function:

========================================================================================================================
LV_EX_CalcViewSize(HLV, Rows := 0)
========================================================================================================================
Calculates the approximate width and height required to display a given number of items.
Params:
Rows - the number of rows to be used for the calculation.
Default: 0 (current rows)
Returns:
An object containing the following key/value pairs:
W: the calculated width
H: the calculated height

So two things, if I were to use this in my script, what do I put where he has Rows := 0
Do I put things like this:

Code: Select all

MyVar:=LV_EX_CalcViewSize(HLV, Rows := 5)
Or

Code: Select all

MyRows:=LV_GetCount()
LV_EX_CalcViewSize(HLV, Rows := MyRows)
Or like this:

Code: Select all

LV_EX_CalcViewSize(HLV, MyRows)
or this, if variable names aren't supported

Code: Select all

LV_EX_CalcViewSize(HLV, 5)

----------

Once that is resolved, I still don't understand how to extract the "H" (the height) which is all I want, not this object containing Width and Height thing.
From the docs, I can get values from object properties by doing this:

Code: Select all

Value := Object.Property
So what is the objects name of this function?

Code: Select all

MyVar:=LV_EX_CalcViewSize(HLV, Rows := 0)
is it

Code: Select all

MyVar
or is it

Code: Select all

HLV
or is it something else?

And once that is resolved

how the heck do I get the H "value" from it without knowing what the object "property" is?
Is it merely named "property" like this?

Code: Select all

Height := MyVar.property.H
or like this:

Code: Select all

Height := HLV.property.H
or like this:

Code: Select all

Height := HLV.H
or like this:

Code: Select all

Height := MyVar.H

............

There is just too many possibilities for me to find the answer via trial and error, I did try though

I hope everyone can see how fucking useless it is to mention "objects" with "key/value" pairs in a readme, with no guidance on how to use that info in the docs or the readme, kind of like taking someone out in the middle of the ocean and telling them to swim south to find treasure. They have half the information, but without knowing which direction south is, how the hell are they supposed to do anything with your helpful advice?
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

17 Apr 2016, 00:17

Image

Here is the docs page talking about Objects.Property

what the hell is Property in real situations?
is it the "H" in this example and the "W", why doesn't docs say properties can be names, vars, return-parameter-whatevers, why is there no helpful examples showing how this works beneath it in that page of the docs, for all the sections, there's just piles and piles of new information with no "show and tell" of that information or how it actually looks

furthermore, this guy just_me says in his readme that:
Returns:
An object containing the following key/value pairs:

docs dont say shit about objects having key/value pairs, it talks about how objects have Object.Property and Value, no keys or pairs
so are the docs wrong, or is just_me making up stuff, or is this simply undocumented
or is it put in an entirely stupid place in the docs, besides the "Objects" docs page.
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Don't understand returned object syntax for this function

17 Apr 2016, 03:21

The description might be confusing. The returned object is actually an associative array , but arrays are again objects in AHK as yet.

Usage sample:

Code: Select all

Size := LV_EX_CalcViewSize(HLV, 5)
; or
Size := LV_EX_CalcViewSize(HLV, MyVarKeepingTheNumberOfRows)
; ...
Width := Size.W
Height := Size.H
; or
Width := Size["W"]
Height := Size["H"]
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

19 Apr 2016, 22:34

It doesn't work. I guess the lib/function is broken, no wonder I was pulling my hair out

Here's the script I tested with

Code: Select all

Size:=LV_EX_CalcViewSize(HLV, 5)
Height:=Size.H
Width:=Size.W
msgbox, %Height% %Width%
return

LV_EX_CalcViewSize(HLV, Rows := 0) {
   ; LVM_APPROXIMATEVIEWRECT = 0x1040 -> http://msdn.microsoft.com/en-us/library/bb774883(v=vs.85).aspx
   SendMessage, 0x1040, % (Rows - 1), 0, , % "ahk_id " . HLV
   Return {W: (ErrorLevel & 0xFFFF), H: (ErrorLevel >> 16) & 0xFFFF}
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Don't understand returned object syntax for this function

19 Apr 2016, 22:46

You have no HLV, it is meant to be the handle to a ListView control.
Try this:

Code: Select all

Gui, Add, ListView, hwndHLV
Gui, Show

Size:=LV_EX_CalcViewSize(HLV, 5)
Height:=Size.H
Width:=Size.W
msgbox, %Height% %Width%
return

LV_EX_CalcViewSize(HLV, Rows := 0) {
   ; LVM_APPROXIMATEVIEWRECT = 0x1040 -> http://msdn.microsoft.com/en-us/library/bb774883(v=vs.85).aspx
   SendMessage, 0x1040, % (Rows - 1), 0, , % "ahk_id " . HLV
   Return {W: (ErrorLevel & 0xFFFF), H: (ErrorLevel >> 16) & 0xFFFF}
}
I hope that helps.
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

20 Apr 2016, 00:22

Yes, thanks, I figured that might've been the problem, and I actually tried testing that, and I had read in the docs the way to get a hwnd for a control is by ControlGet

so I tried this and about 15 other varients (moving the gui vVar and the gui gGosub stuff to different places, etc.) all was futile.

Code: Select all

; Create the ListView with two columns, Name and Size:
Gui, Add, ListView, r20 w700 vMyView gMyListView, Name|Size (KB)
Loop, %A_MyDocuments%\*.*
    LV_Add("", A_LoopFileName, A_LoopFileSizeKB)

LV_ModifyCol()  ; Auto-size each column to fit its contents.
LV_ModifyCol(2, "Integer")  ; For sorting purposes, indicate that column 2 is an integer.
Gui, Show

ControlGet,HLV,Hwnd,,MyView,ahk_exe AutoHotkey.exe
Size:=LV_EX_CalcViewSize(HLV, 5)
Height:=Size.H
Width:=Size.W
msgbox, %Height% %Width%
return

LV_EX_CalcViewSize(HLV, Rows := 0) {
   ; LVM_APPROXIMATEVIEWRECT = 0x1040 -> http://msdn.microsoft.com/en-us/library/bb774883(v=vs.85).aspx
   SendMessage, 0x1040, % (Rows - 1), 0, , % "ahk_id " . HLV
   Return {W: (ErrorLevel & 0xFFFF), H: (ErrorLevel >> 16) & 0xFFFF}
}

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo)  ; Get the text from the row's first field.
    ToolTip You double-clicked row number %A_EventInfo%. Text: "%RowText%"
}
return

GuiClose:  ; Indicate that the script should exit automatically when the window is closed.
ExitApp
In all I tried about 60 combinations of test scripts and of course none were the right one, because both the AHK official docs and just_me's docs are built on outdated methods of explaining a language, freaking frustrating.

Just show one example of literally everything that actually functionally works. Users can cut and paste and use it to their own needs then. I know there are about 50% of examples in the AHK docs to show 50% of what you can do, but that leaves half the language a difficult mystery to solve. Try using 50% of basic english to write a book.
Perfect example: ahk docs functions tell you what you can do, and only show you about 40% of what you can do.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Don't understand returned object syntax for this function

20 Apr 2016, 07:18

gallaxhar wrote:Just show one example of literally everything that actually functionally works.
I also wish the documentation was written with that sentence as the primary directive, rather than this:
"All needed information is published, but with the support of several different syntaxes, we can not afford to include any examples."
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:27

LV_EX.ahk wrote:

Code: Select all

; Notes:
;     In terms of Microsoft
;        Item     stands for the whole row or the first column of the row
;        SubItem  stands for the second to last column of a row
;     All functions require the handle of the ListView (HWND). You get this handle using the 'Hwnd' option when
;     creating the control per 'Gui, Add, HwndHwndOfLV ...' or using 'GuiControlGet, HwndOfLV, Hwnd, MyListViewVar'
;     after control creation.
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:37

just me
that is located in LV_EX.ahk while LV_EX_usage.txt doesn't mention it, also the syntax you use in that note "HwndOfLV" would not work with the syntax you used in functions "HLV", just saying it's hard to piece together if you're don't already know how it works in AHK

your docs aren't the problem though, the AHK docs are, built for newbies to use but only explained well enough for newbies to learn ~50%, and people who understand other programming languages to learn 90-100% of it
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:41

gallaxhar wrote:... also the syntax you use in that note "HwndOfLV" would not work with the syntax you used in functions "HLV" ...
Did you try it? It actually works. When passing a variable to a function, the name defined in the function header is only valid within the function.
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:45

no I wouldn't think to try it since that is not explained until now
stupid concept, but nice to know, would be awesome if we didn't have to learn through random chance. also extremely misleading implementation on AHK's part, where variable names dont match but actually use the same contents, you just have to know that they are using the same variable based on placement in a function's parentheses, harder to read code later that way, better to use same names
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:49

You should read about "How to call a function and pass parameters" before you deal with the returned values. ;)
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Re: Don't understand returned object syntax for this function

20 Apr 2016, 15:53

googling your quote and ahk at the end yielded this result
https://autohotkey.com/docs/Functions.htm

it may be in there, but I couldn't find it. so far I need to be able to read that page, your usage.txt, and your lv_ex.ahk, before even beginning to be able to use that library
and even after doing so and having you point out to me what I need, I still can't find that information from those 3 sources

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 344 guests