C# Clickable And Transparent Form - c#

I'm trying to do transparent and clickable form. This form will work full screen. So this form must be clickable. For example if desktop exists behind this form, I must be able to click to folders and open.
If a document exists I must be able to change this document. I couldn't solve how to enable this.
I think it can be done with API codes, but I don't know which API. If you can give me some information/link I will be pleased.

This is simple and works without calling any API function.
But it will prohibit any interaction of your program with the user..
For a Screensaver this makes no sense, however, at least not as you described it.
The rule is this:
Anything that is not 100% transparent is not click-through.
So you could easily create a darker screen using Opacity and a Black BackColor but it won't be click-through.
The simplest solution would be to switch between the both modes upon MouseMove using a Timer to turn the saving mode back on, maybe like this:
public Form1()
{
InitializeComponent();
this.Text = "";
this.MinimizeBox = false;
this.MaximizeBox = false;
this.ControlBox = false;
this.WindowState = FormWindowState.Maximized;
switchSaverState(false);
}
Point lastMosePosition = Point.Empty;
int savingWaitMinutes = 10;
int minute = 60000;
Timer timer1 = new Timer;
void switchSaverState(bool saving)
{
if (saving)
{
this.BackColor = Color.Black;
this.Opacity = 0.8;
}
else
{
this.TransparencyKey = Color.Fuchsia;
this.BackColor = Color.Fuchsia;
this.Opacity = 1;
timer1.Interval = minute * savingWaitMinutes;
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (timer1.Interval == minute * savingWaitMinutes)
{
timer1.Interval = 100;
switchSaverState(true);
lastMosePosition = Cursor.Position;
}
else
{
if (Cursor.Position != lastMosePosition) { switchSaverState(false); }
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
lastMosePosition = Cursor.Position;
switchSaverState(false);
}
But again: This is like a Screensver, meaning that you can't work while it 'saves'! To do that use the controls on your monitor!!

Related

Programming Scary Screamer on C#

I am new in c# and trying to develop Scary Screamer Application. It is an joke windows forms application which is running on the PC and invisible in taskbar.
There is timer running in this application. If system datetime.now.minute = 15
It should play scary sound and show scary picture on the screen. After 1-2 seconds picture should disappear from the screen.
But i am stuck and don't know how to make picture disappear. Any Ideas how to do that?
Below is my code:
namespace screamer2
{
public partial class Form1 : Form
{
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Width = Screen.PrimaryScreen.Bounds.Width/2;
this.Height = Screen.PrimaryScreen.Bounds.Height/2;
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
BackgroundImage = Properties.Resources._1;
System.Threading.Thread.Sleep(500);
pla.Play();
}
}
}
}
I would propose to set image once in Form1_Load and then control any showing and hiding of window using Form.Opacity variable. I have tested the code below and should work as you wanted.
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Opacity = 0; //This line added
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height / 2;
this.BackgroundImage = Properties.Resources._1; //We set the image once here
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
this.Opacity = 1; //We show the window
System.Threading.Thread.Sleep(500);
pla.Play();
this.Opacity = 0; //We hide the window
}
}

C# Searchable, Positionable PictureBox

I've been searching about this for a while and haven't found what I'm looking for.
I want to be able to have an image in a picturebox that can be positioned to a point by means of a "search box". Basically, mapping locations in pixels to certain phrases or letters that then shift the position on the picturebox.
I tried to set the location using points, however this does not change the picture at all; the location after calculating the code below stubbornly stays at (3,3).
The picture also is large (~3500 x ~3000) and needs scrolls bars to fully view it.
Here's my code
public Form1()
{
InitializeComponent();
flowLayoutPanel1.AutoScroll = true;
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
flowLayoutPanel1.Controls.Add(pictureBox1);
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "m")
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = Image.FromFile(#"C:\User\Desktop\map.jpg");
pictureBox1.Location = new Point(700, 200);
// ^ The location does not change and stays at (3,3)
// The picturebox is not set as locked
}
Do I need to do something different? Or is the issue with my picture for not allowing me to change the location?
EDIT I found the Solution thanks to the help below. I had to use a panel and place the picturebox within. Below is the code I used.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panel1.AutoScroll = true;
panel1.Controls.Add(pictureBox1);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = Image.FromFile(#"C:\Desktop\image.jpg");
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
label1.Visible = true;
int Hvalue = panel1.HorizontalScroll.Value;
label1.Text = Hvalue.ToString();
label2.Visible = true;
int Vvalue = panel1.VerticalScroll.Value;
label2.Text = Vvalue.ToString();
if (textBox1.Text == "m")
{
// these are just values that I put in
panel1.HorizontalScroll.Value = 616;
panel1.VerticalScroll.Value = 90;
}
}

GUI GroupPanel MouseEvents

