Oct 05 2009

Transparent WindowsForms controls & drawing on top of ProgressBar

Category: ProgrammingMaciek Talaska @ 7:02 pm

No, I actually have not given up posting blog entries :) My inactivity was caused by a lot of things that happened recently. I am changing my job, but it is not actually what I wanted to write today about ;) Some things seem to be simple until you really try to do it. It was exactly the case of one of the questions on StackOverflow that I found. So, how do you create a progress bar with text on top of it? Well, it seems that the easiest way to do it, is just to derive from Progressbar control, override OnPaint and use Graphics to draw anything, right? Well… the idea itself works for other controls but unfortunately not for ProgressBar. It is really strange, but ProgressBar control seems to ‘overdraw’ somehow everything what should be placed on top of it. The second problem is that WindowsForms controls does not support transparency. In WPF it is simple, what could be simpler than adding one attribute with specified opacity?

But hey… there are no things ‘impossible’ for software developers, right? I tried different approaches, and step by step I was just getting closer and closer to the final solution. Just to make it short: my solution is to create:

  1. a Label control with transparent background
  2. a user control that will hold ProgressBar and a label control (on top of progress bar). This control will be used instead of standard progress bar control.

 

  1. Creating label control with transparent background is a must, because assigning Color.Transparent to BackColor property of Label control does not affect the way the control is rendered. What you really need to do is to create a control, that derives from Label control, and add some code to make this control’s background transparent:
    public class TransparentLabel : System.Windows.Forms.Label
    {
        public TransparentLabel()
        {
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
        }
    
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }
    }
  2. Second control is just a standard UserControl, with a ProgressBar (Dock=Fill) and a TransparentLabel control placed on top of ProgressBar. Code:

        public partial class ProgressBarWithTransparentLabel : UserControl
        {
            public ProgressBarWithTransparentLabel()
            {
                InitializeComponent();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                this.progressBar1.SendToBack();
                this.transparentLabel1.BringToFront();
                this.transparentLabel1.Text = this.progressBar1.Value.ToString();
                this.transparentLabel1.Invalidate();
           }
    
            public int Value
            {
                get { return this.progressBar1.Value; }
                set
                {
                    this.progressBar1.Value = value;
                }
            }
        }
     

And… that’s it. You may also want to download complete source (C#) and see if it works for you (I do hope so!). Please, mind that I was creating it with Visual Studio 2010 Beta, so you may need to create a project/solution for yourself, and just copy the .cs files and add them to your project. Happy coding!

Oh, you may wonder why do I constantly invoke SendToBack and BringToFront methods in OnPaint – well, this is done to avoid this ‘overdrawing’ of anything was is being placed on top of ProgressBar. I don’t know why ProgressBar works like this, but… nevermind it just works this way, and if you want to use it, you have to live with it.

And just to prove, that I am not cheating ;)

image

ProgressBarWithTransparentLabel (10)

Tags:


Dec 17 2008

Who am I?

Category: UncategorizedMaciek Talaska @ 4:31 am

My name is Maciek Talaska. I am nearly 30 year old male. I have spent most of my life  in the southern Poland.

My father bought me first computer (Commoder 64) when I was 12 . I wrote my first program (a shooting submarine drawn using ASCII) after half a year of almost non-stop gaming ;) I got it. I knew I wanted to become a computer programmer. And I am one today.

In high school I became interested in real-time calculated computer graphics. As computers then were not as fast as they are today, I got to learn Assembly language (for x86 – at that time I was the proud owner of 486 dx2 80MHz ;) I have created some cool graphics effects – starting from the most simple ones (different types of scrolls, wobblers and other). I started to learn C and later C++ (Pascal as a language was a bit awkward to me because of its assignment syntax; I found C/C++ sytanx more compact and convenient).

Studying was a great time for me. I learned a lot, and thanks to my lecturers and some of my friends broaden my programming horizons. I became interested in Java programming, but gave it up after couple of months (I have developed a few projects using Java: some desktop applications, and JSP based web app that I am the most proud of) – .net became the main technology of interest for me. I was the founder of Student Science Society on Computer Department I was studying. The main goal of the Student Society was to help to learn .net platform and broaden our knowledge concerning programming using .net (everything started arround 2003/2004, so there were not many student familiar with .net). After couple of months I was choosen by Microsoft Poland to be Student Consultant – I was doing exactly the same things, but I got support from Microsoft (we got some books, some MS Techonoly Specialist came to our department and gave lectures…). As a Student Consultant I took part in developing polish .net programmers portal (codeguru.pl).

While studying I started to work as a computer programmer (my dream finally became true ;) I have learned a lot, and realized that developing real software differs a lot from ‘just coding’. I started to value quality oriented approach in the field of programming (code maintenance, code quality: unit testing, test driven development, continuous integration…).

I have about 3 years of commercial c# programming experience (mostly desktop applications, c# 1.1 – c# 3.0). My main fields of interest are:

  • GUI design & programming (I’ve used a lot of GUI toolkits and libraries: Qt, MFC, GTK, WindowsForms, WPF – just to name a few).
  • real-time calculated graphics (I have written some graphics effects using DirectX, OpenGL, Managed DirectX. At the moment I am learning XNA).
  • concurrency & parallel programming – not only using ParallelFX but also CUDA
  • C# / .NET programming as general
  • recently I am more and more interested in functional programming (I am going to take a deeper look into F# and maybe Haskell or OCaml)

Working in IT companies I had a chance to learn a bit about Agile approach to software development. I must admint that this changed the way I think about managing IT projects. In my opion there are dozen of things in Agile / XP approach that any company may try out, and I am sure, that even if they do not decide to switch completely into Agile, such things as Daily StandUps will surely help develop better software.

I am eager to learn new things, and I think that I am fast learner.

I have been living in UK since the end of April 2009. I have spent first couple of months in London (working for one of IT companies located in North London) and in the end of October 2009 I have moved to Manchester area to work for one of the game companies located there. I hope that my knowledge and experience will be valuable for my company and that I will be able to add a significant value to the products.

Maciek Talaska

Tags: