March 29, 2024, 06:03:00 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Fun With Windows

Started by ZeroDog, July 19, 2009, 12:18:18 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ZeroDog

July 19, 2009, 12:18:18 AM Last Edit: July 19, 2009, 02:25:51 AM by ZeroDog
-= ZeroDog's Tips 'N Tricks =-
    -= Fun With Windows =-

Need to add a bit of spice to your EBasic windows?  Here is a small collection
of tips and tricks you can use in your own projects.  Some are good for any
project, some are good for splash screens, and some are just for fun.

Part 1 - Window Animations
Part 2 - Blended Windows
Part 3 - Color Masked Windows
Part 4 - Fading Windows in/out
Part 5 - Text Regions
Part 6 - Mask 'N Blend
Part 7 - Color Mask Animation
Part 8 - Drag 'N Drop Windows

Happy Coding!

ZeroDog

July 19, 2009, 12:18:52 AM #1 Last Edit: July 19, 2009, 02:27:27 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 1 - Window Animations
'*******************************

'Simple window animations such as rolling open or closed is
'easy to do by using the AnimateWindow API function.

'There are 4 different types of animations you can use with
'the AnimateWindow function: Roll, slide, center and blend.

'The roll animation is the default animation, and will be
'used if no animation style is specified.  The roll animation
'will clip the window in the specified direction until the window
'is completely hidden/displayed.

'The slide animation will slide the window in the specified
'direction until the the window is completly hidden/displayed.

'The center animation will collapse/expand the window to/from
'the center of the window until the window is completely
'hidden/displayed.

'The blend animation is only available on win2k or higher and
'will only work with top level windows.
'The blend animation will blend the window with the windows
'and background under the window until the window is completly
'hidden/displayed.
'(**I have not been able to get this animation to work**)

'First thing we need to do is define our window variable
DEF win:WINDOW

'Now declare the API function call (we can IMPORT from user32.dll)
DECLARE IMPORT,AnimateWindow(hwnd:INT, dwTime:INT, dwFlags:INT),INT
'hwnd is the handle of the window to animate. 
'  Ex:  win.hwnd
'dwTime is the length of the animation in millisecs.
'  Ex: 2000
'dwFlags is one or more of the AW_ constant flags used to specify which
'type of animation, what directions to use with the roll/slides, and if
'the animation is hiding or restoring the window.
'  Ex: AW_SLIDE + AW_VER_POSITIVE + AW_HIDE
'Example useage of the function:
'AnimateWindow(win.hwnd, 2000, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE)

'Next we set our constants that we will be using with the AnimateWindow function
'The following 3 constants are used to set what style of animation will be used.
'By default, the roll animation is used if no other styles are specified.
CONST AW_CENTER   = 0x10    :'Center animation. No direction constants are needed when using this style.
CONST AW_SLIDE    = 0x40000 :'Slide animation.
CONST AW_BLEND    = 0x80000 :'Blend animation. This flag can be used only if hwnd is a top-level window.
'The following 4 constants are used to control the direction of roll and slide animations.
CONST AW_HOR_POSITIVE = 0x1 :'Animates the window from left to right.
CONST AW_HOR_NEGATIVE = 0x2 :'Animates the window from right to left.
CONST AW_VER_POSITIVE = 0x4 :'Animates the window from top to bottom.
CONST AW_VER_NEGATIVE = 0x8 :'Animates the window from bottom to top.
'The following 2 constants specify if the window is to be hidden or restored by the animation.
'By default the window is restored if neither AW_HIDE nor AW_ACTIVATE are specified.
CONST AW_HIDE     = 0x10000 :'Hides the window.
CONST AW_ACTIVATE = 0x20000 :'Restores the window.

'Now we open up our window.
OPENWINDOW win,0,0,300,200,@CAPTION|@SIZE,0,"ZDTNT-FWW-1:Window Animations",&winproc
'We will set up our menu in the window process handler when the @IDCREATE message
'is sent and use the AnimateWindow function to initially display the window.

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages, such as menu item selection messages, until the
'run variable is set to 0, when the program is closed.
run=1
WAITUNTIL run=0
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window/program is closed
IF @MESSAGE = @IDCLOSEWINDOW THEN run=0

'--This message is sent when the window is initially created
IF @MESSAGE = @IDCREATE
'Set up a quick little menu to activate some of the different window animations
BEGINMENU win
MENUTITLE "Animations"
MENUITEM "Center",0,1
MENUITEM "Slide",0,2
MENUITEM "Slide 2",0,3
MENUITEM "Roll",0,4
MENUITEM "Roll 2",0,5
MENUITEM "Blend",0,6
ENDMENU
'Center the window, and use the center animation to initially display it
CENTERWINDOW win
AnimateWindow(win.hwnd,1000, AW_CENTER + AW_ACTIVATE )
ENDIF

