Question Create Semi-Transparent rectangle over a checkbox? (Highlighter)

Swoop

Member
Joined
Dec 7, 2011
Messages
14
Programming Experience
Beginner
Does anyone know how to create a semi-transparent rectangle over a checkbox? Something that can be used in a mousehover event to highlight the checkbox and when the mouse is not over the checkbox it looks normal again. It would act like a menuitem that has the mouse over it.

Any advice would be appreciated and thanks!
 
Do you really need this semi-transparent rectangle? Can you not just change the appearance of the CheckBox control itself?

Also, do you really mean MouseHover, which occurs when the mouse pointer stops over the control, or do you actually mean MouseEnter and MouseLeave?
 
The mouse enter and mouse leave events would be great. I think the thrust is to discover what I need to do in order to make it happen. Should I be looking at altering the checkbox or leaving the checkbox as is and painting something on top of it that is translucent? What is the suggested methodology to create a highlighting rectangle that is larger than the content bounds of the checkbox?
 
It really depends exactly what you want. The bounds of the CheckBox control extend well beyond the actual box that you check and uncheck. Do you want this highlight to extend beyond that? Do you want the change to occur within a range of the box or the whole control?
 
Yeah, I think it would be useful to be able to draw a selection rectangle that acts like a highlighter. A rectangle that overlays a control and is a translucent color so you're looking at the control through the selection rectangle.

It would behave exactly like a MenuStrip item. If you have a menustrip close, take a look at the selection highlighting that occurs when over a menu item. Before you click on the menustrip item and simply hover over it, it highlights the item you're over with a translucent blue rectangle. That rectangle is much larger then the item.

How would you go about doing something like that?
 
I'm not sure that you understand exactly what the item or the control actually is. Try adding a CheckBox to a form and changing its BackColor to something unusual. That will show what is the CheckBox control.
 
JM, I have no idea why you're giving me that response. In fact, that response is so terrible you're got me laughing and smiling at the same time.

What I'm hoping for is answer based in GDI+ somewhere. Maybe creating a bitmap from the checkbox and then creating an alpha channel transparency from it. Something along those lines. Changing the background color of a checkbox???...I mean really...can't you do any better then that? I'd like some help here if you would be kind enough to provide it.
 
I didn't suggest that as a solution. I suggested it as a way for you to see what actually constitutes a CheckBox control. You said:
It would behave exactly like a MenuStrip item. If you have a menustrip close, take a look at the selection highlighting that occurs when over a menu item. Before you click on the menustrip item and simply hover over it, it highlights the item you're over with a translucent blue rectangle. That rectangle is much larger then the item.
The highlighted sentence is completely false. The highlighted portion is exactly the size of the item because it is the item. The highlight is bigger than the text on the item, but the text is not the item. Likewise, the actual box on a CheckBox control and the text on a CheckBox control are not the CheckBox control. You obviously don't understand how items on a MenuStrip work so there's every likelihood that you don't understand how a CheckBox works too. I was kind enough to try to provide some help in your gaining that understanding but your attitude has brought that kindness to an end.
 
Listen, I think we're getting off on the wrong foot here. The original question stands and I don't believe that we've broached an answer of any kind. The menu item was simply an example of what I'm looking to do as it's easy to see...a simple example.

The question still is how to duplicate a selection rectangle (sort-of-kind-of) like a menu item in a menu strip. How would you go about doing so? What's the procedure...


Edit:
One more thing...I just realized by your responses that you might not know how to do something like this. So don't feel bad if that's the case. Just say so and maybe someone else wouldn't mind chiming in. It would nice....
 
Last edited:
How would you go about doing something like that?
Handle the controls Paint event, or if you are creating a derived CheckBox class override the OnPaint method.
Along with MouseEnter and MouseLeave event you can detect state and Refresh the control for it to paint accordingly.
Filling a rectangle is done with Graphics.FillRectangle method. You can use any kind of Brush object, for example a SolidBrush with a custom transparent color.
Color structure provides transparency through the alpha channel, check for example out the FromARGB method.
 
Well Paszt, it's nice to see you supporting you co-moderator but to be honest, changing the backcolor won't accomplish a selection style rectangle over a control. So the reality is that the suggestion by JM was not what I was looking to do right out of the box (or rectangle :). In fact, that response was so non-conversational that it simply took me by surprise.

John's response was much better and it was actually close to what I ended up doing. So one out of two isn't bad, it was just a little late. That's the way the cookie crumbles sometimes!

Mark
 
While changing the backcolor can get me part of the way there, it certainly doesn't address drawing a border around the control in the OnPaint event of an inherited checkbox or the form it's currently on. It doesn't speak about lowering the opacity of the brush or color that I'm using.

Now this is becoming the conversation that could have happened but didn't. But I suppose that we can agree to disagree as people would say in a situation like this. Tell you what though, my feeling on this is just as important as yours and I still say that the original conversation did not broach the subject of GDi+, custom controls, sub overrrides, argb colors...nothing.

It was simply change the backcolor of a checkbox. But all things being equal, who cares?
 
Back
Top