Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# .NET Control Transparency Problem

C# .NET Control Transparency Problem

Scheduled Pinned Locked Moved C#
csharpgraphicshelptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hristiyan
    wrote on last edited by
    #1

    Hello there ! I'm developing a Windows appliciation that involves so called "Transparent Controls". As you would probably know, the only ( as far as i know ) way to achieve a fully transparent control in C# is to add the so called "Transparent style" to your control ("WS_EX_TRANSPARENT"). There are numerous articles how to achieve this and the method is ALMOST always one and the same:

    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp=base.CreateParams;
    cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
    return cp;
    }
    }
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
    //do not allow the background to be painted
    }

    And that works fine! ( well .... at least all say that ..... and all the articles say so ), but am i missing something or that method has SOME SERIOUS problems that nobody can see ? For example the Z-order. The following piece of code creates simply a transparent control that draws a line accross itself with a random color:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    namespace WindowsApplication16
    {
    public class HrisTranspControl : Control
    {
    private Pen _drawingPen;
    public HrisTranspControl()
    {
    Random rnd = new Random();
    this._drawingPen = new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(rnd.Next(0,255),rnd.Next(0,255), rnd.Next(0,255))),5);
    }
    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x00000020;
    return cp;
    }
    }
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
    // Do nothing
    }
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    e.Graphics.DrawLine(_drawingPen, new System.Drawing.Point(0, 0), new System.Drawing.Point(this.Width, this.Height));
    }
    }
    }

    And so ... when u put few "line controls" on top of each other the Z-ORDER goes to hell. You can't change it, its not corrent and it changes randomly each time u select a random "line control". I've tried A LOT of methods to fix that ( changing the z-order in background, repainting the actual background because i think its because the

    P 1 Reply Last reply
    0
    • H Hristiyan

      Hello there ! I'm developing a Windows appliciation that involves so called "Transparent Controls". As you would probably know, the only ( as far as i know ) way to achieve a fully transparent control in C# is to add the so called "Transparent style" to your control ("WS_EX_TRANSPARENT"). There are numerous articles how to achieve this and the method is ALMOST always one and the same:

      protected override CreateParams CreateParams
      {
      get
      {
      CreateParams cp=base.CreateParams;
      cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
      return cp;
      }
      }
      protected override void OnPaintBackground(PaintEventArgs pevent)
      {
      //do not allow the background to be painted
      }

      And that works fine! ( well .... at least all say that ..... and all the articles say so ), but am i missing something or that method has SOME SERIOUS problems that nobody can see ? For example the Z-order. The following piece of code creates simply a transparent control that draws a line accross itself with a random color:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Windows.Forms;
      using System.Drawing;
      namespace WindowsApplication16
      {
      public class HrisTranspControl : Control
      {
      private Pen _drawingPen;
      public HrisTranspControl()
      {
      Random rnd = new Random();
      this._drawingPen = new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(rnd.Next(0,255),rnd.Next(0,255), rnd.Next(0,255))),5);
      }
      protected override CreateParams CreateParams
      {
      get
      {
      CreateParams cp = base.CreateParams;
      cp.ExStyle |= 0x00000020;
      return cp;
      }
      }
      protected override void OnPaintBackground(PaintEventArgs pevent)
      {
      // Do nothing
      }
      protected override void OnPaint(PaintEventArgs e)
      {
      base.OnPaint(e);
      e.Graphics.DrawLine(_drawingPen, new System.Drawing.Point(0, 0), new System.Drawing.Point(this.Width, this.Height));
      }
      }
      }

      And so ... when u put few "line controls" on top of each other the Z-ORDER goes to hell. You can't change it, its not corrent and it changes randomly each time u select a random "line control". I've tried A LOT of methods to fix that ( changing the z-order in background, repainting the actual background because i think its because the

      P Offline
      P Offline
      Phil J Pearson
      wrote on last edited by
      #2

      I think your problem stems from a misunderstanding of the meaning of WS_EX_TRANSPARENT. It has nothing to do with visual transparency; it makes a window 'transparent' to mouse clicks. In other words, the click goes through to the window underneath. If you're trying to use the mouse to select such a window you are bound to get confusing results! You probably don't need that style at all.

      Phil


      The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups