How to dark background when my form opens? - c#

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;

Related

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.

How to make a rounded textbox in C# WinForms?

I already made a rounded button and now im trying to create a rounded textbox. I searched some questions but all is in VB and I don't know that language. And converters are weird. So I tried creating a rounded textbox. So I set the UserPaint to true so I can use OnPaint. It works beautifully on my buttons but it glitches out on my textboxes. It has 2 issues:
The font size is always the same size no matter what I set it to.
And when onpaint is called but the text doesn't change it deletes it. That is mostly when I hover my mouse over it.
Code:
class RoundedTextBox : TextBox
{
public Color color = Color.White;
public int borderRadius = 25;
public RoundedTextBox()
{
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height);
SolidBrush brush = new SolidBrush(color);
GraphicsPath GraphPath = Functions.FillRoundedRectangle(e.Graphics, brush, Rect, borderRadius);
this.Region = new Region(GraphPath);
}
}
How I add my textbox:
RoundedTextBox usernameTextbox = new RoundedTextBox();
usernameTextbox.Location = new Point(14, 217);
usernameTextbox.Font = new Font(usernameTextbox.Font.FontFamily, 20);
usernameTextbox.Size = new Size(282, this.Height);
usernameTextbox.color = Color.White;
usernameTextbox.Name = "usernameTextbox";
usernameTextbox.Text = "test";
textboxes.Add(usernameTextbox);
usernameTextbox.BackColor = Color.White;
usernameTextbox.borderRadius = 20;
this.Controls.Add(usernameTextbox);
Gif of the problem

How to Display Controls when using OnPaint Method in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to Display Controls(e.g. RadioButton, Button etc.) when using OnPaint Method in C#? It is possible to create custom controls in constructor, but I need to use common controls of C#? Please suggest.
I'm trying to display common controls but no use.
Please find below code.
private void PaintPanelOrButton(object sender, PaintEventArgs e)
{
Point pt1 = new Point(radioButton1.Left + (radioButton1.Width / 2), radioButton1.Top + (radioButton1.Height / 2));
Point pt2 = new Point(radioButton2.Left + (radioButton2.Width / 2), radioButton2.Top + (radioButton2.Height / 2));
if (sender is Button)
{
Button btn = (Button)sender;
pt1.X -= btn.Left;
pt1.Y -= btn.Top;
pt2.X -= btn.Left;
pt2.Y -= btn.Top;
}
e.Graphics.DrawLine(new Pen(Color.Red, 4.0F), pt1, pt2);
}
public GlassForm()
{
TextBox t = new TextBox();
t.Left = 1000;
t.Top = 900;
t.Name = "txt1";
this.Controls.Add(t);
this.SuspendLayout();
Button buttonOK = new Button();
buttonOK.Location = new Point(10, 10);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
Button buttonCancel = new Button();
buttonCancel.Location = new Point(90, 10);
buttonCancel.Size = new Size(75, 25);
buttonCancel.Text = "Cancel";
this.Controls.AddRange(new Control[] { buttonOK, buttonCancel });
this.ResumeLayout();
this.TransparencyKey = transparentColor;
}
protected override void OnPaint(PaintEventArgs e)
{
try
{
base.OnPaint(e);
Rectangle r1 = new Rectangle(10, 10, 50, 50);
Rectangle r2 = new Rectangle(40, 40, 50, 50);
Region r = new Region(r1);
r.Union(r2);
GraphicsPath path = new GraphicsPath(new Point[] {new Point(45, 45),
new Point(145, 55),
new Point(200, 150),
new Point(75, 150),
new Point(45, 45)
}, new byte[] { (byte)PathPointType.Start,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Line
});
r.Union(path);
using (Brush transparentBrush = new SolidBrush(transparentColor))
{
try
{
BlurBehindWindowEnabled = true;
ExtendFrameEnabled = false;
if (ExtendFrameEnabled)
{
var glassMargins = this.GlassMargins;
NativeMethods.DwmExtendFrameIntoClientArea(this.Handle, ref glassMargins);
marginRegion = new Region(new Rectangle(10, 10, 600, 100));
e.Graphics.FillRegion(transparentBrush, marginRegion);
}
else
{
var glassMargins = new NativeMethods.MARGINS(-1);
NativeMethods.DwmExtendFrameIntoClientArea(this.Handle,
ref glassMargins);
}
if (BlurBehindWindowEnabled)
{
ResetDwmBlurBehind(true, e.Graphics);
e.Graphics.FillRegion(transparentBrush, r);
}
else
{
ResetDwmBlurBehind(false, null);
}
}
catch (Exception ex)
{
lbAeroGlassStyleSupported.Text = "Error";
demoForm.Show();
}
}
this.ShowInTaskbar = false;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
clsTaskbar.Show();
}
catch (Exception ex)
{
}
}
}
If I understand correctly, you want something that look exactly like "normal" controls, but are actually just images.
If so, you can use ControlPaint class: it has many methods to draw buttons, checkBoxex, radiobuttons, etc.
Most of them require a Graphics parameter, so you can easily call them from within your OnPaint event handler
If you want to draw the controls by yourself, you can search for a xxxRenderer class within the .net framework. By using these you can draw check boxes, radio buttons, normal buttons, etc.
But be aware that at is some quite of work, to reproduce the same behavior as the built-in controls (clicking, hovering, focus, keyboard shortcuts, etc.).

Draw a rectangle

