C# creating a form for given size - c#

I want to draw a rectangle on the screen. I guess the most appropriate way is use a form without boarder.
Form frm = new Form();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(GlobalPosX, GlobalPosY);
frm.Size = new Size(101, 30);
frm.BackColor = System.Drawing.Color.Yellow;
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frm.Show();
The created from is not as the given size. It's size is similar to the window having boarder. The displayed window is bit larger as I given and the position is also moved little bit upper and left.
Is there another way to achieve my goal?

Use the ClientSize property instead of Size:
frm.ClientSize = new Size(101,30);

If you want to draw rectangle on the screen, you can draw it directly: http://bytes.com/topic/c-sharp/answers/263740-draw-directly-screen

Drawing C# graphics without using Windows Forms
That like talks about making a border-less window. From there, just use the Graphics object to draw whatever you'd like

Related

StartPosition CenterScreen doesn't work after resizing the Form in Form.Load

In the Form Load event I'm adding some controls to the Form dynamically.
I use this code to resize the Form to adapt its Size to the its new content:
this.MaximumSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
It works fine but StartPosition = FormStartPosition.CenterScreen doesn't work as expected:
the Form center position is calculated based on the original size.
My suggestion is to resize the Form using it's PreferredSize property. When you have added new Controls to the Form and these Controls don't fit in client area, the PreferredSize is updated to reflect the new size needed to contain them all, but limited to the Form's MaximumSize you have set.
Then use the CenterToScreen() method to center the Form to the current Screen.
CenterToParent(), suggested by Hans Passant in comments, has the same effect if the Form is not parented to any other Form: i.e., if it's not shown using the .Show(this) method overload; otherwise, it's centered to the Owner Form.
Setting AutoSizeMode = AutoSizeMode.GrowAndShrink (or calling the SetAutoSizeMode() method) and then setting AutoSize = true can have unwanted results. The more visible is that you cannot resize your Form with normal means anymore. The area occupied by a StatusStrip may not be considered etc.
As a note, if your application is not DpiAware, the Screen measures and your Form's Size may be different from what is expected. In case it's not, read about it here:
High DPI support in Windows Forms
These notes about the Screen and VirtualScreen may also be useful:
So, add your controls to the Form (maybe in the Load handler) then:
var screen = Screen.FromHandle(this.Handle);
this.MaximumSize = new Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
this.ClientSize = this.PreferredSize;
this.CenterToScreen();

Dynamically created PictureBox Does not Show up in C#

