DesktopX Functions

Object Center in Screen

Im looking at posting some simple code samples and this was one i was asked for. 

 

Its a simple "center" function.  It centers any object in the screen.
It can be modified to work across multiple screens (see code). 

Code: vbscript
  1. Function CenterOBJ(obj)
  2.     '  QUICK and DIRTY "Center" Function
  3.     ' vars:
  4.     ' obj - object name
  5.     '--- if you want to use for multi-monitors change ScreenHeight - VScreenHeight
  6.     sw = system.ScreenWidth
  7.     sh = system.ScreenHeight
  8.     '-- divide these all in 1/2   
  9.     scw = sw / 2 '-- screen center width
  10.     sch = sh / 2 '-- screen center height
  11.     ow = desktopx.Object(obj).Width
  12.     oh = desktopx.Object(obj).height
  13.     '-- divide these in 1/2
  14.     ocw = ow / 2 '-- object center width
  15.     och = oh / 2 '-- object center height
  16.    
  17.     ol = scw - ocw
  18.     ot = sch - och
  19.     desktopx.Object(obj).left = ol
  20.     desktopx.Object(obj).top = ot
  21. End Function

 

To use this function simply use the following code:

Code: vbscript
  1.   Call CenterOBJ("ObjNameHere")

I dont know if people are interested in these types of "tutorials" but if you are, please let me know of some things you would like to see.  Im not sure if this has been posted before (I'm sure it has somewhere).

RomanDA

39,611 views 12 replies
Reply #1 Top

That's ratehr cool.  Thanks!

Reply #2 Top

Sweet David, would that script be like semi-locking something to the center? because that would rock!:thumbsup:

Reply #3 Top

i dont know what you mean by "semi-locking".

This moves whatever object you want to the center of the screen.

Reply #5 Top

just make the object unable to be moved..

Reply #6 Top

*smacks forhead* I'm an idiot.. hehe I've seen that option beforeX| :grin:

Reply #8 Top

What I'd really love.. is an easy to follow tut on making a calendar:grin:

 

/me really WILL start ticking if she makes another clockX|

Reply #9 Top

What I'd really love.. is an easy to follow tut on making a calendar

Here is the easiest tut you will find. Go to Martin's gallery, he has a template there intended for you to use for learning calendars. Use your own graphics, give Martin credit for his script.  Easy enough?  Hope this helps.

Reply #10 Top

Jim, is that the calendar with the link to the red DX one? there is a missing PNG listed in the tut, and it's too confusing.. I've tried that a dozen times, basically, I edit existing calandars to use myself, but just can't get them right.. I'd like to do one from scratch, with a template included :)... hehe well, that wouldn't be from scratch, but a blank template would be nice, I might be able to get somewhere hehehe, love Martin's stuff though :)

Reply #12 Top

As a side note:

system.ScreenWidth and system.ScreenHeight can go a bit crazy on multi-monitor setups that dont use a 1-2-3 lineup of monitors.  Those only get the width/height of the primary monitor.  To determine 'true' width and height on all screens, try using the following:

Code: vbscript
  1. sw = System.VScreenWidth
  2. sh = System.VScreenHeight

However, if you're going to do calculations with this full width, you need to take into account that the left-most pixel wont always be at 0.  For example my secondary monitor is on my left, not my right, which means that its left-most pixel is at -1680.  This means, in order to place the objects properly 'centered' in the full width, you need to use the proper left-value.

Code: vbscript
  1. sw = sw / 2
  2. sh = sh / 2
  3. scw = System.VScreenLeft + sw
  4. sch = System.VScreenTop + sh

After this bit, you still need to do the part that centres the object on that point, but that should get ya started on true positioning on multi-monitor setups.

 

I use a slightly more complex version in this object: Filler

+1 Loading…