home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Translucen2067915292007.psc / Form1.frm < prev    next >
Text File  |  2007-05-29  |  10KB  |  295 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00000000&
  4.    ClientHeight    =   6150
  5.    ClientLeft      =   60
  6.    ClientTop       =   60
  7.    ClientWidth     =   8700
  8.    ControlBox      =   0   'False
  9.    ForeColor       =   &H8000000E&
  10.    Icon            =   "Form1.frx":0000
  11.    KeyPreview      =   -1  'True
  12.    Picture         =   "Form1.frx":000C
  13.    ScaleHeight     =   410
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   580
  16.    StartUpPosition =   2  'CenterScreen
  17.    Begin VB.HScrollBar OpacityScroll 
  18.       Height          =   375
  19.       LargeChange     =   10
  20.       Left            =   2520
  21.       Max             =   255
  22.       TabIndex        =   1
  23.       Top             =   4200
  24.       Width           =   3735
  25.    End
  26.    Begin VB.PictureBox FadeAnswerPictureBox 
  27.       AutoRedraw      =   -1  'True
  28.       BackColor       =   &H00A4E3EC&
  29.       BorderStyle     =   0  'None
  30.       BeginProperty Font 
  31.          Name            =   "Tahoma"
  32.          Size            =   8.25
  33.          Charset         =   0
  34.          Weight          =   700
  35.          Underline       =   0   'False
  36.          Italic          =   0   'False
  37.          Strikethrough   =   0   'False
  38.       EndProperty
  39.       Height          =   2415
  40.       Left            =   2640
  41.       ScaleHeight     =   161
  42.       ScaleMode       =   3  'Pixel
  43.       ScaleWidth      =   214
  44.       TabIndex        =   0
  45.       Top             =   1560
  46.       Visible         =   0   'False
  47.       Width           =   3210
  48.    End
  49. End
  50. Attribute VB_Name = "Form1"
  51. Attribute VB_GlobalNameSpace = False
  52. Attribute VB_Creatable = False
  53. Attribute VB_PredeclaredId = True
  54. Attribute VB_Exposed = False
  55. Option Explicit
  56.  
  57. Private Const DIB_RGB_COLORS = 0&
  58. Private Const BI_RGB = 0&
  59. Private Const AC_SRC_OVER = &H0
  60.  
  61. 'Private Const pixR As Integer = 3
  62. 'Private Const pixG As Integer = 2
  63. 'Private Const pixB As Integer = 1
  64.  
  65. Const DT_BOTTOM = &H8
  66. Const DT_CENTER = &H1
  67. Const DT_LEFT = &H0
  68. Const DT_RIGHT = &H2
  69. Const DT_TOP = &H0
  70. Const DT_VCENTER = &H4
  71. Const DT_WORDBREAK = &H10
  72.  
  73. Const StringToPrint = "Hello There"
  74.  
  75. Private Type BitmapInfoHEADER '40 bytes
  76.     biSize As Long
  77.     biWidth As Long
  78.     biHeight As Long
  79.     biPlanes As Integer
  80.     biBitCount As Integer
  81.     biCompression As Long
  82.     biSizeImage As Long
  83.     biXPelsPerMeter As Long
  84.     biYPelsPerMeter As Long
  85.     biClrUsed As Long
  86.     biClrImportant As Long
  87. End Type
  88.  
  89. Private Type RGBQUAD
  90.     rgbBlue As Byte
  91.     rgbGreen As Byte
  92.     rgbRed As Byte
  93.     rgbReserved As Byte
  94. End Type
  95.  
  96. Private Type BitmapInfo
  97.     Header As BitmapInfoHEADER
  98.     Colors As RGBQUAD
  99. End Type
  100.  
  101. Dim Pixels() As Byte
  102. Dim BackgroundBitmap As BitmapInfo
  103.  
  104. Dim BF            As BlendFunction
  105. Dim lBF           As Long
  106. Dim ThisRectangle As RECT
  107. Dim Str           As String
  108. Dim BackGroundDC  As Long
  109. Dim iBitmap       As Long
  110.  
  111.  
  112. Private Type RECT
  113.     Left As Long
  114.     Top As Long
  115.     Right As Long
  116.     Bottom As Long
  117. End Type
  118.  
  119. Private Type BlendFunction ' This structure holds the arguments required by Alphablend function to work
  120.   BlendOp As Byte
  121.   BlendFlags As Byte
  122.   SourceConstantAlpha As Byte
  123.   AlphaFormat As Byte
  124. End Type
  125.  
  126. Private Declare Function AlphaBlend Lib "msimg32.dll" (ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal BLENDFUNCT As Long) As Long
  127. Private Declare Sub RtlMoveMemory Lib "kernel32.dll" (Destination As Any, Source As Any, ByVal Length As Long) 'Conver to long
  128. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
  129. Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  130. Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
  131. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  132. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  133. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitmapInfo, ByVal wUsage As Long) As Long
  134. Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitmapInfo, ByVal wUsage As Long) As Long
  135.  
  136. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  137. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  138. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  139. Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
  140. Private Declare Function StretchDIBits Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal wSrcWidth As Long, ByVal wSrcHeight As Long, lpBits As Any, lpBitsInfo As BitmapInfo, ByVal wUsage As Long, ByVal dwRop As Long) As Long
  141. Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BitmapInfo, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
  142. Sub CopyBackGroundIntoPictureBox()
  143.  
  144. Dim ThisWidth   As Integer
  145. Dim ThisHeight  As Integer
  146. Dim XCoord      As Integer
  147. Dim YCoord      As Integer
  148.  
  149. XCoord = FadeAnswerPictureBox.Left
  150. YCoord = FadeAnswerPictureBox.Top
  151.  
  152. ThisWidth = FadeAnswerPictureBox.ScaleWidth
  153. ThisHeight = FadeAnswerPictureBox.ScaleHeight
  154.  
  155. FadeAnswerPictureBox.Visible = False
  156. BitBlt FadeAnswerPictureBox.hdc, 0, 0, ThisWidth, ThisHeight, Form1.hdc, XCoord, YCoord, vbSrcCopy 'The part of form1 behind the picturebox
  157. FadeAnswerPictureBox.Visible = True
  158.  
  159. End Sub
  160. Sub PrintTranslucentText(ByVal ThisText As String, ThisOpacity As Integer)
  161.  
  162. CopyBackgroundFromMemory
  163. FadeAnswerPictureBox.ForeColor = RGB(129, 0, 0)
  164. DrawText FadeAnswerPictureBox.hdc, StringToPrint, Len(StringToPrint), ThisRectangle, DT_WORDBREAK   ' Print text
  165. AlphaBlendWithBackground (ThisOpacity)
  166. FadeAnswerPictureBox.Refresh
  167.  
  168. End Sub
  169. Sub FadeIn()
  170.  
  171. Dim Opacity As Integer
  172.  
  173. For Opacity = 0 To 160
  174.     PrintTranslucentText StringToPrint, Opacity
  175.     DoEvents: Sleep (1) ' Wait
  176. Next Opacity
  177.  
  178. End Sub
  179. Sub FadeOut()
  180.  
  181. Dim Opacity As Integer
  182.  
  183. For Opacity = 160 To 0 Step -1
  184.     PrintTranslucentText StringToPrint, Opacity
  185.     DoEvents: Sleep (1) ' Wait
  186. Next Opacity
  187.  
  188. DoEvents: Sleep (2000)
  189.  
  190. End Sub
  191. Private Sub Form_Activate()
  192.  
  193. Dim Opacity As Integer
  194.  
  195. SetRect ThisRectangle, 0, 0, FadeAnswerPictureBox.ScaleWidth, FadeAnswerPictureBox.ScaleHeight ' Set coordinates
  196. FadeAnswerPictureBox.FontSize = 48
  197. Form1.Refresh
  198.  
  199. CopyBackGroundIntoPictureBox 'The part of form 1 behind the picture box goes into the picture box
  200. CopyBackgroundToMemory 'The picture box (part of form1) goes into memory to be used in Alphablending
  201.  
  202. Opacity = 127
  203. OpacityScroll.Value = 127
  204. PrintTranslucentText StringToPrint, Opacity
  205.  
  206. End Sub
  207. Sub AlphaBlendWithBackground(ByVal BlendValue As Integer)
  208.  
  209. Dim ThisWidth   As Integer
  210. Dim ThisHeight  As Integer
  211.  
  212. BF.BlendOp = AC_SRC_OVER
  213. BF.BlendFlags = 0
  214. BF.SourceConstantAlpha = 255 - BlendValue
  215. BF.AlphaFormat = 0
  216.     
  217. RtlMoveMemory lBF, BF, 4 'Convert the BLENDFUNCTION-structure to a Long
  218.  
  219. ThisWidth = FadeAnswerPictureBox.ScaleWidth
  220. ThisHeight = FadeAnswerPictureBox.ScaleHeight
  221.  
  222. AlphaBlend FadeAnswerPictureBox.hdc, 0, 0, ThisWidth, ThisHeight, BackGroundDC, 0, 0, ThisWidth, ThisHeight, lBF
  223.  
  224. End Sub
  225. Sub CopyBackgroundFromMemory()
  226.  
  227. SetDIBits FadeAnswerPictureBox.hdc, FadeAnswerPictureBox.Image, 0, FadeAnswerPictureBox.ScaleHeight, Pixels(1, 1, 1), BackgroundBitmap, DIB_RGB_COLORS
  228. FadeAnswerPictureBox.Picture = FadeAnswerPictureBox.Image
  229.     
  230. End Sub
  231. Sub CopyBackgroundToMemory()
  232.  
  233. Dim ThisWidth   As Integer
  234. Dim ThisHeight  As Integer
  235. Dim XCoord      As Integer
  236. Dim YCoord      As Integer
  237. Dim Bytes_per_scanLine As Integer
  238. Dim x, y As Integer
  239.  
  240. XCoord = FadeAnswerPictureBox.Left
  241. YCoord = FadeAnswerPictureBox.Top
  242.  
  243. ThisWidth = FadeAnswerPictureBox.ScaleWidth
  244. ThisHeight = FadeAnswerPictureBox.ScaleHeight
  245.  
  246. With BackgroundBitmap.Header ' Prepare the bitmap description.
  247.     .biSize = 40
  248.     .biWidth = ThisWidth
  249.     .biHeight = -ThisHeight 'Use negative height to scan top-down.
  250.     .biPlanes = 1
  251.     .biBitCount = 32
  252.     .biCompression = BI_RGB
  253.     Bytes_per_scanLine = ((((.biWidth * .biBitCount) + 31) \ 32) * 4)
  254.     .biSizeImage = Bytes_per_scanLine * Abs(.biHeight)
  255. End With
  256.  
  257. ReDim Pixels(1 To 4, 1 To FadeAnswerPictureBox.ScaleWidth, 1 To FadeAnswerPictureBox.ScaleHeight) 'Load the bitmap's data.
  258.  
  259. BackGroundDC = CreateCompatibleDC(0) 'Create a context
  260. iBitmap = CreateDIBSection(BackGroundDC, BackgroundBitmap, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) 'Create a blank picture on the BackBmp standards (W,H,bitdebth)
  261. SelectObject BackGroundDC, iBitmap 'Copy the picture into the context to make the context useable just like a picturebox
  262.  
  263. GetDIBits FadeAnswerPictureBox.hdc, FadeAnswerPictureBox.Image, 0, FadeAnswerPictureBox.ScaleHeight, Pixels(1, 1, 1), BackgroundBitmap, DIB_RGB_COLORS
  264. SetDIBits BackGroundDC, iBitmap, 0, FadeAnswerPictureBox.ScaleHeight, Pixels(1, 1, 1), BackgroundBitmap, DIB_RGB_COLORS
  265.  
  266. End Sub
  267.  
  268. Private Sub Form_Unload(Cancel As Integer)
  269.  
  270. DeleteObject iBitmap
  271. DeleteDC BackGroundDC
  272.   
  273. End Sub
  274.  
  275. Private Sub HScroll1_Change()
  276.  
  277. End Sub
  278.  
  279. Private Sub OpacityScroll_Change()
  280.  
  281. Dim Opacity As Integer
  282.  
  283. Opacity = OpacityScroll.Value
  284. PrintTranslucentText StringToPrint, Opacity
  285.  
  286. End Sub
  287.  
  288. Private Sub OpacityScroll_Scroll()
  289. Dim Opacity As Integer
  290.  
  291. Opacity = OpacityScroll.Value
  292. PrintTranslucentText StringToPrint, Opacity
  293.  
  294. End Sub
  295.