opacity around the message - c#

Is it possible to make a poup screen with high opacity around the popup screen in winform? If yes, how?
How do I make the pop up message or GUI screen to be in the middle of the computer screen?
Please remember that I don't have any source code yet.
An example:

To show your Form at center of screen, use StartPosition property of form to CenterScreen.
this.StartPosition = FormStartPosition.CenterScreen;
Now, to grey rest of the portion of screen.
Take a new form name it frmBlur and set these properties.
this.BackColor = SystemColors.ControlDark;
this.FormBorderStyle = FormBorderStyle.None;
this.Opacity = 0.8;
this.ShowInTaskbar = false;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
Now, use the below code to display MessageBox or winform
private void button1_Click(object sender, EventArgs e)
{
using (frmBlur ob = new frmBlur())
{
ob.Show();
frmMessage f = new frmMessage();
f.TopMost = true;
f.ShowDialog();
}
}

Related

Windows Form Cannot be Displayed CenterScreen

I used this code for the secondary form to act / be like mdi, this is windows form C#
frmFontLoad fontLoad = new frmFontLoad();
fontLoad.TopLevel = false;
Controls.Add(fontLoad);
fontLoad.Show();
fontLoad.BringToFront();
but the result then the form was loaded is like this refer to screenshot:
Form
Code on Form Load:
private void frmLabelPNInput_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
Printer_SerialPort = new SerialPort(ConfigurationManager.AppSettings["Printer_com_port"]);
btnOk.Enabled = false;
}
how can i make this secondary form to be displayed at the center?
frmProgress progress = new frmProgress();
progress.Left = (this.ClientSize.Width - progress.Width) / 2;
progress.Top = (this.ClientSize.Height - progress.Height) / 2;
This solved my problem, thanks

Full screen Windows Form goes beyond screen dimensions

