Form Fade from color to grayscale on quit

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
Hi,

I hope I'm posting this in the right forum, there are so many.

For my VB2005e app, when the user tried to quit, I'd like the main form to fade from color to grayscale, when the confirm msgbox appears "Are you sure you want to quit?"

Sort of like when you try and shut down your PC on WinXP. If user chooses "no" then the form needs to return to color.

Please can someone help me or point me to some code for this ?

Thanks very much.
 
You can experiment with this example using a ColorMatrix that modifies saturation from 1.0 to 0.0. As you see in the FormClosing event I "cheat" a lot; first taking a screenshot of the form, then removing all controls and titlebar, finally fades the saturation with the image as background.
VB.NET:
Private Sub frmDiv_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
  Me.SetStyle(ControlStyles.DoubleBuffer, True)
  Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  SendKeys.SendWait("%{PRTSC}")
  Dim bmp As Bitmap = DirectCast(Clipboard.GetDataObject.GetData(DataFormats.Bitmap), Bitmap)
  Me.Controls.Clear()
  Me.ControlBox = False
  Me.Text = ""
  For i As Single = 1 To 0 Step -0.05
    Me.BackgroundImage = saturation(bmp, i)
    Me.Refresh()
    Threading.Thread.Sleep(10)
  Next
  bmp.Dispose()
End Sub
colormatrix/saturation code needed too:
VB.NET:
Public Function saturation(ByVal img As Image, ByVal s As Single) As Image
  Const lumR As Single = 0.3086
  Const lumG As Single = 0.6094
  Const lumB As Single = 0.082
  Dim a As Single = (1.0 - s) * lumR + s
  Dim b As Single = (1.0 - s) * lumR
  Dim c As Single = (1.0 - s) * lumR
  Dim d As Single = (1.0 - s) * lumG
  Dim e As Single = (1.0 - s) * lumG + s
  Dim f As Single = (1.0 - s) * lumG
  Dim g As Single = (1.0 - s) * lumB
  Dim h As Single = (1.0 - s) * lumB
  Dim i As Single = (1.0 - s) * lumB + s
  Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
    {New Single() {a, b, c, 0, 0}, _
     New Single() {d, e, f, 0, 0}, _
     New Single() {g, h, i, 0, 0}, _
     New Single() {0, 0, 0, 1, 0}, _
     New Single() {0, 0, 0, 0, 1}})
  Return apply_cm(img, cm)
End Function
 
Public Function apply_cm(ByVal img As Image, ByVal cm As Drawing.Imaging.ColorMatrix) As Image
  'return new image with given ImageAttributes set
  Dim bm As Bitmap = New Bitmap(img.Width, img.Height)
  Dim g As Graphics = Graphics.FromImage(bm)
  Dim ia As New Drawing.Imaging.ImageAttributes
  ia.SetColorMatrix(cm)
  g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
  g.Dispose()
  Return bm
End Function
 
Wow, what a cheat. This code fades the form itself. What if I wanted to fade everything in the background except the modal form or msgbox? Like when you click the logout link on the menu of this site...
 
John, I would like to use this code in a mdi forms scenario.

When parent calls child, fade the parent.

When child is closed, show parent as normal in full color.

Can you please show me how this should be coded ?
 
Back
Top