Advertisement
C_Volume2 Custom Controls/ Forms/ Menus #72251

Animated Glass Cube Effect using StretchBlt

This code animates a glass cube moving on a picture in real-time. Almost the same as that Windows 95 screen saver (the moving glass boll). I though this code can help make a screen-saver like that. (You have to e-mail me a copy of that screen-saver please)

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
'**************************************
'This code must be copied into the form
'**************************************
'
Option Explicit
Dim CompDC As Long, hBmp As Long, CompDCOrg As Long, hBmp2 As Long
Dim SourceHDC As Long, SourceBMP As Long, SourceBMP2 As Long
Dim SourceHDC2 As Long
Dim rtn As Long, xsize As Long, ysize As Long
Dim xbounce As Long, ybounce As Long
Dim aw As Integer, xdir As Integer, ydir As Integer, iloop As Integer
Dim StayInLoop As Boolean
Private Sub Form_Activate()
  Randomize
  'The x and y size of the picture in pixels for the API's
  xsize = Picture1.Width / Screen.TwipsPerPixelX
  ysize = Picture1.Height / Screen.TwipsPerPixelY
  'The aw (Alteration Width) of the glass deformation object
  aw = 20
  'xdir and ydir is the bounce directional variables
  xdir = (Rnd * 5) + 1
  ydir = (Rnd * 5) + 1
  'Make a copy of both picture's into memory DC's
  Call MakeCopyOfImgage
  'Make sure the display picture doesn't redraw itself
  Picture1.AutoRedraw = False
  'The next variable controls the animation loop
  StayInLoop = False
  'Copy the origanal image to the visible picture box
  rtn = BitBlt(Picture1.hdc, 0, 0, xsize, ysize, CompDCOrg, 0, 0, SRCCOPY)
  'xbounce and ybounce is the center-point of the glass object
  'making it aw will display it in the top left-hand corner of the picture box
  xbounce = aw: ybounce = aw
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  'Terminate the animation loop
  StayInLoop = False
  'Free the memory used for the DC's
  Call DeleteCopyOfImage
End Sub
Private Sub Command1_Click()
  StayInLoop = Not StayInLoop
  While StayInLoop
    'Reset the portion of the DC that was deformed
    Call ResetPortion(xbounce, ybounce, aw)
    'Do the movement
    xbounce = xbounce + xdir
    If xbounce > xsize - aw Then xdir = -(Rnd * 5) - 1
    ybounce = ybounce + ydir
    If ybounce > ysize - aw Then ydir = -(Rnd * 5) - 1
    If xbounce < aw Then xdir = (Rnd * 5) + 1
    If ybounce < aw Then ydir = (Rnd * 5) + 1
    'Do the deformation on the memory DC
    Call Stretch(xbounce, ybounce, aw)
    'Copy the memory DC to the visible picture box
    rtn = BitBlt(Picture1.hdc, 0, 0, xsize, ysize, CompDC, 0, 0, SRCCOPY)
    'Let windows do some other stuff (I WILL NOT RECOMEND TO REMOVE THE NEXT LINE)
    DoEvents
  Wend
End Sub
Sub Stretch(ByVal xpos As Long, ByVal ypos As Long, ByVal areawidth As Long)
  Dim Stretchit As Double, i As Double
  Dim rtn As Long
  'The next variable set's the percentage of deformation
  'You can change this variable to get some interesting effects
  Stretchit = 0.9
  For i = 2 To 0.1 Step -0.2
    rtn = StretchBlt(CompDC, _
            xpos - (areawidth * i), _
            ypos - (areawidth * i), _
            (areawidth * i) * 2, _
            (areawidth * i) * 2, _
          CompDC, _
            (xpos - (areawidth * Stretchit * i)), _
            (ypos - (areawidth * Stretchit * i)), _
            (areawidth * Stretchit * i) * 2, _
            (areawidth * Stretchit * i) * 2, _
          SRCCOPY)
  Next i
End Sub
Sub ResetPortion(ByVal xpos As Long, ByVal ypos As Long, ByVal areawidth As Long)
  Dim rtn As Long
  'This next line will reset the are on the DC that was deformed
  rtn = BitBlt(CompDC, _
          xpos - (areawidth * 2), _
          ypos - (areawidth * 2), _
          (areawidth) * 4, _
          (areawidth) * 4, _
          CompDCOrg, _
          xpos - (areawidth * 2), _
          ypos - (areawidth * 2), _
          SRCCOPY)
End Sub
Sub MakeCopyOfImgage()
  'Get the handel to the DC for the two picture boxes
  SourceHDC = Picture1.hdc
  SourceHDC2 = Picture2.hdc
  'Get the pictures
  SourceBMP = Picture1.Picture
  SourceBMP2 = Picture2.Picture
  'Create the to memory DC's
  CompDC = CreateCompatibleDC(SourceHDC)
  CompDCOrg = CreateCompatibleDC(SourceHDC)
  'Copy the pictures to these DC's
  hBmp = SelectObject(CompDC, SourceBMP)
  hBmp2 = SelectObject(CompDCOrg, SourceBMP2)
End Sub
Sub DeleteCopyOfImage()
  'Delete the memory DC's
  rtn = DeleteDC(CompDC)
  rtn = DeleteDC(CompDCOrg)
End Sub

Consider the following program
<BR><BR>
#include <iostream.h><BR>
#include <conio.h><BR>
<BR><BR>
void main()<BR>
{<BR>
 int n;<BR>
 cin>>n;<BR>
 const int p = n;<BR>
 p++;  <B>// C++ gives error that p is a const</B><BR>
 int a[p]; <B>// C++ gives error that p is not a const</B><BR>
}<BR>
Now how can c++ give an error for p being a constant in first commented line and also an error for p not being a constant in the second commented line.....logical huh? And the program compiled perfectly till the line<BR>
  const int p=n;<BR>
(This line is accepted).<BR><BR>
Anyone with a way out, plz write a message. And if u feel that this discovery is worth a credit, plz vote.

Upload
Původní komentáře (3)
Obnoveno z Wayback Machine