Click here to Skip to main content
Click here to Skip to main content

Transparent CSliderCtrl Class

By , 14 Nov 2002
 

sliders.jpg

Introduction

This is a MFC transparent slider class. As it uses CSliderCtrl as its base class it can be used as a replacement class for any CSliderCtrl object. Tic marks are fully supported as can be seen in the screen shot above. To use, add this class to your project. Add your sliders to your project normally, then change the CSliderCtrl type in your .h file to CMySliderControl and #include the MySliderControl.h file. Once compiled your sliders will be transparent and if you have auto tics set then these too will show.

Update

V2.0 Re-wrote parts of the class. Added the ability to redraw the controls when the backgrounds had changed. Added custom transparent channel and custom thumb with optional colours. The demo source has been updated to show the background changing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nic Wilson
Web Developer
Australia Australia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDoesn't work with CScrollViewmemberhoxsiew27 Mar '07 - 10:13 
I'm trying to create this on the fly in a CScrollView derived class as part of a SDI application. I suspect that the scroll view or it's base CWnd is eating the notify messages, but I can't be sure. Spy++ on your demo shows WM_NOTIFY messages sent to the parent (CDialog) but the same control instantiated as a child of the scroll view doesn't seem to be sending WM_NOTIFY messages to the parent at all. Without WM_NOTIFY, the parent can't send a reflected message.
 
Any clues?
GeneralTransparent Controlmemberarijit_datta268 Mar '07 - 20:41 
Hi,
I am trying to develop a transparent control.It is having a picture box with a png image and transparent background.I have set the background color of the control to transparent.
Now, it is working if I place it on a panel or a form. But, it is not working as a transparent control if I place it on another control such as list view or month calender.
If some body can help me,I would be thankful to him.
Generalpocket pc applicationmemberjstanna11 Feb '05 - 5:57 
While trying to apply this to a pocket pc program, it compiled right away without a problem, but the transparent background didn't work. As I worked with it, it would either have a white or grey background behind the slider, or if I commented out the line in the beginning of DrawChannel for ScreenToClient, that was the only time I got something of the original bitmap to show up, but it was the rectangle beginning immediately below the slider instead of where the slider was. I was able to get a custom thumb bitmap to show up fine, but only for a horizontal slider. Absolutely nothing works for a vertical slider. Any ideas?
 
Jennifer
GeneralProblem with the resizing windowsmemberAtif Mushtaq30 May '04 - 23:14 
in case of resizing window ,if initially some part of slider is hidden and then u resize it , it wont get drawn properly.
 
Unmanaged in a .NET world
GeneralRe: Problem with the resizing windowsmemberranxiaowu12 Dec '12 - 19:44 
i met the same problem,when i resize the slider control,transparent not work.
hello,every one.

GeneralProblem with the backgroundmemberOlaf Gramkow5 Nov '03 - 10:43 
When I start the demo and another program (for example the explorer) is in front of the demo program (the demo program will be displayed behind the other program), the background of the slider have a wrong background. The background behind the slider has the backgroud of the window, which was in the front of the demo program, when I startet the demo program. Any ideas how to fix this problem?
 
You want to test it?
Start the Windows Task-Manager and place the window in the middle of the Desktop. Now start the demo program, it will appear behind the Task-Manager. Then close the Task-Manager and you will see the problem.
 