'--This message is sent when the user selects a menu item
IF @MESSAGE = @IDMENUPICK
'When a menu item is selected, we will Animate the window twice
'First we will hide the window using the animation selected, and
'then we will restore the window back so the user can try another animation.
IF @MENUNUM = 1 :'Center animation was selected
'Collapse(hide) the window inwards using the CENTER anmimation
AnimateWindow(win.hwnd,2000, AW_CENTER + AW_HIDE )
'Expand(restore) the window outwards using the CENTER animation
AnimateWindow(win.hwnd,1000, AW_CENTER + AW_ACTIVATE )
ENDIF
IF @MENUNUM = 2 :'Slide animation was selected
'Hide SLIDE the window downwards
AnimateWindow(win.hwnd,2000, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE )
'Restore SLIDE the window to the left
AnimateWindow(win.hwnd,1000, AW_SLIDE + AW_HOR_POSITIVE + AW_ACTIVATE )
ENDIF
IF @MENUNUM = 3 :'Slide 2 animation was selected
'Hide SLIDE the window to the bottom right
AnimateWindow(win.hwnd,2000, AW_SLIDE + AW_HOR_POSITIVE + AW_VER_POSITIVE + AW_HIDE )
'Restore SLIDE the window to the top right
AnimateWindow(win.hwnd,1000, AW_SLIDE + AW_HOR_POSITIVE + AW_VER_NEGATIVE + AW_ACTIVATE )
ENDIF
IF @MENUNUM = 4 :'Roll animation was selected
'Hide ROLL the window to the bottom
AnimateWindow(win.hwnd,2000, AW_VER_POSITIVE  + AW_HIDE ) 
'Restore ROLL the window to the bottom
AnimateWindow(win.hwnd,1000, AW_HOR_POSITIVE + AW_ACTIVATE )
ENDIF
IF @MENUNUM = 5 :'Roll 2 animation was selected
'Hide ROLL the window to the top right
AnimateWindow(win.hwnd,2000, AW_VER_NEGATIVE + AW_HOR_POSITIVE + AW_HIDE )
'Restore ROLL the window to the bottom right
AnimateWindow(win.hwnd,1000, AW_VER_POSITIVE + AW_HOR_POSITIVE + AW_ACTIVATE )
ENDIF
IF @MENUNUM = 6 :'Blend animation was selected
'Fade the window out until hidden
AnimateWindow(win.hwnd,2000, AW_BLEND + AW_HIDE )
'Fade the window in until restored
AnimateWindow(win.hwnd,1000, AW_BLEND + AW_ACTIVATE )
ENDIF
ENDIF
RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:19:21 AM #2 Last Edit: July 19, 2009, 02:28:05 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 2 - Blended Windows
'*******************************

'Creating blended windows is easy to do in only a few steps using the
'SetLayeredWindowAttributes API function the and MODIFYEXSTYLE function.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function call (we can IMPORT from user32.dll)
DECLARE IMPORT, SetLayeredWindowAttributes(hWnd:INT, crKey:INT, bAlpha:INT, dwFlags:INT), INT
'hwnd is the handle of the window to blend. 
'  Ex:  win.hwnd
'crKey is used for another purpose, so we will set it to null.
'  Ex: 0
'bAlpha is the alpha blending value. It can be between 0(completly transparent) and 255(solid).
'  Ex: 172
'dwFlags is the style flag that will be applied. In this case we will be using the alpha blending flag.
'  Ex: LWA_ALPHA
'Example useage of the function:
'SetLayeredWindowAttributes(win.hwnd, 0, 172, LWA_ALPHA)

'You can only use layered window styles on layered windows so we must
'modify the extended style of the window to be a layered window
'before applying the alpha blending style to the window by using MODIFYEXSTYLE.

'Set the constant that we will be using with the MODIFYEXSTYLE function.
CONST WS_EX_LAYERED = 0x80000 :'This constant specifies a layered window.
'Set the constant that we will be using with the SetLayeredWindowAttributes function.
CONST LWA_ALPHA = 0x2 :'This constant specifies alpha blending for the layered window attribute.

'Now we open up our window.
OPENWINDOW win, 0,0,300,300,@CAPTION|@SIZE, 0, "ZDTNT-FWW-2: Blended Windows",&winproc

'We now modify the window we opened to be a layered window.
MODIFYEXSTYLE win, WS_EX_LAYERED, 0

'And finally we set the alpha blending style and amount to the now layered window.
SetLayeredWindowAttributes(win.hwnd, 0, 172, LWA_ALPHA)

'Set the window color to red just to make it easier to see the effect.
SETWINDOWCOLOR win,rgb(255,0,0)

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed.
run=1
WAITUNTIL run=0
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window is initially created.
IF @MESSAGE = @IDCREATE THEN CENTERWINDOW win

'--This message is sent when the window/program is closed.
IF @MESSAGE = @IDCLOSEWINDOW THEN run=0
RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:19:47 AM #3 Last Edit: July 19, 2009, 02:29:04 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 3 - Color Masked Windows
'*******************************

