Lowering Background Image of a Panel in C# WinForms - c#

I am trying to build a piano and i have a panel that is designed to house the musical staffs as a background image of this panel.
I have tried to apply these as a Picture Box but this hindered a lot of my other features of this piano. These are the current settings that are related to this particular panel:
this.BorderStyle = BorderStyle.FixedSingle;
this.Size = new Size(750, 75)
this.Location = new Point(125, 75);
this.Anchor = AnchorStyles.None;
this.BackgroundImage = Image.FromFile("..\\..\\..\\Notes-Images\\Notes-Images\\staff2.bmp");
this.Visible = true;
I need to find a way how to actually have control on the location of where the background image is placed so as to lower it from the top left of the panel

Related

PictureBox background ignoring WPF UserControl

I want to create a Checkers Board with animation on tiles.
In order to do so I created a rectangle WPF UserControl to represent the tiles.
on top of the tiles I'm using a PictureBox with a picture of red/black piece.
when creating and adding the tiles and after that the PictureBoxes I can't see the PictureBoxes because they are behind the tiles.
(if I reverse the order of the creation and adding of the elements I see the pictures above the tiles but the transparent background of the PictureBoxes shows me the form background like it ignores the UserControl beneath it.
for better explanation I attached a photo (I set the form's background to green for better understanding)
also here are the code parts I think are relevant
the WPF UserControl (named UserControlCell) Xaml
<Grid>
<Rectangle Fill="White" HorizontalAlignment="Left" Height="50" Width="50"
VerticalAlignment="Top" Name="myRect"/>
</Grid>
Each tile is treated that way
ElementHost eh = new ElementHost();
UserControlCell square = new UserControlCell ;
square.Size = new Size(someWidth, someHeight);
eh.Size = new Size(someWidth, someHeight);
eh.Location = new System.Drawing.Point(xCoordinate,yCoordinate);
if ((i + j) % 2 == 0)
square.BackColor = System.Drawing.Color.Black;
else
square.BackColor = System.Drawing.Color.White;
eh.BackColor = Color.Transparent;
eh.Child = square;
Controls.Add(eh);
and for each PictureBox
PictureBox pb = new PictureBox();
pb.Image = relevantImage;
pb.Size = new Size(someWidth, someHeight);
pb.Location = new System.Drawing.Point(xCoordinate,yCoordinate);
pb.TabStop = false;
pb.Visible = true;
pb.BackColor = Color.Transparent;
pb.Refresh();
Controls.Add(pb);
what could be the problem that causes the picture box to think that it's background is the form instead of the tiles (or why does adding them in opposite order making me not seeing the PictureBoxes at all)

How to properly autoresize splitPanels and their elements

My form consists of one splitContainer with two horitzontal panels, several buttons on the top panel and charts on the bottom panel.
When the form loads, the top panel is cut and some of the elements are hidden/cut. Moreover if I resize the form, none of the elements or splitContainer resize.
How can properly do it?
I tried with the autoresize property in Form_Load()
//this.AutoSize = true;
//this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
These are the splitContainer properties
//
// splitContainer1
//
this.splitContainer1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.btnPlay);
this.splitContainer1.Panel1.Controls.Add(this.grpOptions);
this.splitContainer1.Panel1.Controls.Add(this.grpDisplay);
this.splitContainer1.Panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.splitContainer1_Panel1_Paint);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.splitContainer1.Size = new System.Drawing.Size(784, 561);
this.splitContainer1.SplitterDistance = 69;
this.splitContainer1.TabIndex = 0;
This is the screenshot:
I suggest you anchor the controls on the splitcontainer
I will recommend putting in a TableLayoutPanel on the top panel and put buttons in the cells. You can then set resize behaviour of rows and columns in TableLayoutPanel either percentage or absolute value. Ensure that Dock property for buttons is set to Fill. This is will ensure smooth and proper resizing of controls. You can go for similar approach for bottom panel as well.

Panel transparency over different panels

