I have a Panel (PanelBase) with a TextBox. I need to draw and drag a rectangle over the textbox using doble buffer to eliminate the flicker. To do that I'm puting a transparent panel over de PanelBase and draw the rectangle. How can I clear the last graphics in the transparent panel?
There are many techniques, but what I would do is rather than try manually manage the drawing and clearing of the rectangle, I would actually create a control that has the rectangle drawn on it and then adjust the control position as you drag the mouse. This way Windows will handle the rendering and clearing for you.
Related
In my WPF application I have a control that acts like a zoom box. You can zoom and drag its content. In my case this content is a grid that contains an image and some UserControls. These UserControls have to be positioned ontop of the image to highlight some segments of that image.
Here is an example (this is not an actual use case):
My question is, how should I position those red rectangles (what is the better practise here)?
By replacing the grid with a canvas and use Canvas.SetLeft resp. Canvas.SetTop?
or
By manipulating the RenderTransform of those rectangles?
Note: The rectangles are interactive (they have to interact with the mouse and maybe some other input devices).
I have a panel and I need to draw a horizontal chart on it. But sometimes the chart can be too long for the panel, even the form has maximum size. So I want to make a horizontal bar on panel to enable the user to see the remaining part of the drawing that is out of the bounds.
Chart is something like this:
As you can see, the chart is out of the panel's bounds and form's too. I don't have any idea how can it be done, so I have no code to show. So how can I do it using a basic method?
Yes, the solution is pretty basic, as long as the size you want to draw won't go over 32k pixels width:
Put your Panel inside another one.
The outer Panel has AutoScroll=true
The inner one where you draw has the size of your drawing.
You need to draw in the Paint event, as you should anyway (!)
Now the outer Panel shows a horizontal scrollbar and the user can scroll right and left and see all parts of the drawing..
One alternative would be to add a dummy control that enforces the AutoScroll of your drawing Panel to work, but I find using two Panels the cleaner way to go..
Note: You should either use a PictureBox or at least a double-buffered Panel subclass to avoid flicker and tearing:
class DrawPanel : Panel
{
public DrawPanel()
{ DoubleBuffered = true; }
}
Update: Instead of a Panel, which is a Container control and not really meant to draw onto you can use a Picturebox or a Label (with Autosize=false); both have the DoubleBuffered property turned on out of the box and support drawing better than Panels do.
I would need a picturebox that contains a graphic for an actor. The actor is placed on PictureBoxes that contain a tileset graphic. The actor should be able to move freely which i already did, but the transparent .PNG Actor-Image has a control-colored background when I load it into the form.
Question:
How could I make the background of this Image transparent AND allow to be moved?
The problem with windowsforms is that its control does not have true transparency
The transparency of a control is based on the BackgroundColor or BackgroundImage of its parent and that is why your PictureBox is Control-Colored
Now if you want to make it true transparency you have some options
Draw everything yourself
You can use the power of gdi+ or Graphics to draw everything, but only the needed ones
Handle the draw of the control
This could be a tricky one, you can override the method OnPaint and OnPaintBackground of the picture box so it do not redraw its background (Control-Colored), the bad side of this is that it will have a lot of flickering and buggy background on movement
Use another techology
I'm not sure but if I'm not mistaken you could use wpf for this, altough it could change your entire project
I'm working on an image editor, there are some shape controls like rectangle, eclipse etc with drag drop feature on canvas and they are re-sizable(one corner is fixed, other three are re-sizable), attached a screen shot below:
I'd like to only resize the seleted corner, other three will be fixed, any ideas?
DaveRook Edit
In Photoshop, this is achieved by wrapping (under transforms)! This means re-drawing that corner un-proportionally to the rest of the image.
You will need to do a few things to achieve resizing of your rectangle.
a drag handle (something for the user to click and move).
to calculate the new height and width of your rectangle based upon the mouse position when the drag handle is being moved.
to update the height and width of the rectangle.
There is no shortcut here, but this SO question should be enough to get you started
I'm trying to write a very simple photo editor using C# 2008 or QT4.
How to make a resizeable rectangle selection tool like the photoshop did?
If you are talking about the "classic" "rubber band" type of selection rectangle, check out How to draw a rubber band rectangle or a focus rectangle in Visual C#.
WPF Code Example: http://www.codeproject.com/KB/WPF/wpfmarchingants.aspx?display=Print
This one is a bit more complicated because it involves image cropping, but if you scroll down to the selection rectangle, you can see the basic formulas for calculating the rectangle: http://69.10.233.10/KB/WPF/ImageCropper.aspx
Lastly, another one using GDI: http://codelog.blogial.com/2008/10/31/rubber-band-selection-rectangle-in-c/
If you provide more specifics, I'm sure we can help you out more.
Qt has a class for that:
http://doc.trolltech.com/4.5-snapshot/qrubberband.html
I don't know the specific calls, but the idea is this:
You want to draw a transparent rectangle with an opaque or dotted border. The rectangle appears when the mouse button is clicked. While the button is held, the dimension of the rectangle will change as the mouse moves, with the top-left point at the position where the button was clicked and the bottom-right following the mouse as it moves. Releasing the button causes the rectangle to fix its position over the selection area.
You should be able to figure out the particulars from a resource on the C# Drawing namespace.