'Creating color masked windows is easy to do in just a few steps using the
'SetLayeredWindowAttributes API function and the MODIFYEXSTYLE function.
'Simply create a layered window, apply a masking color, and anything that
'is drawn in the window using the masking color will be completely transparent.
'Keep in mind however, that the color masking of the window applies to the entire
'window, including menus, titlebars, buttons, images, and just about anything that
'is drawn in the window.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function call (we can IMPORT from user32.dll)
DECLARE IMPORT, SetLayeredWindowAttributes(hWnd:INT, crKey:INT, bAlpha:INT, dwFlags:INT), INT
'hwnd is the handle of the window to blend. 
'  Ex:  win.hwnd
'crKey is the masking color in RGB. 
'  Ex: RGB(255,0,0)
'bAlpha is used for another purpose so we will leave it null.
'  Ex: 0
'dwFlags is the style flag that will be applied. In this case we will be using the color masking flag.
'  Ex: LWA_COLORKEY
'Example useage of the function:
'SetLayeredWindowAttributes(win.hwnd, RGB(255,0,0), 0, LWA_COLORKEY)

'Next we set the constant that we will be using with the MODIFYEXSTYLE function.
CONST WS_EX_LAYERED = 0x80000 :'This constant specifies a layered window.
'Set the constant that we will be using with the SetLayeredWindowAttributes function.
Const LWA_COLORKEY = 0x1 :'This constant specifies color masking for the layered window attribute.

'Now we open up our window.
OPENWINDOW win, 0,0,300,300,@CAPTION|@SIZE, 0, "ZDTNT-FWW-2: Color Masked Windows",&winproc

'We now modify the window we opened to be a layered window.
MODIFYEXSTYLE win, WS_EX_LAYERED, 0

'And finally we set the color masking style to the now layered window.
'The color red, RGB(255,0,0), is set as the masking color, so anything that is drawn in red
'in the window will be transparent.
SetLayeredWindowAttributes(win.hwnd, RGB(255,0,0), 0, LWA_COLORKEY)

'Now we will just draw a few items to the window to make the effect easier to see.
SETWINDOWCOLOR win,RGB(0,0,255) :'Set the window color to blue
CIRCLE win, 150,150, 100, RGB(255,0,0),RGB(255,0,0):'Draw a red circle which will be transparent
DRAWMODE win, @TRANSPARENT :'text will be drawn with no backpen color
FRONTPEN win,RGB(255,0,0) :'frontpen set to red so text will be transparent
SETFONT win,"arial",48,700 :'set our font type, size and weight
PRINT win," ZeroDog " :'print some text to the window
MOVE win,0,175 :'move the print cursor to lower on the window
FRONTPEN win,RGB(0,255,0) :'frontpen set to green so text will be solid.
PRINT win," ZeroDog " :'print some text to the window
'Note: when using color masking and printing to the window, you will sometimes notice
'that the font smoothing options set in the display settings may leave little bits of odd
'coloring around fonts that are being masked around due to the antialiasing of the font.
'Turning off font smoothing in your dispay settings will eliminate these extra bits.

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed.
run=1
WAITUNTIL run=0
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window is initially created.
IF @MESSAGE = @IDCREATE THEN CENTERWINDOW win

'--This message is sent when the window/program is closed.
IF @MESSAGE = @IDCLOSEWINDOW THEN run=0
RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:20:28 AM #4 Last Edit: July 19, 2009, 02:29:43 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 4 - Fading Windows in/out
'*******************************

'Want your windows to fade in and out when you open and close them? No Problem.
'Just a few lines of code using the SetLayeredWindowAttributes API function
'and the MODIFYEXSTYLE function are all you need.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function call (we can IMPORT from user32.dll)
DECLARE IMPORT, SetLayeredWindowAttributes(hWnd:INT, crKey:INT, bAlpha:INT, dwFlags:INT), INT
'hwnd is the handle of the window to blend. 
'  Ex:  win.hwnd
'crKey is used for another purpose, so we will set it to null.
'  Ex: 0
'bAlpha is the alpha blending value. It can be between 0(completly transparent) and 255(solid).
'  Ex: 128
'dwFlags is the style flag that will be applied. In this case we will be using the alpha blending flag.
'  Ex: LWA_ALPHA
'Example useage of the function:
'SetLayeredWindowAttributes(win.hwnd, 0, 128, LWA_ALPHA)

'You can only use layered window styles on layered windows so we must
'modify the extended style of the window to be a layered window
'before applying the alpha blending style to the window by using MODIFYEXSTYLE.

'Set the constant that we will be using with the MODIFYEXSTYLE function.
CONST WS_EX_LAYERED = 0x80000 :'This constant specifies a layered window.