Olaf
GeneralRe: Problem with the backgroundmembermalcolm clarke17 Jul '05 - 10:32 
was a solution for this found, I like the control minus this bug Frown | :(
GeneralChanging the background of the slidermemberManju16 Jul '03 - 21:17 
Hi,
 
I am not familiar with the things explained in the article. But I would like to have the slider having the same background color as its parent window. How can I do that? I don't want to change the thumb or anything. Can someone help me how to do this?
 
Thanks
Manju
GeneralDrawTransparent() questionmemberHarold Bamford21 Nov '02 - 8:37 
Nic,
 
Another dumb question for you. In DrawTransparent() you have the following:
	Invalidate();
	EnableWindow(FALSE);
	EnableWindow(TRUE);
I don't understand what is happening here. What do the pair of EnableWindow() calls do that Invalidate() doesn't? The documentation for EnableWindow() seems to indicate that this is for controlling mouse and keyboard input rather than drawing.
 
Thanks for the help!
GeneralRe: DrawTransparent() questionmemberNic Wilson21 Nov '02 - 11:13 

Hi,
 
This is the only way I have found to make sure a full set of paint messages are sent, mainly needed for the users who need to change backgrounds with transparent controls.
 
Nic

GeneralRe: DrawTransparent() questionmemberHarold Bamford22 Nov '02 - 8:05 
I have found that this works as advertised, but not well in my application.
 
I have another CSliderCtrl-derived slider that gets various parameters changed very rapidly. Using the
Invalidate();
EnableWindow(FALSE);
EnableWindow(TRUE);
sequence doesn't seem to "keep up". But I found that I get the same effect with this:
int min,max;
CSliderCtrl::GetRange(min,max);
CSliderCtrl::SetRange(min,max,TRUE);
This forces the update immediately, which is what I needed.
 
For your application, it probably doesn't matter. But it is quite a remarkable difference in mine. Very strange. It would appear that there are yet more hidden actions going on!
GeneralRe: DrawTransparent() questionmemberDavid Cole30 Jul '04 - 7:00 
Why don't you try Invalidate() followed by UpdateWindow()? UpdateWindow() forces a paint to happen right now synchronously which is probably a side effect of the SetRange TRUE param...

 
David Cole

AnswerRe: DrawTransparent() questionmemberdhclark3 Nov '10 - 8:11 
I've tried just about everyone's suggestions regarding this, and this is the only one I have found to work. I have one more suggestion, however. If the CMySliderControl object happens to be disabled, the background color is not updated. Try this:
if (bRepaint == TRUE) 
{
   Invalidate();
   BOOL bEnabled = IsWindowEnabled();
   if ( !bEnabled )
      EnableWindow();
   int min, max;
   CSliderCtrl::GetRange(min, max);
   CSliderCtrl::SetRange(min, max);
   if ( !bEnabled )
      EnableWindow(FALSE);
}
This works every time under all conditions (so far). Thanks to everyone who has contributed to this!
QuestionBetter way to know vertical from horizontal?memberHarold Bamford20 Nov '02 - 17:27 
In your code for DrawChannel(), you ask if there is a better way to determine if the control is vertical or horizontal. Try:
 
	// get channel orientation
	BOOL IsVertical = (TBS_VERT & GetStyle()) ? TRUE : FALSE;
Having said that, I have GOT to ask what this is all about:
	crect = lpcd->rc;
	if (IsVertical)
		crect.InflateRect(1, 0, 1, 0);
	else
		crect.InflateRect(0, 2, 0, 2);
	DrawEdge(pDC->m_hDC, &crect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
It looks like the goal of DrawChannel() is merely to capture the background bitmap and then finish the channel. Wouldn't it be easier to just capture the background in OnCustomDraw() and set
*pResult = CDRF_DODEFAULT;
What have I missed here?
 
And while I'm asking dumb questions, why do you use the DC for the parent window rather than the current window?
 
Thanks for your help on this. Obviously, I'm a GDI beginner...Confused | :confused:
AnswerRe: Better way to know vertical from horizontal?memberNic Wilson21 Nov '02 - 11:18 
Harold Bamford wrote:
In your code for DrawChannel(), you ask if there is a better way to determine if the control is vertical or horizontal. Try:
 
Great! I could not find that anywhere I will give it a try!
 
Harold Bamford wrote:
It looks like the goal of DrawChannel() is merely to capture the background bitmap and then finish the channel. Wouldn't it be easier to just capture the background in OnCustomDraw() and set *pResult = CDRF_DODEFAULT;
 
The class is unfinished and this is skeleton code for those who wish to alter the channel, some users do not know how to do this. I also like the type of channel this draws, I have expanded it further to fill the channel with various colours to the left, right or both sides of the slider sort of like a progress bar built in.
I will upload changes soon.
 

Harold Bamford wrote:
And while I'm asking dumb questions, why do you use the DC for the parent window rather than the current window?
 
The parent DC holds the background image we use to make the control transparent.
 
Nic

GeneralChanging the background imagememberAtlantys22 Sep '02 - 7:51 
I got the first post! Big Grin | :-D
 
This is a very cool class, (almost) exactly what I need.
It's "almost" because I've found one thing I wish it could do for me.
 
The background bitmap of my dialog changes while the program is running (so it's not just set once and that's it). So obviously, I would like the background image the slider saves to be changed. Unfortunetaly, the old background image, behind the sliderctrl, is still shown when I change to the new background.
 
GDI is NOT something I know well, but I tried to do this myself, but I couldn't. I tried deleting/clearing m_dcBk and m_bmpBk, but that didn't work, as well as some other funky things that created all kinds of ASSERT failures.
 
Any help would be greatly appreciated!
Thanks. Big Grin | :-D

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 15 Nov 2002
Article Copyright 2001 by Nic Wilson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid