AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GetDesktopArea function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Feb 13, 2006 5:46 pm    Post subject: GetDesktopArea function Reply with quote

[EDIT] As pointed in next message, there is a built-in command for this:
SysGet mon, MonitorWorkArea

Sorry for the noise.
I left the original message below.

---

Althought I found some related topics, I have not seen a function that just return the usable desktop area, ie. the desktop area minus the taskbar and various other possible toolbars.

It can be useful to snap windows to a side of this area, for example (a limitation of Titan's WindowSizing inspired this hack).

So I made this function, designed to be simple to use and fast: I compute the values once, then "cache" them and return them on demand. I had to do that because AutoHotkey cannot return an array or a structure.

Code:
/*
// Call this function with "x", "y", "w" or "h" parameter
// to get respectively the top-left coordinates of the desktop area,
// its width or its height.
// The "desktop area" is the screen minus docked toolbars like the taskbar.
*/
GetDesktopArea(p)
{
   static sbSet, sX, sY, sW, sH
   If not sbSet
   {
      ; http://support.microsoft.com/support/kb/articles/Q154/8/23.asp
      ; RECT structure: left, top, right, bottom
      VarSetCapacity(desktopAreaRect, 16)

      DllCall("SystemParametersInfo"
         , "UInt", 0x30   ; uiAction=SPI_GETWORKAREA
         , "UInt", 0   ; uiParam
         , "UInt", &desktopAreaRect   ; PVOID pvParam
         , "UInt", 0)   ; fWinIni
      sX := GetLong(desktopAreaRect, 1)
      sY := GetLong(desktopAreaRect)
      sW := GetLong(desktopAreaRect) - sX
      sH := GetLong(desktopAreaRect) - sY

      sbSet := true
   }
   IfEqual p, x, Return sX
   IfEqual p, y, Return sY
   IfEqual p, w, Return sW
   IfEqual p, h, Return sH
}

GetLong(ByRef struct, pos=0)
{
   local sa
   static sPos

   If (pos != 0)
   {
      ; Reset
      sPos = pos
   }
   Else
   {
      ; Next
      sPos++
   }
   ; Long: 32 bits = 4 bytes
   sa := &struct + (sPos - 1) * 4
   Return *sa + (*(sa+1) << 8) + (*(sa+2) << 16) + (*(sa+3) << 24)
}


Test:
Code:
x := GetDesktopArea("x")
y := GetDesktopArea("y")
w := GetDesktopArea("w")
h := GetDesktopArea("h")
MsgBox %x% - %y% - %w% - %h%

Easy enough? Smile
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on Tue Feb 14, 2006 9:54 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Mon Feb 13, 2006 10:04 pm    Post subject: Reply with quote

What's wrong with
Code:
SysGet Mon, MonitorWorkArea
MsgBox %MonLeft% - %MonTop% - %MonRight% - %MonBottom%
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Tue Feb 14, 2006 8:50 am    Post subject: Reply with quote

That it is hard to find? Laughing Embarassed Or too simple to use? Wink

I stupidly overlooked this one, searching the SPI_GETWORKAREA constant in the page...
It was surprising that Chris didn't provided such a function, it is nice that I was wrong Smile Looks like I reinvented the wheel Wink

Note: either moderator suppress this obsolete thread, or keep it as the trick of cached variables may inspire other people. But I can reuse it in other scripts.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3841
Location: Bremen, Germany

PostPosted: Tue Feb 14, 2006 9:01 am    Post subject: Reply with quote

You should edit your first post and mention that there is a simpler solution. So that users find it right away.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
JSLover



Joined: 20 Dec 2004
Posts: 541
Location: LooseChange911.com... the WTC attacks were done by the US Gov't... the official story is a lie...

PostPosted: Tue Feb 14, 2006 9:19 am    Post subject: Reply with quote

Laszlo wrote:
What's wrong with

...or my SPI_GetWorkArea() / A_ScreenWidthWA() / A_ScreenHeightWA() functions...

Code:
msgbox, % "0 - 0 - " A_ScreenWidthWA() " - " A_ScreenHeightWA()
;...is Left/Top ever not 0?...if so...
;msgbox, % SPI_GetWorkArea("Left") " - " SPI_GetWorkArea("Top") " - " A_ScreenWidthWA() " - " A_ScreenHeightWA()

SPI_GetWorkArea(which)
{
   SysGet, WA, MonitorWorkArea
   if which=top
      return WATop
   else if (which="bottom" || which="height")
      return WABottom
   else if which=left
      return WALeft
   else if (which="right" || which="width")
      return WARight
}

A_ScreenWidthWA()
{
   return SPI_GetWorkArea("width")
}

A_ScreenHeightWA()
{
   return SPI_GetWorkArea("height")
}

...why cache?...what if a toolbar is hidden/shown between calls?

I'd like A_ScreenWidthWA / A_ScreenHeightWA as builtin vars (& perhaps A_ScreenLeftWA / A_ScreenTopWA...if they are ever not 0)...& maybe A_ScreenLeftVirt / A_ScreenTopVirt / A_ScreenWidthVirt / A_ScreenHeightVirt (corresponding to the 76 / 77 / 78 / 79 values of the SysGet command).

PhiLho...when you link with p=48008 it's impossible to know which topic it belongs to...plus when you are linking to the 1st post (the topic, not a post in it) you should use...t=8007...or when linking to a post that's not on the 1st page...t=8007&p=48008#48008...it makes it clear which topic it belongs to.
_________________

Home • Click image! • Blog
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Tue Feb 14, 2006 10:19 am    Post subject: Reply with quote

JSLover wrote:
[What's wrong with]... my SPI_GetWorkArea() / A_ScreenWidthWA() / A_ScreenHeightWA() functions...

Nothing. I searched for SystemParametersInfo, not for MonitorWorkArea...
At least, users reading this topic will have choice now Smile

JSLover wrote:
...why cache?...what if a toolbar is hidden/shown between calls?

Remark is pertinent, but if screen configuration change between calls, you will get incorrect or inconsistent values anyway.
The SysGet command sets 4 variables at once, so at least it is consistent.
These variables can be local.
I see no way to do the same with a function call, keeping result local.
Hence the cache idea: maintain consistency, if not accuracy.
And probably of changes between two calls, which are likely to be successive, is very small.

JSLover wrote:
when you link with p=48008 it's impossible to know which topic it belongs too...plus you are linking to the 1st post...the topic, not a post in it...

It was intentional, I was refering to the program made by Titan, not to a specific comment message. But finally, I understand what you mean by not knowing the topic (see below).

JSLover wrote:
you should use...t=8007...when linking to a topic

Thank you for the tip.
Actually, I don't know how I got this p=48008 (which works), as I usually link to topics by copying the link from the list of topics for a given area.
I will try and be more attentive to this issue.

So, when I put p=xxx, I refer to a given post (the first one in my case), while a t=xxx refers to a given topic (or thread)? Using both designates topic and thread.
Thank you for the information.

I see that p=48008 is a reference to my post about this topic (why did I used it?), although using it alone display the whole topic... OK, I won't repeat the error now.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group