'Set the constant that we will be using with the SetLayeredWindowAttributes function.
CONST LWA_ALPHA = 0x2 :'This constant specifies alpha blending for the layered window attribute.

'Now we open up our window. The window is created at -10000,-10000 (x,y)
'to hide it from the user until we are ready to fade it in.
OPENWINDOW win, -10000,-10000,300,300,@CAPTION|@SIZE, 0, "ZDTNT-FWW-4: Fading Windows in/out",&winproc

'We now modify the window we opened to be a layered window.
MODIFYEXSTYLE win, WS_EX_LAYERED, 0

'Set the window color to green just to make it easier to see the effect.
SETWINDOWCOLOR win,RGB(0,255,0)

'We are ready to fade in, so now we center the window
CENTERWINDOW win

'This is the fade in loop. The counter x is the opacity of the window,
'0 being totally transparent, 255 being totally opaque.
'For a faster/slower fade in, increase/decrease the step.
FOR x = 0 TO 255 STEP 4
'we set the alpha blending style and amount to the layered window.
SetLayeredWindowAttributes(win.hwnd, 0, x, LWA_ALPHA)
'wait for any pending window messages.
WAIT 1
NEXT x

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed.
run=1
WAITUNTIL run=0

'the program has now been closed by the user, so its time to fade the window out.
'The counter x is the opacity of the window, 0 being totally transparent,
'255 being totally opaque.
'For a faster/slower fade out, decrease/increase the step.
FOR x = 255 TO 0 STEP -4
'we set the alpha blending style and amount to the layered window.
SetLayeredWindowAttributes(win.hwnd, 0, x, LWA_ALPHA)
'wait for any pending window messages
WAIT 1
NEXT x

'Close up the window and end the program.
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window/program is closed.
IF @MESSAGE = @IDCLOSEWINDOW THEN run=0
RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:20:51 AM #5 Last Edit: July 19, 2009, 02:30:12 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 5 - Text Regions
'*******************************

'Text regions are similar to color masking using text, except
'that text regions are always pixel perfect even if font smoothing
'is turned on in your display settings, and transparency is not
'dependant on colors painted in the window.  You can switch between
'normal and inverted text regions by switching the drawing mode
'between @TRANSPARENT and @OPAQUE.

'Press ESC or ALT-F4 to exit the program.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function calls (we can IMPORT them from gdi32.dll and user32.dll)
DECLARE IMPORT,EndPath(hdc:INT),INT
DECLARE IMPORT,BeginPath(hdc:INT),INT
DECLARE IMPORT,PathToRegion(hdc:INT),INT
DECLARE IMPORT,TextOutA(hdc:INT, x:INT, y:INT, lpSTRING:STRING, nCount:INT),INT
'hdc is the handle to the device context of the window which we get by using the GETHDC function.
'x and y are the top and left positions to align the text to.
'pSTRING is the text string to print.
'nCount is the number of characters in the string.
DECLARE IMPORT,SetWindowRgn(hWnd:INT, hRgn:INT, bRedraw:INT),INT
'hwnd is the handle to the window to apply the region onto.
'hRgn is the handle to the region to apply.
'bRedraw specifies if the system should redraw the window after applying the region. TRUE/FALSE
DECLARE IMPORT,DeleteObject(hObject:INT),INT
'hObject is the handle to the object to be deleted.

'Define the variables we need for the routines.
DEF WindowHDC:INT :'This variable is for the Device Context Handle of the window.
DEF TextRegion:INT :'This variable is for the handle of the Region we will create
DEF TextWidth,TextHeight:INT :'These variables are for the width and height of the text.
DEF TextStyle:INT :'This variable is for the text style, can be @TRANSPARENT or @OPAQUE.
'We will be using two lines of text for the region, so we use separate variables to hold each line.
DEF TextString,TextString2:STRING
TextString = " Ionic       " :'This is the top line of text we will use for the region.
TextString2 ="       Wind  " :'This is the bottom line of text we will use for the region.

'We need to figure out how big our window should be for the text region.
'First we open up our window temporarily, with no size or caption.
'We will make it a @TOPMOST window so its easier to see the region effect.
OPENWINDOW win,0,0,0,0,@NOCAPTION|@TOPMOST|@SYSMENU,0,"ZDTNT-FWW-5:Text Regions",&winproc
'Then we set the font type and size.
SETFONT win,"Arial",64,800
'Now we can calculate the size in pixels of the text we will be using and
'store the values into TextWidth and TextHeight.
GETTEXTSIZE win, TextString, TextWidth, TextHeight
'We now resize the window to the width of the text and double the height of
'the text, and position it at -10000,-10000 until we are ready to display it.
SETSIZE win,-10000,-10000,TextWidth,TextHeight*2

'We can set the draw mode to either @OPAQUE or @TRANSPARENT.
'Setting it to @OPAQUE will create a rectangular window with the text area cut out.
'Setting it to @TRANSPARENT will create a window that is the shape of the text only.
DRAWMODE win,@OPAQUE

