Autoscroll panel with graphics drawn on it - c#

I have a WinForms app, which has a large panel, which I am drawing on to. Basically, I implemented a scrollbar mechanism, which can imitate a zoom behavior on my drawing. Now I want to do something more.
I thought about adding a possibility to not zoom my drawing if it doesn't fit on the panel, but just use a scroll (vertical or horizontal) to see the full picture. So it looks like this:
Zoom is set to 1: (drawing is too big, there are supposed to be 4 circles)
I zoom back, and there they are, but really, really small:
And I want to have a possibility to just scroll the panel (f.e. in this situation horizontally), to see the whole drawing without having to shrink those drawings.
P.S I searched through SO, and I only found answers suggesting using PictureBox instead of Panel and I'm not sure I want to do that

Related

Winforms C#: problem with image transparency

I am making chess with c sharp and visual studio 2019. I managed to generate the chessboard with panels and I am planning to use PictureBox for the chess pieces. However, despite the chess images have a transparent background, it ignores the color of the panel and uses the default background color of the form. I suspect it is due to these lines.
...
Controls.Add(newPanel);
...
Controls.Add(picturebox);
...
What it is doing is adding each newly created panel to the form and adding the picturebox (which is used for showing a chess piece) to the form. I tried adding picturebox to the panel but the location of the picturebox becomes relative to that panel. When I drag the picturebox around (at runtime), the picturebox is not visible as soon as it leaves the panel. I have seen someone implemented this successfully before but I cannot figure out how.
This is what the background color looks like when moving the picturebox around (I have methods to handle mouse up, mouse down and mouse move). Any help would be appreciated.
Drawing the shapes and putting them into a single control seems like the way to go. If I draw the grid and chess pieces instead of using controls, the background of the chess pieces show up correctly as transparent.

How to make a horizontal bar in a panel control when drawing on it?

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.

Transparency over multiple pictureBoxes

I have an array of picture boxes that are arranged in a square. I want to put a larger, mostly transparent picture box over the top. But when I do it covers the other picture boxes and just draws the background of the form.
Is there a way to get it to have all the other picture boxes show where it is transparent?
Transparency in WinForms isn't great. Some controls have transparency support, others don't. Some controls can be subclassed to enable this (by calling Control.SetStyle() with the SupportsTransparency flag). I believe this can be done with PictureBox.
However, transparency in all WinForms controls works by having the transparent control call its parent control to draw the background before the child control draws. This means that you cannot have two sibling controls and expect transparency on one to show through to the other. Sorry!
All that said, it would be possible to code your own workaround to support this. It would involve subclassing PictureBox and clever coding in the OnPaint override to locate sibling controls and manually trigger painting of them into in-memory bitmaps. Lots of gotchas with this approach.
Try WPF!
Here's a tip to get the desired result:
Create multiple copies of your top image.
Add each copy to the Controls of each picture box it should cover.
Adjust location of each copy according to the offset of each picture box to be covered.
So you will see every copy of your big image covering each picture box as if they were a single image.

How can I create a transparent System.Windows.Forms.Panel on top of two other already existing Panels then draw a line on my transparent panel?

I'm writing a form in C# and have several panels. I need to draw a line between two of the panels. I've found online several ways to go about this, the most promising appears to be to create a third panel, make it transparent, place it on top of my original panels and draw the line here.
I'm not able to get the panel to be transparent, even if I set its BackColor and ForeColor properties to transparent (in code or in design view of VS).
Any ideas on how to make the panel itself transparent (or not Visible) but have the line I draw on it still visible on top of everything else?
Thanks in advance.
No, it's transparent. See this by giving the form's BackgroundImage a value. You'll see it through the transparent panel. Of course, that's not the kind of transparency you want, you want stacking effects to work. There is no direct support for that.
If you want layers to work then don't use controls. Use the Paint event to draw. Now there's no problem, if you want transparency then just don't paint. Draw a line across an image simply by drawing the image first. This is also the rendering model of WPF.
You can actually do this pretty easily as your own UserControl. Here's a code example:
Drawing on top of controls inside a panel (C# WinForms)
This is similar to what you were originally attempting to do, only instead of drawing a line on top of a transparent panel, this code creates an irregularly-shaped user control (which happens to be in the irregular shape of a line).

Animation Effects in WinForms/C#

I'm pasting an image from the game im building.
the matrix of empty cells you see are made of PictureBox[][].
I wan't whenever I drop a coin to one of the columns... I want it to go down but the purple stuff will hide the falling coin and the gray color you see wont hide it.
How do I make this effect?
please notice that in each PictureBox control I have set the BG Image as you can see
Don't do it like that.
Create custom control. In custom control, override Paint, and then draw COIN sprite first, then draw mask over it. Be sure that you use double-buffered painting here.
It will work like a charm, trust me!
And, since you are (I gueess) building 5-in-a-row game here, your custom control will be able to paint occupied slots as well.
By designing custom control, you'll be able to hide all the animation and graphics stuff away from your main form.
I don't think it is possible like that. Controls in WinForms cannot be transparent, that is the problem
I would think in three directions:
Forgetting about controls and
painting everything OnPaint event of
the form. It is not too complicated
(well it would be more complicated
to detect some mouse events like you
did now, as you wouldn't know which
graybox is hit, but rather just
mouse coordinates)
Experimenting with coin as a form
(forms can be transparent)
Possibly using WPF with same logic
you did, as controls can be
transparent there
Controls in Windows Forms can be transparent. You need to set the TransparencyKey of the Form to some color that you never plan on using (many people seem to love/hate Magenta for this), and then set the BackgroundColor of each PictureBox to the same color. The result should be a transparent PictureBox with its Image drawn over it. I'm pretty sure this won't work with the BackgroundImage property, mind you- just the Image property.
One potentially unwanted side effect of this method is that you'll be able to see whatever's behind your form (the desktop, other application windows, etc.) through the transparent places in the PictureBox.

Categories