I have a WinForms app (.NET 4) that needs to be shown either full screen or maximized without borders.
Using the following code in the Form_Shown event:
#if (DEBUG)
var debug = true;
#else
var debug = false;
#endif
this.Text = "";
this.ControlBox = false;
this.ShowInTaskbar = true;
//this.TopMost = debug;
this.TopLevel = true;
this.FormBorderStyle = FormBorderStyle.None;
if (debug) { this.Bounds = Screen.FromControl(this).WorkingArea; }
else { this.WindowState = FormWindowState.Maximized; }
If you look closely at the screenshot below, the top and bottom areas are cut off by a few pixels. Also, if maximized, the window still does not cover the task bar.
Please note that I have only one monitor attached. No secondary displays.
Any suggestions on how to address the two issues above would be appreciated.
UPDATE: The code above seems to work fine with forms without a MenuStrip or StatusStrip.
Here is the code I use for fullscreen. I create a FullScreen property for my form and when I need, I set this.FullScreen = true;
private bool fullScreen = false;
[DefaultValue(false)]
public bool FullScreen
{
get
{
return fullScreen;
}
set
{
fullScreen = value;
if (value)
{
//this.SuspendLayout();
this.WindowState = FormWindowState.Normal;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
//this.ResumeLayout(true);
}
else
{
this.Activate();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
}

Form not in right position

I'd like to show an instance of a form class for a specific time. The form needs to be topmost and not steal focus. Here is my code:
public class mSplashForm : Form
{
public mSplashForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LightBlue;
this.Opacity = 0.92D;
this.ShowInTaskbar = false;
this.MinimumSize = new System.Drawing.Size(5, 5);
}
}
public static void mSplash(int time = 500)
{
mSplashForm SF = new mSplashForm();
Application.EnableVisualStyles();
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;
SetWindowPos(SF.Handle, HWND_TOPMOST, SF.Left, SF.Top, SF.Width, SF.Height, SWP_NOACTIVATE);
ShowWindow(SF.Handle, mEnumShowWindowCommands.ShowNoActivate);
Application.DoEvents();
Thread.Sleep(time);
SF.Close();
}
It works, but the form is not shown in the right position defined using Top and Left parameters. What is wrong please?
You've got your form set to start in FormStartPosition.WindowsDefaultLocation. Add this into your mSplash function:
SF.StartPosition = FormStartPosition.Manual;
This is why it's trying to position successively down the page (as per your comment) on each opening.
set the start position to manual:
this.StartPosition = FormStartPosition.Manual;
Try this
SF.StartPosition = FormStartPosition.Manual;
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;

Transparent background showing up black

I'm trying to make an image appear on top of another and still show the image underneath via a transparent background. I've got it so the new image appears on top of the other however setting BackColor to Color.Transparent just results in a black background.
Full Code:
public partial class frm_airportApplication : Form
{
PictureBox PicBox;
public frm_airportApplication()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x000000200;
return cp;
}
}
private void button1_Click(object sender, EventArgs e)
{
AllowTransparency = true;
plane p = new plane();
p.getPB().Parent = pb_airport;
this.Controls.Add(p.getPB());
this.Update();
}
protected void InvalidateEx()
{
if (Parent == null)
return;
Rectangle rc = new Rectangle(this.Location, this.Size);
Parent.Invalidate(rc, true);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do not allow the background to be painted
}
private void button2_Click(object sender, EventArgs e)
{
AllowTransparency = true;
ResourceManager resourceManager = new ResourceManager("Airport_Application.Properties.Resources", GetType().Assembly);
PicBox = new PictureBox();
PicBox.BackColor = Color.Transparent;
PicBox.Image = (Bitmap)resourceManager.GetObject("plane_icon");
PicBox.Top = 100;
PicBox.Width = 120;
PicBox.Height = 120;
PicBox.Left = 10;
PicBox.SizeMode = PictureBoxSizeMode.Zoom;
PicBox.Parent = pb_airport;
Controls.Add(PicBox);
PicBox.BringToFront();
}
}
public class plane
{
PictureBox pb;
Bitmap image;
ResourceManager resourceManager;
public plane()
{
resourceManager = new ResourceManager("Airport_Application.Properties.Resources", GetType().Assembly);
image=(Bitmap)resourceManager.GetObject("plane_icon");
pb = new PictureBox();
pb.Image = image;
pb.Top = 500;
pb.Width = 100;
pb.Height = 100;
pb.Left = 50;
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.BackColor = Color.Transparent;
}
public PictureBox getPB()
{
return pb;
}
}
I've found a lot of people who have had similar issues but none of the solutions helped.
It has been awhile but I think you have to set your form to Allow Tranparencies
this.AllowTransparency = true;
or
YourForm.AllowTransparency = true;
that would get rid of the black
I had the same issue but I had just a Panel which should've been transparent so I could see everything underneath it.
The problem was with DoubleBuffered property, it should be set to false.
this.DoubleBuffered = false;
No blackness anymore.
You can create an irregularly shaped form easily by setting its "Region" property. Here's an example:
Irregularly shaped form
As for truly transparent Controls, here's an excellent resource with step-by-step instructions:
Transparent Controls
In simple words, you cannot easily achieve transparency using the default PictureBox control in Windows Forms.
Either you switch to WPF, which by default supports transparency in every bits, or you use a custom control. Once I created such a control called AppIcon, but it is released under GPL, not commercial friendly,
http://mymobilepack.codeplex.com/SourceControl/changeset/view/39314#512415
For forms you can try this:
this.BackColor = System.Drawing.Color.XXX;
this.TransparencyKey = System.Drawing.Color.XXX;
You can try to solve it on the bitmap level:
Make a image in bitmap format and make the backgroundcolor transparant with this method:
bm.MakeTransparent(Color.XXX);
I seemed to solve a similar problem with my splashscreen bij setting a timer every 100ms,
and call DoEvents in it:
private void timer1_Tick(object sender, EventArgs e)
{
//BringToFront();
Application.DoEvents();
}
Hope this helps
If you want to overlay images over images (and not images over form), this would make the trick:
overImage.Parent = backImage;
overImage.BackColor = Color.Transparent;
overImage.Location = thePointRelativeToTheBackImage;
Where overImage and backImage are PictureBox with png (with transparent background).

C# - Transparent modal form on a window

I want to use fully transparent Modal form in my application, with being able to fill it with partially-transparent image; For this, I used to remove all visible elements from the form and got the code below.
class WinScreenshotWindow : Form
{
public WinScreenshotWindow()
{
// Create from without erasing background with a color
// Going not to use transparent form instead, it will produce context menu bugs in textboxes for child form
this.SuspendLayout();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
this.ControlBox = false;
this.Visible = false;
this.Size = new Size(100, 100);
this.Location = new Point(200, 200);
this.ResumeLayout();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Erase Background Windows message:
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle clientRect = e.ClipRectangle;
e.Graphics.FillRectangle(Brushes.Transparent, clientRect);
}
}
static void Main()
{
Form form = new Form();
form.Size = new Size(400, 400);
form.Show();
var ww = new WinScreenshotWindow();
ww.ShowDialog(form);
}
But the result is something strange:
When I remove filling in OnPaint(), it is not visible at all.
The question is - why does this happen? If the background is transparent why do it shows the form in such way? And what can be done in this situation?
Any help appreciated.
Wouldn't it be easier to open a borderless form with a red backcolor and set the TransparencyKey = red?

Categories