'We need to get the handle for the device contect of the window.
WindowHDC=GETHDC(win)
'We need to create a path to convert to a region in the device context of the window.
BeginPath(WindowHDC) :'Begins the path for the region.
MOVE win,0,0 :'Move the text position to the left top of the window.
TextOutA(WindowHDC, 0, 0, TextString, LEN(TextString)) :'Output the text to the path.
MOVE win,0,TextHeight :'Move the text position to the left middle of the window.
TextOutA(WindowHDC, 0, TextHeight, TextString2, LEN(TextString2)) :'Output the text to the path.
EndPath(WindowHDC) :'Ends the path for the region.
TextRegion = PathToRegion(WindowHDC) :'Creates a region from the path and stores it in TextRegion.
SetWindowRgn(win.hwnd, TextRegion, 1):'Apply the region to the window, and redraw the window.
DeleteObject(TextRegion):'Free memory used by the region.
RELEASEHDC(win, WindowHDC) :'Free memory used by the HDC.

'Finally, we add a splash of color to the window
SETWINDOWCOLOR win,0 :'Paint the window black.
RECT win,0,0,TextWidth,TextHeight,RGB(0,0,255),RGB(0,0,255):'Paint the top half of the window blue.

'We are ready to display our window, so now we center it on the screen
CENTERWINDOW win

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed or when ESC is pressed.
run = 1
WAITUNTIL run = 0

'Close up the window and end the program.
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'If ESC is pressed or the user closes the window then the run variable is set to 0.
IF (@MESSAGE = @IDCHAR) & (@CODE = 0x1B) OR (@MESSAGE = @IDCLOSEWINDOW) THEN run = 0

'This message is sent when the user holds the left mouse button down
'It drags the window around, as if we had been clicking on the title bar
IF @MESSAGE = @IDLBUTTONDN THEN SENDMESSAGE (win, 0xA1,2,0)
RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:21:11 AM #6 Last Edit: July 19, 2009, 10:35:47 AM by ZeroDog
Dont forget to download the image for this example at the bottom of the post.
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 6 - Mask 'N Blend
'*******************************

'Creating color masked windows is easy to do in just a few steps using the
'SetLayeredWindowAttributes API function and the MODIFYEXSTYLE function.
'Simply create a layered window, apply a masking color, and anything that
'is drawn in the window using the masking color will be completely transparent.
'Keep in mind however, that the color masking of the window applies to the entire
'window, including menus, titlebars, buttons, images, and just about anything that
'is drawn in the window.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function call (we can IMPORT from user32.dll)
DECLARE IMPORT, SetLayeredWindowAttributes(hWnd:INT, crKey:INT, bAlpha:INT, dwFlags:INT), INT
'hwnd is the handle of the window to blend. 
'  Ex:  win.hwnd
'crKey is the masking color in RGB. 
'  Ex: rgb(0,255,0)
'bAlpha is the alpha blending value. It can be between 0(completly transparent) and 255(solid).
'  Ex: 150
'dwFlags is the style flag that will be applied. In this case we will be using the color masking flag.
'  Ex: LWA_COLORKEY
'Example useage of the function:
'SetLayeredWindowAttributes(win.hwnd, rgb(0,255,0), 150, LWA_COLORKEY)

'Next we set the constant that we will be using with the MODIFYEXSTYLE function.
CONST WS_EX_LAYERED = 0x80000 :'This constant specifies a layered window.
'Set the constant that we will be using with the SetLayeredWindowAttributes function.
CONST LWA_COLORKEY = 0x1 :'This constant specifies color masking for the layered window attribute.
CONST LWA_ALPHA = 0x2 :'This constant specifies alpha blending for the layered window attribute.

'Now we open up our window.
OPENWINDOW win, -10000,-10000,600,400,@NOCAPTION|@SYSMENU, 0, "ZDTNT-FWW-6: Mask N Blend",&winproc

'We now modify the window we opened to be a layered window.
MODIFYEXSTYLE win, WS_EX_LAYERED, 0

'And finally we set the color masking style to the now layered window.
'The color green, rgb(0,255,0), is set as the masking color, so anything that
'is drawn in green in the window will be transparent.
SetLayeredWindowAttributes(win.hwnd, RGB(0,255,0), 150, LWA_COLORKEY | LWA_ALPHA)

bgImage = LOADIMAGE (GETSTARTPATH+"ZeroDog.gif", @IMGSCALABLE)
SHOWIMAGE win, bgImage, @IMGSCALABLE, 0, 0 , 600, 400

DELETEIMAGE bgImage, @IMGSCALABLE

CENTERWINDOW win


'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed.
run=1
WAITUNTIL run=0

CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'If ESC is pressed or the user closes the window then the run variable is set to 0.
IF (@MESSAGE = @IDCHAR) & (@CODE = 0x1B) OR (@MESSAGE = @IDCLOSEWINDOW) THEN run = 0