I was trying to Load a pictureBox in run time. There is no run time error. But the PictureBox does not show up.
Later on I also added InitializeComponent(), but it did not help.
My Code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox picLoadingNew = new PictureBox();
picLoadingNew.BackColor = System.Drawing.Color.Transparent;
picLoadingNew.Image = global::QuiDip.Properties.Resources.ajax_loader_Long;
picLoadingNew.Location = new System.Drawing.Point(790, 760);
picLoadingNew.Name = "picLoadingNew";
picLoadingNew.Size = new System.Drawing.Size(142, 22);
picLoadingNew.Dock = DockStyle.Fill;
//InitializeComponent();
this.Controls.Add(picLoadingNew);
picLoadingNew.Show();
picLoadingNew.Visible = true;
}
You wont need .Show or .Visible lines as this would be the default anyway, but you do certainly need the this.Controls.Add(picLoadingNew) - as long as the form is going to be the parent and not a Panel or anything else. If the PictureBox is sitting on top of another control like a panel, you actually need: panel.Controls.Add(picLoadingNew) - and not your current this.Controls.Add(picLoadingNew) line.
What I would try first as I can't see anything too wrong with your code is, change the default location of your picture box to:
picLoadingNew.Location = new System.Drawing.Point(5, 5);
And remove the .Dock line.
This might be setting the PictureBox off screen (depending on the form's size?) - you haven't provided this so guessing what could be wrong. If the PictureBox is off screen, the new coordinates will force it to be top-left - so you should definitely see it then. Once you know it's working, you can then move it into position and set anchors later once you've proved the code is actually working.
You might find setting .SizeMode = StretchImage might be useful incase you're image is too large and again is "off screen". StretchImage will force the image to resize to fit nicely inside your PictureBox with the current .Size dimensions that you provided.
Something like:
PictureBox picLoadingNew = new PictureBox();
picLoadingNew.BackColor = System.Drawing.Color.Transparent;
picLoadingNew.Image = global::QuiDip.Properties.Resources.ajax_loader_Long;
picLoadingNew.Location = new System.Drawing.Point(5, 5);
picLoadingNew.Size = new System.Drawing.Size(142, 50);
picLoadingNew.SizeMode = PictureBoxSizeMode.StretchImage;
parent.Controls.Add(picLoadingNew);
The code above worked for me (using my own image of course!).
Hope that helps.
Your code should work but there is a little problem. If your image is exactly positioned at the same location of another image and its size is identical, you can't see it. It is located behind the current image on screen.
Remove the Show and Visible call but add a call to BringToFront
this.Controls.Add(picLoadingNew);
// picLoadingNew.Show();
// picLoadingNew.Visible = true;
picLoadingNew.BringToFront();
By the way, what is the point in giving a specific size to your new image if then you ask it to Fill the available space on its container?

c# windows form: How to match backgroundimage size to form size

I am trying to match form size to the form background image size. The code:
var newImage = PB_DEPTH.Image;
var newForm = new Form();
newForm.BackgroundImage = newImage;
newForm.Width = this.depthFrameDescription.Width;
newForm.Height = this.depthFrameDescription.Height;
newForm.Show();
Problem is new form still crops part of the image. I imagine that this is due to the top menu strip containing the close, minimize, maximize icons.
Is there any way to get the size of that strip instead of applying constant offset values?
Or better yet a way to properly resize a form to match backgroundimage dimensions.
Try setting newForm.ClientSize to the size of your background image.

How do I make my entire game window transparent?

I’m working on a little overlay for Diablo 3 (For personal use only!)
I want just to draw a Text string (We’ll see later for font) in the middle of the screen.
But with XNA I cannot find how to put background to transparent…
My code so far is :
GraphicsDevice.Clear(new Color(0, 0, 0, 255));
spriteBatch.Begin();
spriteBatch.DrawString(font, this.TestToShow, new Vector2(23, 23), Color.White);
spriteBatch.End();
So I only need 1 thing: make this black transparent!
You do not seem to understand what GraphicsDevice.Clear(Color) does.
XNA opens a Windows Window and draws with DirectX into it.
GraphicsDevice.Clear(Color) clears the buffer drawn with DirectX, but doesn't have anything to do with the window.
To make the window transparent, you have to modify the underlaying window.
To do that you have to first add references to System.WIndows.Forms and System.Drawing.
In the Constructor of your Game1 class, you do the following:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IntPtr hWnd = Window.Handle;
System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);
System.Windows.Forms.Form form = ctrl.FindForm();
form.TransparencyKey = System.Drawing.Color.Black;
}
Let's go through this line by line:
Well, the first two are auto generated and we don't care about these.
IntPtr hWnd = Window.Handle;
This line gets you the pointer to the underlaying Window that is registered in Windows.
System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);
This line gets the WindowsForms-Control in the given Window.
System.Windows.Forms.Form form = ctrl.FindForm();
This line gets you the Form to which the Control belongs to.
form.TransparencyKey = System.Drawing.Color.Black;
This last line sets the key-Color, that identifies one single Color-value to not be drawn at all. I used Black, but you could also choose CornflowerBlue.
This makes your window internally transparent for that Color. I suggest you should choose the same Color as your clear-Color.
Two things to note:
Best practice is to cache your Form so you can set the TransparencyKey from anywhere.
You can also make your Window borderless this way:
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Hope I could help.
Edit:
I just realized this was asked years ago and had no answer. So feel free to use this, if you stumble upon it.

PrimaryScreen.Bounds.Height is not the full screen

I have a windows form. I'm setting it's size using the following code. My desired behaviour is to have it take the full width and half the height of the primary display.
this.Location = new Point(0, 0);
this.WindowState = FormWindowState.Normal;
this.Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height / 2);
The problem is there is a space on the right form (it's not taking up the full screen). Does anyone have any suggestions as to why?
Thanks
Try use
Screen.PrimaryScreen.WorkingArea
Two ways to make it fullscreen:
// maximize the form
this.WindowState = FormWindowState.Maximized;
or
// hide the border
this.FormBorderStyle = FormBorderStyle.None;
You can try them together but have to notice the order, hide the border first, then set the window state to maximized.

Categories

Resources