Place WinForm On Bottom-Right - c#

How can I place a form at the bottom-right of the screen when it loads using C#?

try something on the lines of
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width,
workingArea.Bottom - Size.Height);
Hope it works well for you.

Form2 a = new Form2();
a.StartPosition = FormStartPosition.Manual;
a.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - a.Width,
Screen.PrimaryScreen.WorkingArea.Height - a.Height);

This worked for me; i just put this code listed below after my InitializeComponent();
public FormProgress()
{
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}

It's easy to try;
//Get screen resolution
Rectangle res = Screen.PrimaryScreen.Bounds;
// Calculate location (etc. 1366 Width - form size...)
this.Location = new Point(res.Width - Size.Width, res.Height - Size.Height);

In you form constructor put the following code:
StartPosition = FormStartPosition.Manual;
This will set the start position of the form to whatever you set as the value for the form's location (you can set this in the form designer).

Related

How to dark background when my form opens?

I am writing the app, and a part of it is open textbox. When the textbox is opening I want to dark background.
I have looked the solution and found it here:
Creating a dark background when a new form appears
But, it does not work for me correctly.
Here is my code:
private void App_Load(object sender, EventArgs e)
{
this.Text = "TestApp";
this.Size = new Size(350, 250);
this.BackColor = Color.DarkGray;
this.Location = new Point(50, 50);
this.MaximizeBox = false;
TextBox.BackColor = Color.WhiteSmoke;
TextBox.Multiline = true;
TextBox.Size = new Size(200, 90);
Button.Text = "Search";
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), this.ClientRectangle.Size);
double percent = 0.60;
Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
using (Brush brsh = new SolidBrush(darken))
{
G.FillRectangle(brsh, this.ClientRectangle);
}
}
using (Panel p = new Panel())
{
p.Location = new Point(0, 0);
p.Size = this.ClientRectangle.Size;
p.BackgroundImage = bmp;
this.Controls.Add(p);
p.BringToFront();
// display your dialog somehow:
Form frm = new Form();
frm.StartPosition = FormStartPosition.Manual;
frm.ShowDialog(this);
}
}
I receive this:
Maybe, someone can point me out where is my mistake?
EDIT: I have found the solution, the question was not clear enough.
When the textbox is opening I want to dark background.
So you want the textBox to be dark, not the complete form?
Almost always when you think you have to do some painting yourself, think again. It is seldom necessary do to paint. Only do this, if you don't have any standard options.
Just set Property BackGround of the text box. Use visual studio designer to do this.
If you don't want to do this using the designer, do this in the constructor after InitializeComponent:
public MyForm()
{
InitializeComponent();
// text box dark background:
this.textBox1.BackColor = Color.Black;
}
If you want the complete form to be black, again use visual studio designer, or add:
InitializeComponent();
this.BackColor = Color.Black;

C# - What is the best way to center a form vertical and fixed horizontal programmatically?

I have a windows form and i want to set it in the center vertically and to a fixed point horizontally.
You can see in the code what i have tried unsuccessfully.
What is the best way to solve this?
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
//this.Location = new Point(100, 100);
this.Location = new Point(this.CenterToScreen, 0);
//this.CenterToScreen();
//var rec = Screen.PrimaryScreen.WorkingArea;
//int margain = 10;
//this.Location = new Point(rec.Width - (this.Width + margain), rec.Height - (this.Height + margain));
}

Making a pictureBox inside of a groupBox transparent

So i've got a groupBox with a picture 1 inside of it. So i need to add smaller picture 2 over the picture 1, but picture 2's background mustn't overlap picture 1.
I've tried this approach:
InitializeComponent();
groupBox.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
and this
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
but neither work.
Thanks in advance.
You need to set the parent control of the picture box. The transparent background that is displayed is that of the parent control.
InitializeComponent();
groupBox.Controls.Add(pictureBox2);
pictureBox2.Parent = pictureBox1;
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
In order to accomplish this try the following:
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
pictureBox1.SendToBack();
pictureBox2.BringToFront();
used .SendToBack() for the one on the back and .BringToFront() for the one on the front.

WPF - Set dialog window position relative to user control

I need help with Setting dialog window position relative to User control.
I want to show my window in the middle user control when a window starts.
How can I find the left and tom location of my User Control?
I use this code in my app but does not work correctly in WPF.
Thank you for help.
private void PossitionWindow(object sender, RoutedEventArgs e)
{
Window wind = new Window();
var location = this.PointToScreen(new Point(0, 0));
wind.Left = location.X;
wind.Top = location.Y - wind.Height;
location.X = wind.Top + (wind.Height - this.ActualHeight) / 2;
location.Y = wind.Left + (wind.Width - this.ActualWidth) / 2;
}
Here is a small example.
// Get absolute location on screen of upper left corner of the UserControl
Point locationFromScreen = userControl1.PointToScreen(new Point(0, 0));
// Transform screen point to WPF device independent point
PresentationSource source = PresentationSource.FromVisual(this);
Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
// Get Focus
Point focus = new Point();
focus.X = targetPoints.X + (userControl1.Width / 2.0);
focus.Y = targetPoints.Y + (userControl1.Height / 2.0);
// Set coordinates
Window window = new Window();
window.Width = 300;
window.Height = 300;
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Top = focus.Y - (window.Width / 2.0);
window.Left = focus.X - (window.Height / 2.0);
window.ShowDialog();
Preview

Capture Screen But Not Current Form In C#?

I want to capture the screen but without the current form, I tried to minimize -> capture -> maximize, but I'm looking for a better solution if exists. This was my code::
int w = this.Width;
int h = this.Height;
Size sz = this.Size;
Point loc = this.Location;
this.WindowState = FormWindowState.Minimized;
System.Threading.Thread.Sleep(500);
using (Bitmap b = new Bitmap(w, h))
{
using (Graphics g = Graphics.FromImage(b))
{
g.CopyFromScreen(loc, new Point(0, 0), sz);
}
b.Save(Environment.CurrentDirectory + "\\slides\\screen.jpeg");
this.BackgroundImage = System.Drawing.Image.FromFile(#"slides\screen.jpeg");
}
this.WindowState = FormWindowState.Maximized;
And I ask if there is a direct way to put the captured image as form background, without saving it.
Set the forms Opacity to 0.
This will make the form completely transparent and hence invisible.
Make the form invisible. Set the Visible property to `false.
This will hide the form without changing it's state, though on XP this won't make the window disappear straight away.
Try, although I did notice both yours and mine is marginally offset by the form border.
int w = this.Width;
int h = this.Height;
Size sz = this.Size;
Point loc = this.Location;
Hide();
System.Threading.Thread.Sleep(500);
using (Image b = new Bitmap(w, h))
{ using (Graphics g = Graphics.FromImage(b))
{
g.CopyFromScreen(loc, new Point(0, 0), sz); }
Image x = new Bitmap(b);
this.BackgroundImage = x;
}
Show();

Categories

Resources