IF @MESSAGE = @IDLBUTTONDN THEN SENDMESSAGE (win, 0xA1,2,0) : 'Drag window
RETURN
ENDSUB


ZeroDog

July 19, 2009, 12:21:34 AM #7 Last Edit: July 19, 2009, 02:31:29 AM by ZeroDog
'ZeroDog's Tips 'N Tricks
'Fun With Windows
'Part 7 - Color Mask Animation
'*******************************

'Ever seen a realtime window region animation before?
'In this example we will be creating one using two windows.
'One window will be hidden, which we will use as a 'backbuffer'.
'The other window will be centered on the screen which we will use
'as a 'frontbuffer', and will have a color mask applied to it.
'We will use the SetLayeredWindowAttributes to apply the color mask.
'Then we draw each of our frames on the hidden window, using the
'masking color of the other as our background color.  Each frame
'is then copied and pasted to the frontbuffer window using the
'BitBlt function.  The window will automaticallly adjust
'the region according to the masking color.  You can left
'click and drag the window around anywhere in the window. You can
'press ESC to close the window.

'First thing we need to do is define our window variables.
DEF win,win2:WINDOW

'Now declare the API function calls.
'We can IMPORT this one from user32.dll.
DECLARE IMPORT, SetLayeredWindowAttributes(hWnd:INT, crKey:INT, bAlpha:INT, dwFlags:INT), INT
'hwnd is the handle of the window to color mask. 
'  Ex:  win.hwnd
'crKey is the masking color in RGB. 
'  Ex: RGB(255,0,0)
'bAlpha is used for another purpose so we will leave it null.
'  Ex: 0
'dwFlags is the style flag that will be applied. In this case we will be using the color masking flag.
'  Ex: LWA_COLORKEY
'Example useage of the function:
' SetLayeredWindowAttributes(win.hwnd, RGB(255,0,0), 0, LWA_COLORKEY)

'We can IMPORT this one from gdi32.dll.
Declare IMPORT, BitBlt(hDestDC:INT, x:INT, y:INT, nWidth:INT, nHeight:INT, hSrcDC:INT, xSrc:INT, ySrc:INT, dwRop:INT),INT
'hDestDC is the device context of the destination window to paste to.
' Ex: win
'x is the logical left position in pixels of the upper left corner of the destination rectangle.
' Ex: 0
'y is the logical top position in pixels of the upper left corner of the destination rectangle.
' Ex: 0
'nWidth is the width of the destination rectangle in pixels.
' Ex: 400
'nHeight is the height of the destination rectangle in pixels.
' Ex: 400
'hSrcDC is the device context of the source window to copy from.
' Ex: win2
'xSrc is the logical left position in pixels of the rectangle to copy from in the source window.
' Ex: 0
'ySrc is the logical top position in pixels of the rectangle to copy from in the source window.
' Ex: 0
'dwRop is the Raster Operation flag to use for the bit block transfer.
' Ex: SRCCOPY
'Example useage of the function:
' BitBlt(winHDC, 0,0, 400,400, win2HDC, 0,0, SRCCOPY)

'Next we set the constant that we will be using with the MODIFYEXSTYLE function.
CONST WS_EX_LAYERED = 0x80000 :'This constant specifies a layered window.
'Set the constant that we will be using with the SetLayeredWindowAttributes function.
Const LWA_COLORKEY = 0x1 :'This constant specifies color masking for the layered window attribute.
'Set the constant that we will be using with the BitBlt function.
CONST SRCCOPY = 0xCC0020 :'This flag specifies a regular copy/paste mode

'Now we open up our windows.
OPENWINDOW win, 0,0,400,400,@NOCAPTION|@TOPMOST|@SYSMENU, 0, "ZDTNT-FWW-7: Color Mask Animation",&winproc
OPENWINDOW win2, 0,0,400,400,@TOOLWINDOW|@NOCAPTION, 0, "ZDTNT-FWW-7: Hidden Window",&winproc

'We can draw and copy from a hidden window, so we hide the backbuffer window
SHOWWINDOW win2,@SWHIDE
'We now modify the frontbuffer window to be a layered window so we can color mask it.
MODIFYEXSTYLE win, WS_EX_LAYERED, 0

'The color blue, RGB(0,0,255), is set as the masking color, so anything that is drawn
'with it in the window will be transparent.
SetLayeredWindowAttributes(win.hwnd, RGB(0,0,255), 0, LWA_COLORKEY)

'We need to set up the backbuffer window font and pen color options
DRAWMODE win2, @TRANSPARENT :'text will be drawn with no backpen color
SETFONT win2,"Impact",72,700 :'set our font type, size and weight
DEF TextWidth, TextHeight:INT :' Variables for the text size
GETTEXTSIZE win2, "ZeroDog", TextWidth, TextHeight :'Calculate the size of the string we will print
FRONTPEN win2,RGB(0,0,254) :'frontpen set to blue (but not the mask color blue)
x=0.00 :'x is our counter we use to calculate our sin and cos positions
run=1 :'Our run flag.  When this is set to 0, the program closes.

