C# Form popup location - c#

I am building a popup application to show some notification. I want to show it on the screen at the right-side bottom, in any screen resolution. The screen size is 422 x 217 (width x height). Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
/*System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer(); */
int num = 1;
this.Hide();
if (num == 1)
{
Form fm = new Form
{
Location = new Point(50, 60),
StartPosition = FormStartPosition.Manual,
ShowInTaskbar = false
};
fm.ShowDialog();
}
}
I want to make the popup as coming from bottom. Can anyone help me with this?

I recently got code for a 'toaster' popup which comes out from the bottom right corner of any screen. Here is part of the code-behind I used:
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right + 15, workingArea.Bottom - 10));
this.Left = corner.X - this.ActualWidth - 100;
this.Topmost = true;
this.Top = corner.Y - this.ActualHeight;
}));
Adjust the -100, +15 and -10 based on your 'popup' dimensions.

Related

Change form position at runtime changing screen number

I'm trying to change Form position to center after changing from a primary screen on a secondary screen
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
ff = !ff;
if(ff)
showOnScreen(1,this);
else
showOnScreen(0,this);
}
}
void showOnScreen(int screenNumber,Form frm)
{
Screen[] screens = Screen.AllScreens;
if (screenNumber >= 0 && screenNumber < screens.Length)
{
Location = screens[screenNumber].WorkingArea.Location;
this.Location = new Point((screens[screenNumber].Bounds.Size.Width / 2) - (this.Size.Width / 2), (screens[screenNumber].Bounds.Size.Height / 2) - (this.Size.Height / 2));
}
}
The form is moved in center of screen but only in my primary screen
You need to set StartPosition manual of form to set start position value in Location Property.
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(0, 0);
}
Also Take Reference From Here and also check my Another Reference
Now
Try to setting WindowStartUpLocation parameter as "manual" inside your showOnScreen method.

Grow windows form with button click

I want to grow a form when I click the button and it should be in the center of the screen .so I wrote following code snippet.
private void ord_Click(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new Size(1308,599);
this.Show();
}
But when I click the button window grows but half of the window can not see.Here is the picture of that.
GUI after growing
How can I get rid of this problem.?
Whats wrong with my code?
You have to compute both Size and Location:
private void ord_Click(object sender, EventArgs e) {
// Ensure that suggested form size doesn't exceed the screen width and height
this.Size = new System.Drawing.Size(
Screen.GetWorkingArea(this).Width >= 1308 ? 1308 : Screen.GetWorkingArea(this).Width,
Screen.GetWorkingArea(this).Height >= 599 ? 599 : Screen.GetWorkingArea(this).Height);
// locate the form in the center of the working area
this.Location = new System.Drawing.Point(
(Screen.GetWorkingArea(this).Width - Width) / 2,
(Screen.GetWorkingArea(this).Height - Height) / 2);
}
You can use the PrimaryScreen property of the Screen class.
//this.StartPosition = FormStartPosition.CenterScreen;
//this.Show();
Omit these lines you've written, except setting the Size property of the form:
private void ord_Click(object sender, EventArgs e)
{
this.Size = new Size(1308,599);
CenterForm();
}
Create a method named CenterForm() which will set a new location of the form. You can achieve this by calling this method in your button click event.
private void CenterForm()
{
int getWidth = Screen.PrimaryScreen.Bounds.Width;
int getHeight = Screen.PrimaryScreen.Bounds.Height;
int X = getWidth - this.Width;
int Y = getHeight - this.Height;
this.Location = new Point(X / 2, Y / 2);
}
Note: Always remember to anchor your controls when the size of the form has changed.

Capturing screen properly works only in certain taskbar status

