For a side project, I am working on in my spare time since college, I needed to create a splash screen. Finally. Eleven years after version 1.0 and we are thinking splash screen. Anyway, the splash screen must contain a custom image as a background, a progress bar to display how the initialization is progressing and something that will show every step of execution. Steps completed must be stacked on top of current step. Needless to say, application is written entirely in C++ using MFC framework and said splash screen, naturally, must be part of the application and not a separate project.

I started by creating a simple dialog and put on progress bar and two static controls. If you built all that, it looked weird as MFC does not reposition controls relative to the dialog size but keeps them at static positions. So, my first task was to reposition every control to desired place. You want that to happen at exact moment that dialog window is resized or your users will actually see controls moving from one place to another. To do this, you need to handle WM_ON_SIZE message.

First, all that UNREFERENCED_PARAMETER stuff is just to notify compiler that we are not using specified variable. Method GetWindowRect returns rectangle that represents entire dialog window. We are calling SetBackground method and after that SetProgresBarAndLabel method. For dialog to reposition properly, we need to call OnSize method of base CDialog class.

SetBackground method is simple. First, we need to get rectangle that represents current window. We then load PNG image. As we want to set image to the center of the screen we must call CalculateOffset method that calculates needed offset left and top of the image. Next we detach PNG image into CBitmap class and paint it on dialog window.

With Background done, we are now seeing difficulties. CStatic controls have gray background and CProgressCtrl looks like something that was made in 90s. But first, we still need to move all controls to desire window position relative to background image. This is where SetProgressBarAndLabel method steps in.

Progress bar control gets sorted first, so we obtain it from the dialog using GetDlgItem, control’s ID and some casting. Next, we set progress bar range. Range can be totally custom set. So, if you like, you can set it from 3 to 29 for instance. Zero to 100 makes it easier to understand, so I am using that one. We position the window using MoveWindow with last parameter set to FALSE. If you set last parameter to TRUE, you will be left with nice gray rectangle at control’s original position. You need to RedrawWindow, so that progress bar control appears on the screen at get go.

Next, we move both static controls. Nothing fancy there. You may have noticed you cannot use SetTextColor method in your application. This method is overriden in CTransparentStatic class (more about that later), for the purpose of setting different than default color to CStatic control. I want status to have shade of green that can be seen on the background, so I set it to RGB(0, 120, 0).

Still, this only positioned controls. Progress bar still looks like it is from 90s and CStatic control still has that lovely gray background. I want my Progress bar to look much like it does in Windows 7 and according to World-wide web there is only one way to go about that. You must insert code below into stdafx.h file.

This, however causes a bit of a glitch. You see, normal progress bar works from 0 value onward. This is not true for Windows 7 style progress bar. If you want your progress bar to work properly, you have to start with value higher than 0 and end at value higher than 100. It is a known glitch and it could be it is fixed in newer MFC versions. In this one, it most certainly is not.

So, with progress bar looking a tad more 2010-ish, we are ready to sort out that pesky CStatic control background. To do this, we need to create our own class that inherits from CStatic and make dialog believe we are using CTransparentStatic control in dialog and not plain old CStatic. This class will be called CTransparentStatic, due to it’s main feature. Transparent background. In dialog class, we must override DoDataExchange method and do the following:

By doing this, we tell to our dialog that controls with IDs IDC_PROGRESSTEXT and IDC_PROGRESSTEXTSTATUS will use sepcified member variables which are of type CTransparentStatic. This leads us on the right path, but we did nothing yet. To create transparent background, we must handle three messages sent to our control: WM_ERASEBKGND, WM_CTLCOLOR_REFLECT and WM_SETTEXT.

Method OnEraseBkgnd is used to handle WM_ERASEBKGND message and is triggered when background of control needs to be erased due to a redraw.

We handle things differently the first time around and every next time, as the first time around, we must get background of our parent windows e.g. dialog and we store it into member CBitmap object m_Bmp. Everything else is the same. We load Bitmap and paint background with the exact part of dialog background where our control is positioned. This will result in smooth image.

Next, we handle message WM_CTLCOLOR_REFLECT in method CtlColor. This method setsĀ  background to transparent value and uses empty brush for paint.

Final magic happens in OnSetText method that is used to handle WM_SETTEXT message, where we invalidate our control, update it and redraw the entire thing.

Nice. Now we have a transparent CStatic control. This is all you need to do, if you are thinking of putting only one line of text in your static control. However, when we print multiple lines to our static control, we notice that result gets re-printed each and every time at different position, which leads to smudged, unreadable and ugly result.

To fix this, you need to handle another message WM_PAINT. This is done in OnPaint method.

Idea here is to obtain text rectangle from control before it is printed on screen (DrawText method with DT_CALCRECT flag set). Then we offset text to desired position and draw text back to our control. We must avoid calling CStatic::OnPaint method here, as that would corrupt our attempts to reposition our text. This done, we now have exactly what we wanted. A multiline transparent CStatic control which fills text from bottom to top.

Figuring out this solution took me quite few hours of what would be my spare time. I hope it helps any of you out there still coding in C++ and using MFC. Dialog class code and CTransparentStatic class code can be found at GitHub.