'==== This is our main loop that draws the animation
DO
winHDC=GETHDC(win) :'Get the device context for the frontbuffer window so we can copy to it
win2HDC=GETHDC(win2) :' Get the device context for the backbuffer window so we can copy from it

RECT win2,0,0,400,400,RGB(0,0,255),RGB(0,0,255) :'Fill the background with the masking color

FOR z = 1 TO 4  :'Draw 4 red circles orbiting around the center of the window
CIRCLE win2, 200+SIN(x+z/3)*175,200+COS(x+z/2)*175, 10+z, RGB(255,0,0),RGB(255,0,0)
NEXT z

CIRCLE win2, 200,200, 140, RGB(0,225,0),RGB(0,225,0) :'Draw a green circle in the center

FOR z = 0 TO 5  :'Draw 6 circles in the masking color orbiting inside the green circle
CIRCLE win2, 200+SIN(x+z)*100,200+COS(x+z)*100, 30, RGB(0,0,255),RGB(0,0,255)
NEXT z

FOR z = 0 TO 5  :'Draw 6 circles in the mask color orbiting opposite direction in the green circle
CIRCLE win2, 200+SIN(-x+z)*45,200+COS(-x+z)*45, 17, RGB(0,0,255),RGB(0,0,255):'Draw a red circle which will be transparent
NEXT z

MOVE win2,200-(TextWidth/2),200 :'move the print cursor to lower center on the window
PRINT win2,"ZeroDog" :'print some text to the window

RASTERMODE win2,@RMMASKPEN :'change the raster mode
FOR z = 20 TO 23 :'Draw 4 black circles orbiting around the center of the window
CIRCLE win2, 200+SIN(x+z/3)*175,200+COS(x+z/2)*175, 10+(z-17), RGB(0,255,0),RGB(0,255,0)
NEXT z

rastermode win2,@RMCOPYPEN :'change the raster mode
FOR z = 11 TO 15 :'Draw 5 red circles orbiting around the center of the window 
CIRCLE win2, 200+SIN(x+z/3)*175,200+COS(x+z/2)*175, 10+(z-10), RGB(255,0,0),RGB(255,0,0)
NEXT z

FOR z = 31 TO 33  :'Draw 3 black circles orbiting around the center of the window
CIRCLE win2, 200+SIN(x+z/3)*175,200+COS(x+z/2)*175, 10+(z-30), RGB(0,0,0),RGB(0,0,0)
NEXT z

'Copy the entire backbuffer to the frontbuffer
BitBlt(winHDC, 0,0, 400,400, win2HDC, 0,0, SRCCOPY)

RELEASEHDC(win,winHDC)  :'Release the device context from the frontbuffer window
RELEASEHDC(win2,win2HDC) :'Release the device context from the backbuffer window

x=x+.05 :'Increment our sin and cos counter
WAIT 1 :'Wait FOR any system or window message to be processed

'End of our main animation loop.
'Keep looping until the user hits ESC or closes the program.
UNTIL run=0
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window/program is closed.
IF (@MESSAGE = @IDCHAR) & (@CODE = 0x1B) OR (@MESSAGE = @IDCLOSEWINDOW) THEN run = 0

'--This message is sent when the window is initially created.
IF @MESSAGE = @IDCREATE THEN CENTERWINDOW win

'--This message is sent when the user holds the left mouse button down
'  It drags the window around, as if we had been clicking on the title bar
IF @MESSAGE = @IDLBUTTONDN THEN SENDMESSAGE (win, 0xA1,2,0)

RETURN
ENDSUB
'End of window message handler subroutine.

ZeroDog

July 19, 2009, 12:21:57 AM #8 Last Edit: July 19, 2009, 02:31:59 AM by ZeroDog
'ZeroDog's Tips N Tricks
'Fun With Windows
'Part 8 - Drag 'N Drop Windows
'*******************************

'Creating windows that you can drop files into is easy to do with
'only a few commands.  We can modify any window to accept files
'by using the MODIFYEXSTYLE function with the WS_EX_ACCEPTFILES flag,
'or we can use the DragAcceptFiles function to setup the window for
'drag and drop operation. Either method has the same results. Then we
'simply respond to the WM_DROPFILES window message that our window
'is sent when a file is dropped.  To get the filename of the file
'that was dropped we can use the DragQueryFileA function. You can
'Get the x,y point at which the file was dropped in the window by
'using the DragQueryPoint function.

'First thing we need to do is define our window variable.
DEF win:WINDOW

