Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / ASM

MASM ImageFader

3.89/5 (15 votes)
19 Jan 2013CPOL1 min read 1   680  
An Image fader application. This hides information behinde an image and fades it out onMouseOver to display the content behind the image. Useful for hiding something, or as a stylish accessory.

Sample image

Introduction

It came across my mind that there must be a way to initially hide information on a screen from the user. But he should be able to access the info if he knows where to click or where to go with his mouse pointer. The idea came up to produce an image fader, which initially displays an image and fades out the image if you hover it.

Initial State

This shows how the fade image is shown initially, you don't see what's behind the image:

Fade Out State

Here you see how the control looks like if you hover the control. It will fade out the image after a short delay and show what's behind the control.

Fade Routine

Here, you have the fade routine that uses AlphaBlend (Win32) to blend the image over the background image taken initially:

ASM
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Procedure: SetTrans
; Parameter: nAlpha (new AlphaValue)
; Version: 1.0
; Date/Author: 10.09.2005, dave (juniorsoft)
; Description: This procedure draws the backround together with the
; new bitmap with a given alphavalue to get a 
; transparent effect.
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SetTrans proc nAlpha:BYTE
local hdc:HDC 
local hMemDC:HDC 
local hMemDC2:HDC 
local hMemDC3:HDC 
local hBmpTmp:HBITMAP
local bf:BLENDFUNCTION

;invoke GetWindowDC,hWndBitmap
invoke GetDC,hWndBitmap
mov hdc, eax
invoke CreateCompatibleDC,hdc 
mov hMemDC,eax
invoke SelectObject,hMemDC,hBmpBack
;Create copy of Bitmap
invoke CreateCompatibleDC,hdc 
mov hMemDC2,eax
invoke CreateCompatibleBitmap,hdc,nPicWidth,nPicHeight
mov hBmpTmp, eax
invoke SelectObject,hMemDC2,hBmpTmp
invoke BitBlt,hMemDC2,0,0,nPicWidth,nPicHeight,hMemDC,0,0,SRCCOPY

;Alpha blending bitmap over background
mov bf.AlphaFormat, 0
mov bf.BlendFlags, 0
mov bf.BlendOp, AC_SRC_OVER
mov al,nAlpha
mov bf.SourceConstantAlpha, al
invoke CreateCompatibleDC,hdc 
mov hMemDC3,eax
invoke SelectObject,hMemDC3,hBitmap

;Call the AlphaBlend Func direct cuz "invoke" Macro 
;doesn't work right (addr bf)
push bf
push nPicHeight
push nPicWidth
push 0
push 0
push hMemDC3
push nPicHeight
push nPicWidth
push 0
push 0
push hMemDC2
call AlphaBlend 
;invoke AlphaBlend,hMemDC2,0,0,nPicWidth,nPicHeight,
;       addr hMemDC3,0,0,nPicWidth,nPicHeight,addr bf

;Copy new bitmap back to Ctrl
invoke BitBlt,hdc,0,0,nPicWidth,nPicHeight,hMemDC2,0,0,SRCCOPY

;Cleanup
invoke DeleteDC,hMemDC3
invoke DeleteDC,hMemDC2
invoke DeleteDC,hMemDC
invoke ReleaseDC,hWndBitmap,hdc
invoke DeleteObject,hBmpTmp
Ret
SetTrans EndP

This example also shows the use of several Win32 GDI functions in MASM. The reason why I use ASM is I think ASM is still a good language, and when it comes to "deep inside" debugging, a basic knowledge of ASM is necessary anyway. It's always good if you can have a look at a routine and you are not completely lost.

Tools Used

  • MASM v8.2
  • WinASM v5.1.1.0
  • OllyDbg v1.10

Feel free to use the source as you want, in any of your applications. If there are any questions or problems, don't hesitate to contact me.

Cheers kim (a.k.a. dave).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)