krussellm

I have read several posts that seem to be the resolution to my problem. I have tried to use the code with no luck. I know I am missing something simple I hope. I am using VB with VS 2005.

I am trying to create an invisible control button or label that overlaps a selected area. I want this button or label to be clickable so as to enter another windows form. I am able to make this button or label transparent with no problems. I set the transparencykey to some non-used color, blah blah blah, that works fine. When I run the program, it makes a big square hole right through my application without just showing the controls behind the control. I want just the control to be transparent, clickable, and allow for the windows form and other controls to be viewed fully behind it.

I have tried using the following code in this link although I don't know if they are applicable:

http://dotnetrix.co.uk/custom.html

I have tried using the code in this link:

http://www.peterritchie.com/Hamlet/Articles/62.aspx

I am very new to this so any help would be greatly appreciated.

Thanks,

Kris



Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

Mick Doherty

Hi Kris,

Transparent areas as created by the forms TransparencyKey are not clickable. They are Transparent to the mouse as well as to the eye.

The Control in the first link is not a Transparent control, it is a Shaped control, the transparent areas have been cut away.

The second link does show a Transparent Control and should work fine, but it is missing one thing, OnPaintBackground needs to be overridden so as to stop the background from being painted.

Code Block

using System.Windows.Forms;

namespace Kris.Russell

{

class TransparentControl : Control

{

protected override CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= 0x20;

return cp;

}

}

protected override void OnPaintBackground(PaintEventArgs pevent)

{

//base.OnPaintBackground(pevent);

}

}

}





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

krussellm

Hi Mick,

Thanks for the reply.

I am able to click it for some reason. Maybe the difference is that I am using VB, not sure... don't have enough experience to know. I created a button and made it transparent over an area. I made it clickable to open another windows form. When I click the invisible area, it opens the windows form as I intended. Could you please repost that code above in VB I appreciate your help.

Kris





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

Mick Doherty

Hi Kris,

Not sure exactly what you're saying, do you want to click on it or not

Here's the code in VB.net

Code Block

Imports System.Windows.Forms

Namespace Kris.Russell

Class TransparentControl

Inherits Control

Protected Overrides ReadOnly Property CreateParams() As CreateParams

Get

Dim cp As CreateParams = MyBase.CreateParams

cp.ExStyle = cp.ExStyle Or &H20

Return cp

End Get

End Property

Protected Overrides Sub OnPaintBackground(ByVal pevent As PaintEventArgs)

'MyBase.OnPaintBackground(pevent)

End Sub

End Class

End Namespace

This creates a transparent control. It is fully Transparent to the eye (you can see right through it), but it is not Transparent to the Mouse (you can click on it, but not through it).

If you are trying to create a Control which can be clicked but which has Transparent areas that can't be clicked on, then you need to create a shaped control.





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

krussellm

Yes, I do want it clickable. I misunderstood your first comment about not being clickable.

I also would like that tranparent control to allow visibility to the windows form and controls behind it. I used the code that you gave me and it still creates a big sqaure hole in my form and I can see the background behind the entire windows form. The control is clickable. I must be missing something simple.

Just so you know, the purpose of this is to allow specific selected areas to be clickable for a touch-screen application. If you know a better way of doing this, please let me know.

Thanks,

Kris





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

Mick Doherty

Reset the forms TransparencyKey value.

As for other ways to do this, it would depend entirely upon the design of the app. This solution will allow you to see controls behind the Transparent Control, but it will not allow you to use the mouse/touchscreen to interact with them.

Why can't you just use the Click events of the controls themselves

I have never worked with touchscreens and so am unable to offer any advice on their use.





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

krussellm

I don't want to just use the Click events of the controls themselves because some of the items are text boxes with values. I have tried to make them clickable and they are not. Only the labels and buttons were clickable. So on the touch screen application, I don't want any area unclickable for a specific region so that users don't mistakenly click unclickable areas. So I want to create an invisible label or button that overlaps the entire area so that any part, no matter whether there is a label or text box, to be clickable. The touch screen just allows for someone to use their finger and click an area and it acts like a mouse is clicking that area. Same thing, nothing special with touch screen applications





Re: Windows Forms Designer Transparency - Controls on top of controls just makes a hole in the windows form

Mick Doherty

OK, I understand that fingers are much bigger than a stylus or mouse cursor. The Transparent Control would appear to be a simpe solution.

A TextBox does recieve a Click event unless it's disabled. Any disabled control will pass mouse messages to their Parent control. If you click in a non client are then a click event will not be raised.

You could create a control Inherited from NativeWindow to intercept WM_NCLBUTTONDOWN and WM_NCLBUTTONUP messages and send Click messages as appropriate. You would also need to handle the Click event for the form and re-route them to a control if the MousePosition is within the bounds + padding of that control. This however, would be far more complicated than a TransparentControl and so I would suggest that the TransparentControl is the best way to go.

Did resetting the forms TransparencyKey give the correct results