'Now declare the API function calls (we can IMPORT from user32.dll)
DECLARE IMPORT, DragQueryFileA(hDrop:INT, iFile:UINT, lpStr:STRING, lpSize:INT),INT
'hDrop is the handle to the drop message. Found in the @WPARAM of the WM_DROPFILES @MESSAGE
' Ex: @WPARAM
'iFile is, ummm, I'm actually not sure, but we're setting it to 0
' Ex: 0
'lpStr is string that will be recieving the filename of the file dropped
' Ex: Filename
'lpSize is the size of the string that will be used to recieve the filename
' Ex: 10000
'Example useage of the function:
'DragQueryFileA(@WPARAM,0,Filename,10000)
DECLARE IMPORT, DragQueryPoint(hDrop:INT, lpPoint:POINT),INT
'hDrop is the handle to the drop message. Found in the @WPARAM of the WM_DROPFILES @MESSAGE
' Ex: @WPARAM
'lpPoint is a POINT type structure (x,y) that will be receiving the position where the file was dropped
' Ex: DropPoint
'Example useage of the function:
'DragQueryPoint(@WPARAM,DropPoint)
DECLARE IMPORT, DragAcceptFiles(hwnd:INT, fAccept:INT)
'hwnd is the handle of the window that you want to enable/disable dropping of files onto
' Ex: win.hwnd
'fAccept is the true/false flag.  If set to TRUE (1) then the dropping will be enabled
' Ex: 1
'Example useage of the function:
'DragAcceptFiles(win.hwnd,1)

'Set the constant that we will be using with the MODIFYEXSTYLE.
CONST WS_EX_ACCEPTFILES = 0x10
'Set the  window message constant that is sent when a file is dropped on the window.
CONST WM_DROPFILES = 0x233

'Define the POINT structure to recieve the coords of where the file is dropped in the window.
DEF DropPoint:POINT
'Define the ISTRING we will use to recieve the filename of the file that is dropped.
DEF Filename[10000]:ISTRING

'Now we open up our window.
OPENWINDOW win, 0,0,400,200,@CAPTION|@SIZE|@TOPMOST, 0, "ZDTNT-FWW-8: Drag 'N Drop Windows",&winproc

'Write some instructions in the window
move win,0,0
print win,"Drag and drop a file into the window."

'We now modify the window we opened to accept file dropping.
'We can do this two different ways.  We can Use the DragAcceptFiles
'function or modify the window style with MODIFYEXSTYLE.  In this
'example we have used the MODIFYEXSTYLE, but you can comment out the
'next line and uncomment the following line to use the DragAcceptFiles
'function.
MODIFYEXSTYLE win, WS_EX_ACCEPTFILES, 0
'DragAcceptFiles(win.hwnd,1)

'At this point, the program is all set up, so we will set our run variable
'to 1 and process window messages until the run variable is set to 0, when
'the program is closed.
run=1
WAITUNTIL run=0
CLOSEWINDOW win
END
'Main program end.


'This is our window message handler subroutine.
SUB winproc
'--This message is sent when the window is initially created.
IF @MESSAGE = @IDCREATE THEN CENTERWINDOW win

'--This message is sent when the window/program is closed.
IF @MESSAGE = @IDCLOSEWINDOW THEN run=0

'--This message is sent when a file is dropped on the window
IF @MESSAGE = WM_DROPFILES
'A file was dropped, so now we get the filename with DragQueryFileA
DragQueryFileA(@WPARAM,0,Filename,10000)
'Print the filename to the screen (with some spaces after it, just for looks)
MOVE win,0,50
PRINT win,Filename+SPACE$(200)
'We can get the x,y position of where the file was dropped with DragQueryPoint.
DragQueryPoint(@WPARAM,DropPoint)
MOVE win,0,100
'Print the x,y position of the file dropped to the window.
PRINT win,"Dropped at: "+STR$(DropPoint.x)+","+STR$(DropPoint.y)+SPACE$(200)
ENDIF
RETURN
ENDSUB
'End of window message handler subroutine.

Barney

Some good and interesting stuff in there. :)

Thanks for sharing.

Barney

Haim

Very interesting programs.
Thanks for sharing!

Haim

Dennisc

hey - thanks for sharing!

Dennis
Failure is only the opportunity to begin again more intelligently
www.denniscomninos.com

Ficko


pistol350

These examples are really impressive!
Thanks ZD!
Regards,

Peter B.

tbohon

Wow - neat stuff!

Thanks.
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

ZeroDog

 8)  Glad you all liked them.

I dont usually code with so many comments, but, since I was writing these specifically for the forums, I thought I'd make them as easy to follow as possible.


billhsln

Really neat samples.  Thanks for sharing.

Bill
When all else fails, get a bigger hammer.

Logman

Glad I checked into this section. These look like really great routines.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

hugh

Great Stuff, I learn something new every day javascript:void(0);
I will be looking at the animation window's  code a bit more.
some new things, i either missed, or, did not know about, i choose the latter.
Cheers