For some reason the rectangle doesn't show up when I run the program. But the code runs without any errors. What am I doing wrong?
(I am using csc.exe to compile the code, and I'm writing it in notepad++)
Drawing code:
Graphics g = myform.CreateGraphics();
Pen selPen = new Pen(Color.Blue);
g.DrawRectangle(selPen, 10, 10, 50, 50);
g.Dispose();
Complete Code:
using System;
using System.Windows.Forms;
using System.Drawing;
public class Hello1
{
public static void Main()
{
Form myform = new Form();
myform.Text = "Main Window";
myform.Size = new Size(640, 400);
myform.FormBorderStyle = FormBorderStyle.FixedDialog;
myform.StartPosition = FormStartPosition.CenterScreen;
Graphics g = myform.CreateGraphics();
Pen selPen = new Pen(Color.Blue);
g.DrawRectangle(selPen, 10, 10, 50, 50);
g.Dispose();
myform.ShowDialog();
}
}
You can draw on a form in the Form.OnPaint method override or in the Form.Paint event handler only.
So you need to create a new class inherited from Form:
public class MyForm : Form
{
}
Add the following code to your form:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
using (Pen selPen = new Pen(Color.Blue))
{
g.DrawRectangle(selPen, 10, 10, 50, 50);
}
}
Alternatively, you could subscribe to the myform.Paint event as follows:
myform.Paint += (o, e) => {
Graphics g = e.Graphics;
using (Pen selPen = new Pen(Color.Blue))
{
g.DrawRectangle(selPen, 10, 10, 50, 50);
}
};
This is because the form is painted when it is shown (in your case when ShowDialog is called), and that erases the rectangle you drew.
You have to draw the rectangle:
After the form is shown. For instance, in the Shown event of the form - but beware that when the form will be painted again, the rectangle will disappear (for instance when you minimize/maximize the form),
Or better, while the form is painted (in the Paint event, see Dmitry's answer).
If you rearrange the code to put the drawing code after the showing part, you can see the rectangle. As no lines are read after ShowDialog() until the shown form is closed, you might need to call the Show() method.
public static void Main()
{
Form myform = new Form();
myform.Text = "Main Window";
myform.Size = new Size(640, 400);
myform.FormBorderStyle = FormBorderStyle.FixedDialog;
myform.StartPosition = FormStartPosition.CenterScreen;
myform.Show(); // -> First Show
// -> Then Draw
Graphics g = myform.CreateGraphics();
Pen selPen = new Pen(Color.Blue);
g.DrawRectangle(selPen, 10, 10, 50, 50);
g.Dispose();
}
By doing so, you can see the rectangle. But it will not remain there unless you add the drawing part on its OnPaint event Handler. The drawn rectangle will be vanished when you try to minimize or move the side containing the rectangle, or wheneever the form needs to be Drawn by the OS.

Drawing Graphics on a borderless form

This behavior in C# is bizarre. I have the following class to allow me to effectively 'draw' on the desktop:
class drawOnDesktop {
public static Form dodF = new Form();
public static Graphics formGraphics;
public drawOnDesktop() {
formGraphics = dodF.CreateGraphics();
dodF.BackColor = Color.LightGreen;
dodF.TransparencyKey = Color.LightGreen;
dodF.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
dodF.Location = new Point(0,0);
dodF.StartPosition = FormStartPosition.Manual;
//dodF.FormBorderStyle = FormBorderStyle.None;
dodF.WindowState = FormWindowState.Maximized;
dodF.MinimizeBox = false;
dodF.MaximizeBox = false;
dodF.ControlBox = false;
//dodF.TopMost = true; //For development in case something goes wrong
dodF.BringToFront();
dodF.Show();
}
public static void drawCircle(Point location) {
formGraphics.FillEllipse(Brushes.Black, location.X, location.Y, 10, 10);
}
}
And I call it like this, from my main form:
drawOnDesktop dod = new drawOnDesktop();
drawOnDesktop.drawCircle(new Point(100,100));
If you run that code, you'll get a small black circle in the top left corner of your screen. The problem is that you can see the form's border. Now, try commenting out the FormBorderStyle line. The black dot will appear for a fraction of a second, and disappear. Why!? As you can see, I've set a lot of properties on this form, and still it refuses to work. Is it getting repainted over by the OS?
I don't need to worry about mouse events or things like that - the dots being placed on the screen are completely programmatic, and not from the user. As well, if I set dodF.ShowInTaskbar = false, the entire program crashes.
How can I fix this code so the dot appears and stays until I formGraphics.Clear(Color.Black)?
Don't keep a copy of the graphics around, that is just asking for trouble. As others have stated, you should use the paint event to draw on the screen:
class drawOnDesktop
{
public Form dodF = new Form();
List<Point> circles = new List<Point>();
public drawOnDesktop()
{
dodF.BackColor = Color.LightGreen;
dodF.TransparencyKey = Color.LightGreen;
dodF.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
dodF.Location = new Point(0, 0);
dodF.StartPosition = FormStartPosition.Manual;
dodF.FormBorderStyle = FormBorderStyle.None;
dodF.WindowState = FormWindowState.Maximized;
dodF.MinimizeBox = false;
dodF.MaximizeBox = false;
dodF.ControlBox = false;
dodF.TopMost = true; //For development in case something goes wrong
dodF.BringToFront();
dodF.Paint += dodF_Paint;
dodF.Show();
}
void dodF_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = dodF.CreateGraphics())
{
foreach(Point location in circles)
g.FillEllipse(Brushes.Black, location.X, location.Y, 10, 10);
}
}
public void drawCircle(Point location)
{
circles.Add(location);
}
}
You can call it the same way, but now every time the form repaints, it will redraw the circles.

Categories

Resources