Hai all,
I am dynamically creating button at runtime in a c#.net Windows Application, I want to draw a border for the dynamically created button how can i do it, i tried this
private void DrawBorder(Button bt)
{
Graphics g = bt.CreateGraphics();
ControlPaint.DrawBorder(g, bt.DisplayRectangle, Color.FromArgb(229, 227, 227), ButtonBorderStyle.Solid);
g.Dispose();
}
but it is not showing any error but i can't see any border
Please Help
If you want a button that draws differently, you should probably create a new class inheriting from Button, and in it you would override OnPaint and/or OnPaintBackground and implement your drawing logics, such as drawing a border.
The reason your current solution is not working probably depends on when you are calling your DrawBorder method. If your drawing code is not executed as part of handling an OnPaint event, then the graphics you draw will be drawn-over in the next paint.
But anyway, instead of drawing the border yourself, can't you set the Border proerpty on the Button object?
Related
This is not my control I downloaded it from this page :
Advanced TrackBar
Then I dragged it to form1 designer from the toolbox.
When the application is running and I use the trackBar at some point it's like I selected the control and it's showing the selected rectangle around it.
and if I select/use another control at runtime the rectangle around the trackBar still exist.
At the bottom of the OnPaint override method in the MACTrackBar.cs file, comment out these lines:
// Draws a focus rectangle
// if(this.Focused)
// ControlPaint.DrawFocusRectangle(e.Graphics,
// Rectangle.Inflate(this.ClientRectangle, -2, -2));
I have used grahics in a panel. At start of the program I draw some points in the panel and after that I want to draw lines connecting those points. Problem is when I press tab button the graphics created disappear (but this happens once in the program). Next problem is I want to clear the panel I used following code to clear panel:
Panel1.Invalidate();
But this only clears the lines but not those points that were initially created. Does anyone has a simple solution because I don't want to recreate the panel.
Technical Detail: to draw initial points in panel, paint event of panel1 is used:
Graphics gfx = e.CreateGraphics()
For lines, there is a seprate function that is called on button click and in that I used:
Graphics gfx = Panel1.CreateGraphics();
Another button that is used to clear panel has following code:
Panel1.invalidate();
but it only clears the line graphics, not those initial points.
I was making a mistake by creating initial points in the paint event of panel1. So everytime i call
Panel1.Invalidate();
it recalls that paint event and those points are redrawn and not cleared.
Do you save the points in some sort of collection and draw them in the Paint event?
Then you should empty the collection and then call the Invalidate
The panel is redrawn inside the paint event. It means that you have to draw everything inside this method, instead of accessing the graphics context directly.
This explains why everything you draw when you use Graphics gfx = Panel1.CreateGraphics(); is lost every time the control is redrawn, since during the paint event, you are only drawing the initial points, nothing more.
To briefly explain:
I have a win form written in C# that contains a panel.
The panel contains a shape container, through that I have programmatically added some oval and line shape controls from VisualBasic.PowerPacks to the panel.
The reason of using such shapes was I needed to perform some operations on their MouseHover and MouseClick events.
I have also drawn some graphics like strings and ellipse on the panel using Graphics in Paint(object sender, PaintEventArgs e) method of the panel.
The application has zoom in and zoom out buttons and whenever user clicks on them the size of shapes and graphics are supposed to be changed based on the scale.
To redraw graphics after scaling, I needed to clear the old ones before drawing new graphics in the new scale, otherwise they remained on the panel. So, I used Clear(Color color) method of the graphic to do so.
Now, the problem I have is upon using Clear(Color color), everything including shape controls gets disappeared. By my perception, I do not expect shapes to be disappeared because they should be treated as controls, unless I am missing something here. How can I avoid this issue? Any advice would be appreciated.
Solved the problem by invalidating "shape.Invalidate()" shape controls.
What causes this ? Other controls are shown fine ...
public CustomControl()
{
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
}
The customcontrol is placed in a TableLayoutPanel.
...................
Edit: For clarification:
Suppose you have a window-sized control with a small modal dialog form on top. When you move the dialog window, it's like your painting with the window on the control (the borders are painted on the control). The control doesn't repaint itself like other controls do on the same form, ie montcalendar or other custom controls. I can't seem to find the cause of this ?
Small detail of drawing artifact:
Hard to tell from the snippet. The standard mistake is to draw through Control.CreateGraphics() instead of the OnPaint() method. Won't work, Windows lets the OnPaint method run when parts of the control get uncovered. Which wipes out whatever you drew. Another failure mode is deriving from a control that's a wrapper for a native Window control. UserPaint is not supported for these type of controls, the native Windows code has to do the drawing.
It is clear from the screen shot, note how the text is staggered. That's because the OnPaint() override is using the e.ClipRectangle property to figure out where to draw. That value always changes when you slowly drag a window across your control, it only tells you what part of the control needs to be redrawn. It does not tell you where to draw. That has to be based on the control bounds, routinely the rectangle from (0,0) to (ClientSize.Width, ClientSize.Height).
Only ever use e.ClipRectangle to optimize the drawing. Like skipping an expensive drawing detail when it is outside of the clipping rectangle. It is otherwise a small one, Windows is already quite good at clipping automatically.
I just started on the .net compact framework. I want to draw a Sudoku field on the screen. So I put down a PictureBox and defined a method for the Paint event:
private void pictureBoxPlayfield_Paint(object sender, PaintEventArgs e)
{
// use e.Graphics to draw the grid, numbers and cursor
}
This works, but you can see as the grid is drawn. So my question is, what is the right/better way to create such a custom control? Is there maybe a way to enable double buffering?
There is no built-in support for double buffering in the Compact Framework. You can add it yourself, PictureBox already supports the Image property. Create a Bitmap in the constructor and assign it to Image. You don't need the Paint event anymore, the one provided by PictureBox already draws it to the screen.
Whenever the image needs to change, create a Graphics object with Graphics.FromImage(), passing the PB's Image and draw your stuff. Call the PB's Invalidate() method to tell it that it needs to redraw the image. If you still see flicker, override the PB's OnPaintBackground() method and do nothing.
The only other consideration is handling resizing, you'd need a larger or smaller Bitmap. Not so sure that would be necessary for a game.