I have a ToolStrip in a C# application that I set the background color to Transparent. This reveals the underlying form's color but unfortunately the ToolStrip border is still visible.
I've implemented a Custom Renderer and overridden the DrawBoarder method to not draw anything but that seems to apply to all of the contained buttons as well (i.e the menu on drop down buttons are also drawn without a border).
So I'm stuck. What's the best way to draw transparent the entire ToolStrip but leave the buttons alone?
I have tried just overriding the OnRenderToolStripBorder method and it seems it doesn't affect the buttons at all. Have you tried it like this?
public class TestStripRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
}
}
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
if( e.ToolStrip.GetType().Name != "MyCustomToolStrip" )
{
base.OnRenderToolStripBorder(e);
}
}
Since you are trying to make the toolstrip hidden but keep the buttons, I have to put this out there.
Do you even need the toolstrip?
It might be better if you just used buttons in the application without the seemingly unneeded toolstrip.
Related
How do I implement scrolling for my Custom Control? My control is fully custom drawn and its height is variable, and a part of the control contains a menu so if there are many items in the control, I'll need to be able to put scroll bars there. I've not really been able to find any clues on how to do this. I did see something about ScrollableControl, but I'm still not sure if that's what I need.
Also, how will my control know when it needs to show the scroll bars? Because my control is fully custom drawn so there's no real "controls" in there it's just a bunch of pixels that are drawn onto it so it's not like I can just set AutoScroll to true and I can't do that anyway because it's not the main part of the control that needs scrolling, it's a particular location on the control that will need to have the scrollbars.
If your custom control inherits from the Panel control, you just set the size of the content yourself in the custom control by this setting:
this.AutoScrollMinSize = New Size(yourWidth, yourHeight);
If your control's ClientSize.Height is greater than yourHeight, you won't get any scrollbars. If it's less, then you get a scrollbar.
In your paint method, add this to the beginning:
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X,
this.AutoScrollPosition.Y);
Now everything you paint gets automatically transformed to the scrolling coordinates.
You have two options.
The good new is that it is possible and both are not really hard.
The bad new is that for one option you will have to adapt all your drawing code:
First make your Control, I use a Panel, to have Autoscroll=true;
Then you need to add one dummy control, I use another Panel, maybe like this, far enough to the right and bottom to force the ScrollBars to show:
public Form1()
{
InitializeComponent();
Panel panelDummy = new Panel();
panelDummy.Size = new Size(1,1);
panelDummy.Location = new Point(yourMaxX,yourMaxY);
panel1.Controls.Add(panelDummy);
}
And then you need to adapt your drawing code. Here is how:
private void panel1_Paint(object sender, PaintEventArgs e)
{
int xx = panel1.HorizontalScroll.Value;
int yy = panel1.VerticalScroll.Value;
e.Graphics.FillRectangle(Brushes.Wheat, new Rectangle(11 - xx, 22 - yy, 22, 311));
e.Graphics.FillRectangle(Brushes.RosyBrown, new Rectangle(11 - xx, 280 - yy, 22, 3));
}
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1.Invalidate();
}
I have added an Invalidate to the Scroll event to avoid messed up painting results.
The other option is simpler: Make your control large enough to hold all your drawn controls and the put it inside a Panel with AutoScroll=true; this will delegate the whole scrolling business to the containing Panel.
you can use aPanel with AutoScroll = true. After that, if you put any controls inside it, as long as their size is larger than the size of panel, the panel will automatically show scroll bars.. The trick can be used for any custom control asa well, as long as you place it inside a AutoScroll panel, and size it as large as you need it to be.
I've partially solved this problem by creating a custom control that inherits from Control and that is completely drawn in OnPaint. My solution so far is to use ScrollBarRenderer to draw my scroll buttons, then I define a Rectangle for the scrollable area. I then create an Image to draw my scrollable content onto, and use TranslateTransform Scroll that Image into position before using DrawImage to draw into the Scrollable Content Rectangle that I created. It's showing promise, although I haven't gotten it completely working yet since I have to handle all of the MouseOver and Click events on my own.
I have a set of three buttons labeled 1, 2 and 3 for a touchscreen application interface.
When the user clicks one, it should be "selected" and have some kind of obvious change that shows this.
When using a flat button style I can do this with a border, however it seems this does not apply to Buttons with the default/3d style.
I've noticed the backcolor can be set, however this only seems to apply plain colors, which generally looks pretty ugly.
Is there a way I can add something like a border or emphasize the "selected" 3d-styled button in another way? Is there a better control for this purpose that works well with touch screen interfaces?
A button cannot indicate a "selected" state, it can only have the focus. Which is of no use in a touch screen app.
What you want is a CheckBox instead. Which also supports looking like a button, set its Appearance property to Button. You can further tinker with the way it looks by modifying the FlatStyle property. By setting it to Popup for example it acquires a 3D-look when checked.
Well, I was able to add an inset border using:
class BorderButton : Button
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(new Pen(Color.Black, 6), this.DisplayRectangle);
}
}
It seems however the paint event can't draw outside of the button's borders, so an outset one wouldn't be possible using this technique.
I've had the same problem and now always use a label instead. This way you can catch the on_click or mousedown events and apply background colours or images as if it was a button.
The code can generate rectangles (Rectangle rectangle) at runtime. The position of rectangles may change according to users' choices.
I want to add code in the method where it creates rectangles to make the rectangles clickable. And after user clicking the rectangle, there will be a new window to show content just like text.
You can use Contains method of the Rectangle object.
private Rectangle _myRectangle;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (this._myRectangle.Contains(e.Location))
{
}
}
Create a label control with the border property and transaparent background(so that it will look as rectangle) and add click event handler for each label you add. It will be good if you create your own Rectangle control by deriving from Label class or you can create your own control(Many other solutions).
I would consider handling the click event on the window itself (or whatever your "background" control is), getting its coordinates, and comparing those to the known locations/sizes of your rectangles.
I've made a control which contains few other controls like PictureBox, Label and TextBox. But I'm having two problems:
1. I tried to paint some things on top of my control and when I'm overriding OnPaint, it results that things I try to draw are under controls that my control contains. Areas on which I would like to draw intersect with controls in ways that are not easy to predict. I mean that it includes something drawn on controls inside as well as on base of control. Is there a simple way to draw something on top of all contents of my control? Setting ForeColor to Transparent isn't a solution that would help me much.
2. I have a problem with grabbing mouse click events when I place my control on a form and add click event handling. It only works when I click on an area not occupied by controls inside. I would like the whole control to react to clicks and other actions like it was one consistent control. How can I redirect/handle these clicks to make them work the way I want?
Thanks in advance for any tips
edit:
Thanks for answer, i hoped for something less complexed.
Finally i decided to do it from scratch, painting all necessary things directly on surface of my UserControl, TextBox shows up only when it is necessary. That solved all of my problems with drawing and events.
MTH
1) Overriding a control's OnPaint event allows you to draw onto that control. Whenever that control is painted, your drawing code will be executed. Then, and only then, will the child controls be drawn on top of that control, each of them with their own OnPaint events executing. Each control is its own window (in the sense of the Windows API), and therefore each is responsible for drawing its own surface. This explains why your graphics are being covered up by the child controls, and why they are only visible in the empty spaces where no child controls have been placed.
The upshot is that drawing on top of multiple controls inside a container is not well-supported. There are a couple of hacks you could try to help get around these limitations, but you may well be disappointed.
The first possible hack is to draw your graphics on the container control as you've already done, and then also draw on top of the child controls themselves. You'll need to override each child control's OnPaint event in order for this to work, however, which immediately presents a problem. A TextBox control (along with a ComboBox, ListView, TreeView, and a handful of other controls) doesn't do its drawing in an OnPaint event; it's drawn natively by the operating system. The possible workarounds for this are so potentially painful that you might as well forget about it and change your design.
The second possible hack is to add another control that sits on top of the container control and do your drawing on its surface. Perhaps the easiest way to do this is by creating something like a transparent panel using the WS_EX_TRANSPARENT extended window style:
public class TransparentPanel : Panel
{
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x00000020;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}
2) Again, it becomes relevant that each child control is its own window and handles its own mouse input events. That's why the control whose MouseClick event you've overridden is only detecting the clicks that occur directly on its surface. The other clicks get passed to the child controls, who just ignore them. Events in WinForms are not bubbled up the control hierarchy.
As before, one possible solution is to handle the MouseClick event (and any others you want) for both the container and child controls. If you decide to go this route, I think I would recommend consolidating your logic into a single routine and simply calling that routine from each control's event handler.
If you're looking for something more comprehensive, you might check out Broadcasting Events through a Control Hierarchy, although I haven't taken the time to read it.
Of course, you could also send the relevant mouse input messages to your parent container control from each child control using the SendMessage API function. But I'll leave that as an exercise for the reader because I feel like you asked a simple question expecting a simple answer.
I have several user drawn controls on a form, unfortunately when the form is shown the user drawn controls are showing the previous forms background rather than the current forms background.
The OnPaint event is very simple, and the OnBackgroundPaint event is empty...
Like this:
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.DrawImageUnscaled(_bmpImage, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//Leave empty...
}
How do I get the current background to be the transparency that is shown, rather than the background of the previous form?
You need to set the window style - here's a nice basic article.
You seem to be overriding the OnPaintBackground method not do anything about the background. Since you're leaving it empty, you probably shouldn't override it in the first place. What happens when you just let the default OnPaintBackground handler do its job? Shouldn't that automatically draw the current control's background properly?
P.S. I've never worked with custom painting .Net controls. I'm merely speculating in an attempt to help find a solution to your problem. Forgive me if what I'm suggesting is completely off...