I am a developing a c# app that will be used in a touch computer. It has a secret combination to close it in case something goes wrong. TopRight, TopLeft, TopRight.
To achieve that, I created a class that add two panels to the form:
public enum Location
{
Top,
Bottom
};
public SecretCode(Form form, int boxSize, int resetTimer, Location location, bool debug)
{
timerReset.Interval = resetTimer;
timerReset.Tick += TimerReset_Tick;
Panel panelRight = new Panel();
if (debug) panelRight.BackColor = Color.Yellow;
panelRight.Size = new Size(boxSize, boxSize);
panelRight.Location = new Point(form.Width - boxSize, location == Location.Top ? 0 : form.Height - boxSize);
panelRight.Click += PanelTopRight_Click;
Panel panelLeft = new Panel();
if (debug) panelLeft.BackColor = Color.Red;
panelLeft.Size = new Size(boxSize, boxSize);
panelLeft.Location = new Point(0, location == Location.Top ? 0 : form.Height - boxSize);
panelLeft.Click += PanelTopLeft_Click;
form.Controls.Add(panelRight);
form.Controls.Add(panelLeft);
panelLeft.BringToFront();
panelRight.BringToFront();
}
On my main form, I have different panels (they are all fullscreen) that I show / hide. Every panel has its own background image.
Panel Intro
Panel Signup
When the user finishes reading the intro, the whole panel disappears and a the Signup Panel shows up.
Is there any easy way to ensure that the SecretCode panels would always be transparent or do I need to change its parent every time a show a new panel?

WinForms Checkbox button image alignment

I'm trying to make a toggle button inside C# WinForms application. Now I have managed to make the toggle button look and feel with the suggestion from my previous post.
Now the problem is, I'm not able to centre align the images from my ImageList properly on the button, so its showing some of the back colors on its edges. Please see the below images for clear view.
How can I get rid of this white edges?
Things tried so far:
FlatAppearance of the Button is set to Flat.
Tried with Transparent background color but that didn't work.
ImageAlign is set to MiddleCenter.
Code generated by WinForms designer
//
// checkBox1
//
this.checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
this.checkBox1.BackColor = System.Drawing.Color.White;
this.checkBox1.CausesValidation = false;
this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.BottomLeft;
this.checkBox1.Cursor = System.Windows.Forms.Cursors.Hand;
this.checkBox1.FlatAppearance.BorderSize = 0;
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.ForeColor = System.Drawing.Color.White;
this.checkBox1.ImageIndex = 0;
this.checkBox1.ImageList = this.imageList1;
this.checkBox1.Location = new System.Drawing.Point(88, 178);
this.checkBox1.Margin = new System.Windows.Forms.Padding(0);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(98, 62);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Sample Button";
this.checkBox1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged_1);
Update: I'v managed to get rid of these white edges by reducing button size 1px (both x, y) from the image size. Image size is: 99x63, Button size 98x62. But I'm not sure whether this is the correct way to do it.
This is simple. Choose therese settings:
checkBox1.FlatStyle = FlatStyle.Flat;
checkBox1.FlatAppearance.BorderSize = 0;
// make all four (!) BackColors transparent!
checkBox1.BackColor = System.Drawing.Color.Transparent;
checkBox1.FlatAppearance.CheckedBackColor = Color.Transparent;
checkBox1.FlatAppearance.MouseDownBackColor = Color.Transparent;
Note that with FlatStyle.Flat the checkboxbutton reserves 8 horizontal pixels, 6 at the left and 2 at the right edge and will cut off 8 pixels from your Image unless you enlarge it like so:
checkBox1.Size = new Size(imageList1.ImageSize.Width + 8, imageList1.ImageSize.Height);
Now all pixels are displayed, however the control is not visibly left aligned until you move it to the left by 6 pixels!
Looking at your example both issues are probably not important, though, but sometimes they are..

Transparency key messing up on transparent images

I have a C# WinForm that has a transparent key of Lime. I also set the background color of the form to Lime. This works great for things like buttons and almost anything you can think of. The form looks like it is not there and allows me to create a custom looking form.
I have a picture box with background of Transparent and the picture box image is has a drop shadow. When I run the application, the drop shadow is not transparent. The drow shadow has a background color of the forms background color (lime). It looks horrible.
How do I have a transparent form that also allows transparent images to be placed on it properly.
Programs such as SWTOR game launcher have this nice background drop shadow so I know it is possible.
Thank You!
You're looking for one of two things, either Windows Regions
OR
Layered Windows
Here is an excellent example for C#
Or a little example i put together:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath();
Region r;
PointF[] p = new PointF[9];
p[0] = new PointF(70, 0);
p[1] = new PointF(170, 0);
p[2] = new PointF(240, 70);
p[3] = new PointF(240, 170);
p[4] = new PointF(170, 240);
p[5] = new PointF(70, 240);
p[6] = new PointF(0, 170);
p[7] = new PointF(0, 70);
p[8] = new PointF(70, 0);
gp.AddPolygon(p);
r = new Region(gp);
this.Region = r;
gp.Dispose();
r.Dispose();
}
you will need to put this code into the form you want it to effect.

Categories

Resources