My Form has a problem with that GroupBbox MouseEvents.
I'm trying to make some GUI gadgetries (docking, opacity..).
Here is an example:
I've linked all (GUI)-Objects to these two functions.
private void MyMouseMove(object sender, MouseEventArgs e)
{
this.Opacity = 1;
}
private void MyMouseLeave(object sender, EventArgs e)
{
this.Opacity = 0.5;
}
..expect the group panels, because they don't have MouseMove and MouseLeave events. Can they be added? A standard Panel has them as well.
I really like the layout of that GroupPanels (with that border and text), that's why I would love to be able to solve that problem with GroupBox.
That gadgets I create will only be triggered, if the cursor is in- or outside the form. (doesn't matter if inactive or active). Maybe there is another way to trigger it, than MouseMove and MouseLeave.
Using a Timer is probably the simplest solution!
Thank you LarsTech for linking to this 'Winform - determine if mouse has left user control' question.
I'm able to continue my Project with this sample below.
public partial class Form1 : Form
{
private Timer timer1;
public Form1()
{
InitializeComponent();
this.Opacity = 0.5D;
timer1 = new Timer();
timer1.Interval = 200;
timer1.Tick += timer1_Tick;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.DesktopBounds.Contains(Cursor.Position))
this.Opacity = 1D;
else
this.Opacity = 0.5D;
}
}
credits goes to: Hans Passant

Fading in and fading out for a form

i have a requirement in which my form is transparent,if my mouse enters into it the form
should became visible,if my mouse leaves out of the form it becomes transparent, i have three different controls placed in my form , each controls mouse leave and mouse enter is the same that of the form . if my mouse enters into the form and enters into a control
form_mouseleaveevent and control_mouseenterd gets fired so iam not able to achieve it,how to overcome this.
below is the piece of code for this:
private void TransToOpac()
{
if (!isTransparent)
return;
if (TtoOON == false )
{
TtoOON = true;
for (i = this.Opacity; i <= 1; i = i + 0.02)
{
this.Opacity = i;
Thread.Sleep(50);
}
isTransparent = false;
TtoOON = false;
}
}
private void OpacToTrans()
{
if (isTransparent)
return;
if (OtoTON == false )
{
OtoTON = true;
for (i = this.Opacity; i >= 0.5; i = i - 0.02)
{
this.Opacity = i;
Thread.Sleep(50);
}
isTransparent = true;
OtoTON = false;
}
}
private void OnMouseEntered(object sender, EventArgs e)
{
TransToOpac();
}
private void OnMouseLeft(object sender, EventArgs e)
{
OpacToTrans();
}
You can't get this done with MouseEnter/Leave events. The smaller problem is that the form's Leave event may never fire if a control is close to the edge. The bigger problem is that it will fire when the cursor moves into the non-client area (border, caption), you don't want to fade the form when the user tries to close or resize the window.
The crude but effective solution is to use a timer to check where the mouse is located:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Opacity = 0.99; // Avoid flicker
mFadeTimer.Interval = 15;
mFadeTimer.Tick += new EventHandler(mFadeTimer_Tick);
mMouseTimer.Interval = 200;
mMouseTimer.Tick += new EventHandler(mMouseTimer_Tick);
mMouseTimer.Enabled = true;
}
void mMouseTimer_Tick(object sender, EventArgs e) {
if (this.Bounds.Contains(Control.MousePosition)) {
if (mFade <= 0) { mFade = 1; mFadeTimer.Enabled = true; }
}
else {
if (mFade >= 0) { mFade = -1; mFadeTimer.Enabled = true; }
}
}
void mFadeTimer_Tick(object sender, EventArgs e) {
double opaq = this.Opacity + mFade * 0.05;
if (opaq >= 0.99) { opaq = 0.99; mFadeTimer.Enabled = false; }
if (opaq <= 0.15) { opaq = 0.15; mFadeTimer.Enabled = false; }
this.Opacity = opaq;
}
private Timer mFadeTimer = new Timer();
private Timer mMouseTimer = new Timer();
private int mFade = 0;
}
You could also check in Form_MouseLeave whether the mouse pointer is still within the form's bounds and in that case not fade out.
EDIT
There are several ways to find out whether the mouse is still within form's bounds. Easiest would be to use the Mouse.GetPosition method to find the current mouse position. The result is the location of the mouse pointer in screen coordinates. You can then check, whether the form's bounding rectangle contains the location.

C# - Make form semi-transparent while moving

Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move event with no luck.
I'm stuck, any help?
The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.
The ResizeEnd event fires after a form has finished moving, so something like this should work:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.5;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:
[STAThread]
static void Main()
{
using (Form form = new Form())
using (Timer tmr = new Timer())
{
tmr.Interval = 500;
bool first = true;
tmr.Tick += delegate
{
tmr.Stop();
form.Opacity = 1;
};
form.Move += delegate
{
if (first) { first = false; return; }
tmr.Stop();
tmr.Start();
form.Opacity = 0.3;
};
Application.Run(form);
}
}
Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.

Categories