Peter Ritchie Inc. Software Consulting Co. Home
      Downloads    General    .NET Topics    C++ Topics    C# Topics

Home
Products & Services
Articles/Blog
Downloads
Links
Members Only
Support
Contact
Login
Register
Transparent .NET control

Posted by on Saturday, September 10, 2005 (EST)

A transparent control, one that lets the parent (or other siblings above in Z-Order) draw behind it.

C#:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Magnifier.NET
{
 /// <summary>
 /// Summary description for TransparentControl.
 /// </summary>
 public class TransparentControl : System.Windows.Forms.UserControl
 {
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public TransparentControl()
  {
   SetStyle(ControlStyles.Opaque, true);
   UpdateStyles();
   // This call is required by the Windows.Forms Form Designer.
   InitializeComponent();

   // TODO: Add any initialization after the InitializeComponent call

  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Component Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   components = new System.ComponentModel.Container();
  }
  #endregion
 
  protected override CreateParams CreateParams
  {
   get
   {
    const int WS_EX_TRANSPARENT = 0x20;
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= WS_EX_TRANSPARENT;
    return cp;
   }
  }
 
  protected override void OnPaint(PaintEventArgs e)
  {
   // TODO:  Add TransparentControl.OnPaint implementation
   base.OnPaint (e);
  }
 }
}


VB:

Public Class TransparentControl
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()

 

        Me.SetStyle(ControlStyles.Opaque, True)
        Me.UpdateStyles()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region

    Private Const WS_EX_TRANSPARENT As Int16 = &H20
    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle() = cp.ExStyle() Or WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        ' TODO: add any drawing code, if needed.
        MyBase.OnPaint(e)
    End Sub
End Class

 


Average Rating:

Add Your Comment

Advertisement
Rent and Buy DVDs. No Late Fees. Click to join Today.

Ottawa, Ontario, CANADA Weather
  • Ottawa, Ontario, CANADA Weather :: 23C Showers in the vicinity
    23C Showers in the vicinity


    Last Refreshed 9/3/2010 6:55:44 PM

  •  
    Copyright © 2005, Peter A. Ritchie All rights reserved