I'm working on a program loading an image or capturing a screen area and convert them to simulate color blinds' vision.
The problem I've encountered now is that capturing algorithm doesn't work in different taskbar status that I wrote the code in.
The taskbar of my computer is set up to the top of the screen, and it's locked. I wrote the code in that status, and I realized that the program doesn't work on other computers with different taskbar status. I unlocked it or moved it to the other sides of the screen and it didn't work in the way as I wanted.
To capture a screen area, an opaque form is opened by the main one, and area to be captured is calculated from the position of mouse when its left button is pressed and released.
Also the position of mouse is shown by a label keeping following the cursor while it is hovering on the form. The label stops following the cursor while I'm pressing the left button.
I have no ides why it works like this.. Does any part of my code is affected by taskbar status? Appreciate in advance! The code is copied below.
public partial class Form_Capture_Rect : Form
{
//CaptureRectangular is the name of this form
private void CaptureRectangular_Load(object sender, EventArgs e)
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new System.Drawing.Size(width, height);
Message_Capture.Left = (width - Message_Capture.Width) / 2;
Message_Capture.Location = new System.Drawing.Point((width - Message_Capture.Width) / 2, (height - Message_Capture.Height) / 2);
}
//src: position the left button of mouse is pressed
//dest: position it is released
//leftTop: gets smaller value from src and dest, X and Y respectively
//width, height: size of the captured image
int srcX, srcY;
int destX, destY;
int leftTopX, leftTopY;
int width, height;
//gets position of mouse when its left button is pressed
private void Form_Capture_MouseDown(object sender, MouseEventArgs e)
{
srcX = Cursor.Position.X;
srcY = Cursor.Position.Y;
//this.WindowState = FormWindowState.Minimized;
}
private void Form_Capture_MouseUp(object sender, MouseEventArgs e)
{
//the captured image will be shown onto a picturebox 'Pic_Unchanged' of the main form
Form_Main mainForm = Application.OpenForms["Form_Main"] as Form_Main;
PictureBox Pic_Unchanged = mainForm.Controls["Pic_Unchanged"] as PictureBox;
//gets the position of mouse when it's released
destX = Cursor.Position.X;
destY = Cursor.Position.Y;
//when its width or height is zero, the function is terminated
if (srcX == destX || srcY == destY)
{
mainForm.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Minimized;
this.Close();
}
else
{
//calculate out these for values to get image
leftTopX = srcX < destX ? srcX : destX;
leftTopY = srcY < destY ? srcY : destY;
width = srcX < destX ? destX - srcX : srcX - destX;
height = srcY < destY ? destY - srcY : srcY - destY;
Graphics myGraphics = this.CreateGraphics();
Bitmap memoryImage = new Bitmap(width, height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
//copies image
mainForm.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Minimized;//whithout this, the form will be captured
memoryGraphics.CopyFromScreen(leftTopX, leftTopY, 0, 0, new Size(width, height));
//show the captured image onto the picturebox
Pic_Unchanged.Image = memoryImage;
this.Close();
}
}
//shows the position of mouse with a label following cursor
//MouseLoc is a label
private void Form_Capture_Rect_MouseMove(object sender, MouseEventArgs e)
{
MouseLoc.Location = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);
MouseLoc.Text = e.X + ", " + e.Y;
}
//Delete a text 'Message_Capture' which says "Capture Area" 2.5 seconds after the form is shown up
private void DeleteMessage_Tick(object sender, EventArgs e)
{
Message_Capture.Visible = false;
}
}

I'm trying to show the current image in the pictureBox and stretch it but it's not showing anything why?

