DesktopX: Scripting Images (Beginner)

From WinCustomize Wiki

Jump to: navigation, search


Scripting Images
Original Author:sViz
Date Created:October 13, 2007
Application:DesktopX
Programs Used:DesktopX


Applying images to your objects through script makes your object more dynamic, allowing the user to change the image without having to go into DX Builder. There are two ways you can do this: through drag and drop or through the use of a dialog box.

First we need to set the width and height of the object. Go to Object Properties > Summary and do so (200x200 or something sizeable). If you don’t set these parameters the object will take on the size of the image.

Contents

Drag and Drop

Here’s the Stardock tutorial - Drag and Drop Pictures

Dialog Box

Sometimes drag and drop might not be applicable or is just inconvenient. You can use a dialog box to browse for the image and then apply it. It’s really easy.

OnLButtonUp, we call the function that brings up the dialog box. In the Opendialog procedure, we bring up the dialog box and then set the object’s image only if the user selects something.


Function Object_OnLbuttonUp(x,y,dragged)
	If Not dragged Then Opendialog
End Function

Sub Opendialog
	file = System.FileOpenDialog("Select Image", "", "", "PNG|*.png|JPEG|*.jpg|All files|*.*",0)
	If file <> "" Then DesktopX.Object("object1").states("").picture = file
End Sub

The syntax for system.fileopendialog is as follows:

System.FileOpenDialog (title, defaultFile, intialDir, extensions, flags)

Example:

System.FileOpenDialog("Select Image...", "Test.png", "C:\", "PNG|*.png|JPEG|*.jpg|All files|*.*",0)

Image:Dx filedialog.PNG

As you can see “Select Image” appears in the title bar, “Test.png” is the default file, “C:\” is where the browsing starts from, and the file types listed in the drop down are pngs, jpegs, and all files. You can change these settings to whatever you’re supposed to be browsing for. The list of flags you can use is in the link at the end of this tutorial.


Thumbnails

This is just a nifty function you might find useful for a slideshow, or a wallpaper changer etc. The formula for this thumbnail script is simple enough. We want to scale down the size of an image proportionately. To do this we’ll need to know the original dimensions, the percentage difference between the height and width, and the maximum size we want to scale it down to. This isn't the definitive way to do this but I thought I would share this method here.

Create two objects. Name them “object1” and “object2”. Object1 will display the thumbnail, & object2 will get us the original image dimensions. Set object2’s visibility to false so we don’t have any large images taking up the screen. Put this script into object1:

Dim maxSize

'Called when the script is executed
Sub Object_OnScriptEnter
	'--Maximum thumbnail size
	maxSize = 300
End Sub

'Called when files are dropped onto object
Sub Object_OnDropFiles(files)
	DesktopX.Object("object2").states("").picture = files
	oldW = DesktopX.Object("object2").width
	oldH = DesktopX.Object("object2").height
	'If image width is greater than image height
	'calculate what percentage height is of width
	'set new width and new height
	If oldW > oldH Then
		newH = Int((maxSize/100)*(oldH/oldW)*100)
		newW = maxSize
	'If image height is greater than image width
	'calculate what percentage width is of height
	ElseIf oldW < oldH Then
		newW = Int((maxSize/100)*(oldW/oldH)*100)
		newH = maxSize
	'If image dimensions are equal
	'set height and width to maxSize
	ElseIf oldW = oldH Then
		newH = maxSize
		newW = maxSize
	End If
	'If image width and height are smaller than maxSize
	'use original image dimensions
	If oldW < maxSize Then 
		If oldH < maxSize Then
			newH = sizeObj.height
			newW = sizeObj.width
		End If
	End If
	'Set object width, height, and picture
	object.Width = newW
	object.Height = newH
	Desktopx.Object("object1").states("").picture = files
End Sub

Drag and drop an image onto the widget to try it out. That's it!

Tips

None right now.

Further Reading