In For1 i have this code:
private void timer1_Tick(object sender, EventArgs e)
{
try
{
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Load(file_array_satellite[file_indxs_satellite]);
file_indxs_satellite = file_indxs_satellite - 1;
if (file_indxs_satellite < 0)
{
file_indxs_satellite = file_array_satellite.Length - 1;
}
}
catch
{
timer1.Enabled = false;
}
}
private void satellitesToolStripMenuItem_Click(object sender, EventArgs e)
{
file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
if (file_array_satellite.Length > 0)
{
DateTime[] creationTimes8 = new DateTime[file_array_satellite.Length];
for (int i = 0; i < file_array_satellite.Length; i++)
creationTimes8[i] = new FileInfo(file_array_satellite[i]).CreationTime;
Array.Sort(creationTimes8, file_array_satellite);
file_indxs_satellite = 0;
file_indxs_satellite = file_array_satellite.Length - 1;
timer1.Enabled = true;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
this.pictureBox1.Size = new Size(500, 500);
pictureBox1.Location = new Point(this.Bounds.Width / 2,
this.Bounds.Height / 2);
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BringToFront();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
this.pictureBox1.Size = new Size(100, 100);
pictureBox1.Location = new Point(12,
27);
}
In the original the picturebox1 size is 100x100 and each image i stretch to fit in the pictureBox.
When it's 100x100 everything is ok i see the animation of each image in the pictureBox.
Now i did an event that when i enter with the mouse to the pictureBox area it should move to the center of the form resize to 500x500 stretch the images and show the same animation.
And when i leave the pictureBox area it should return to it's original size and location.
When i enter with the mouse to the pictureBox1 area the pictureBox just vanish i don't see it anywhere once i leave the pictureBox area i see it 100x100 in it's original place and size.
Why when i enter with the mouse to the pictureBox1 area it's vanish i don't see it in the center of the form on size 500x500 ?
file_array_satellite is string[] and file_indxs_satellite is int.
RainImage*.* are the files names on the hard disk after downloaded them.
The idea is not to convert/change the files sizes on the hard disk each time i enter or leave so i wanted that once i enter the pictureBox1 area it will stretch the current image in the pictureBox and show it . It's working when it's 100x100 but not on 500x500.
When you mouse over the PictureBox and move it to the center of the form, you are moving it out from under the mouse cursor. This causes the MouseLeave event to immediately trigger, which places it back under your mouse cursor again, which causes the MouseEnter event to trigger again, etc.
You can do something like this:
bool suppressMouseLeave;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
suppressMouseLeave = true;
this.pictureBox1.Size = new Size(500, 500);
pictureBox1.Location = new Point(this.Bounds.Width / 2,
this.Bounds.Height / 2);
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BringToFront();
//point the cursor to the new Position so that it's still kept on the pictureBox1
//This is important because it makes your idea acceptable.
//Otherwise you have to move your mouse onto your pictureBox and leave the
//mouse from it then to restore the pictureBox
Cursor.Position = PointToScreen(new Point(pictureBox1.Left + 250, pictureBox1.Top + 250));
suppressMouseLeave = false;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if(suppressMouseLeave) return;
this.pictureBox1.Size = new Size(100, 100);
pictureBox1.Location = new Point(12, 27);
}
I would venture a guess that this.Bounds.Width and this.Bounds.Height are not what you expect them to be, so the PictureBox isn't vanishing, you are just setting it to some location that is offscreen/off your form. Run Visual Studio in Debug mode and put a breakpoint around that line and see what this.Bounds is equal to. This may give you a clue as to the proper location you need to set.
How about in "in place" zoom like this?
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle rc = pictureBox1.Bounds;
rc.Inflate(200, 200);
pictureBox1.Bounds = rc;
pictureBox1.BringToFront();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Rectangle rc = pictureBox1.Bounds;
rc.Inflate(-200, -200);
pictureBox1.Bounds = rc;
}

Width of the form can not be less than 140 pixels. Why?

I created default Windows Forms Application project in Visual Studio 2012. When I run program then saw that width of form can not be less than 140 pixels. Why? And how to overcome this strange restriction?
I was looking for a solution and MinimumSize(0,0) didn't had any effect. Figured out, that MinimumSize set to (1,1) actually fixed the problem and after showing my form it was properly sized smaller than 140px.
Column click event on (ListView)_csvLv that should trigger a popup dialog:
var topAnchor = _csvLv.PointToScreen(new Point(
_csvLv
.Columns
.OfType<ColumnHeader>()
.Where(c => c.DisplayIndex < e.Column)
.Sum(c => c.Width),
0));
Left = topAnchor.X;
Top = topAnchor.Y;
MinimumSize = new Size(1,1);
ClientSize = new Size(_csvLv.Columns[e.Column].Width, 100);
ShowDialog();
Users wouldn't be able to use the window's minimize, maximize, and close buttons at that top. I don't believe you can change that behaviour with the Sizable FormBorderStyle. It's a usability thing.
If you remove the border, by setting it to None for example, you can set it to whatever you want programmatically by doing:
form.Width = [...];
You can resize further forms with border types: None, FixedToolWindow, and SizableToolWindow. The ToolWindows won't let you go below a certain amount as well, but None will let you do anything above 2px. You could set it to some value below that, without getting an exception, but it won't do anything.
Try this.
Autosize no
AutosizeMode growOnly
FormBorderStyle SizableToolWindow <== this one did it
I still can move the form and resize it (width) less tan 112
I never use formborders.. I always like to go with FormBorderstyle.None
To resize, you have to add some code.
Put a pictureBox, add a grip img in it and place it in the corner.
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(Form1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(Form1_MouseMove);
pictureBox1.MouseUp += new MouseEventHandler(Form1_MouseUp);
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
isHolding = false;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isHolding)
{
int diffX = this.Width - pictureBox1.Left;
int diffY = this.Height - pictureBox1.Top;
pictureBox1.Left += e.X - curX;
pictureBox1.Top += e.Y - curY;
this.Width = pictureBox1.Left + diffX;
this.Height = pictureBox1.Top + diffY;
}
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
isHolding = true;
curX = e.X;
curY = e.Y;
}
int curX = 0, curY = 0;
bool isHolding = false;

Categories