Drawing Graphics on a borderless form - c#

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.

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;

How to resize/redraw rectangle on paint event?

I have three monitors in the following configuration:
Monitor 1 Monitor 2 Monitor 3
[1280 x 1024] [1200 x 1900] [1280 x 1024]
I use a form to outline a selected monitor (code below), which works great when I initialize or change between Monitors 1 and 3, but results in two rectangles drawn when Monitor 2 is selected :/
I tried to modify the code in many-many different ways, but nothing seems to work. I thought someone might be able to help me understand why two rectangles are being drawn (1280 x 1024 and 1200 x 1900) and how to correct.
Thank you for your time, regards and happy New Year.
P.S. If possible, please keep explanation simple as I am still learning.
public partial class ScreenArea : Form
{
private Pen _pen;
private int screenSelect;
public ScreenArea(int selectScreen = 0)
{
//xInitializeComponent();
TopMost = true;
ShowInTaskbar = true;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
_pen = new Pen(Color.Aqua, 5);
Paint += new PaintEventHandler(ScreenArea_Paint);
ScreenSelect = screenSelect;
}
private void ScreenArea_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(_pen, 0, 0, Width, Height);
}
public int ScreenSelect
{
get
{
return screenSelect;
}
set
{
Rectangle screenBounds;
try
{
screenBounds = Screen.AllScreens[value].Bounds;
screenSelect = value;
}
catch (Exception)
{
screenBounds = Screen.AllScreens[screenSelect].Bounds;
}
this.Left = screenBounds.X;
this.Top = screenBounds.Y;
this.Width = screenBounds.Width;
this.Height = screenBounds.Height;
}
}
}

Buttons are two pixels too small

I have been working with windows forms for a short while and have noticed that button controls always appear one pixels smaller in each direction than I am trying to make them.
To illustrate, the TextBoxes and Button in the image bellow are set to the exact same size but are different sizes.
wrong size buttons
Here is the code that I used to generate the buttons:
public Form1() {
InitializeComponent();
this.Size = new Size(216, 239)
TB1 = new TextBox();
TB1.Multiline = true;
TB1.Size = new Size(100, 100);
TB1.Location = new Point(100, 0);
Controls.Add(TB1);
TB2 = new TextBox();
TB2.Multiline = true;
TB2.Size = new Size(100, 100);
TB2.Location = new Point(0, 100);
Controls.Add(TB2);
TestButton = new Button();
TestButton.Text = "sdfsdf";
TestButton.Size = new Size(100, 100);
TestButton.Location = new Point(100, 100);
Controls.Add(TestButton);
}
From the Image you can see that there is white space around the button. I have tried changing the Control.Margin and Control.Padding but this extra space around the button is unaffected by those.
In order to make the button appear 100x100 (the way I want it) you have to move it one pixel up and to the left and make it two pixels wider and taller. (TextButton.Size = new Size(102, 102); TextButton.Location = new Point(99, 99);)
What I would like to do is make the buttons actually the size I set them to be. Because of the number of buttons in my program, it is undesirable to manually increase the size of each button and I am looking for a more elegant long term solution that I can use going forwards.
I have tried to create a wrapper class around the button class called MyButton but it doesn't work with polymorphism (explained bellow):
class MyButton : Button {
public MyButton() : base() {}
public new Size Size {
get;
set {
int width = value.Width + 2; // make it two wider
int height = value.Height + 2; // make it two taller
Size size = new Size(width, height);
base.Size = size;
}
}
public new Point Location {
get;
set {
Console.WriteLine("running"); // used to make sure this is actually being run
int x = value.X - 1; // move it one left
int y = value.Y - 1; // move it one up
Point point = new Point(x, y);
base.Location = point;
}
}
}
When I create a MyButton object and use myButtonObject.Size = ... it works perfectly and the button sizing and location works out. However, at another place in my code I have a function that takes a Control object as input and here my code from the MyButton class is not being used.
MyButton btn1 = new MyButton();
btn1.Size = new Size(100, 100);
btn.Location = new Point(100, 100);
// ^ this works great and the button is the right size
public void ControlFormatter(Control control) {
control.Size = new Size(100, 100);
control.Location = new Point(100, 100);
}
MyButton btn2 = new MyButton();
ControlFormatter(btn2);
// ^ this doesn't work
Using the Console.WriteLine("running") print statement that I put in MyButton.Location.Set, I can tell that when control.Location is called inside ControlFormatter() the code that I wrote is not run (I can only assume that it is using the default Control.Location property and thus making the buttons the wrong size.
I guess I'm asking two things
Is there an easier/better/cleaner way to make the buttons the right size?
How can I make it so that ControlFormatter() uses MyButton.Size when it can and not Control.Size?
Thanks, also I'm fairly new to C# so grace is appreciated.
I opted for the quicker and dirtier fix of testing whether or not the Control was a Button in my ControlFormatter() function.
public void ControlFormatter(Control control) {
int width = 100;
int height = 100;
if (control is Button) {
width -= 2;
height -= 2;
}
control.Size = new Size(width, height);
control.Position = new Point(); // you get the jist
}

Display a Form in grayscale

Is it possible to Draw any Form (without overridding the Paint method) in grayscale.
If I show a Form in a Modal() Dialog, I wan't do show its parent as grayscale.
I noticed this in the Visual Studio Extension Manager. If a progressbar is downloading a package, the underlying window is grayed out.
I am thinking of this:
private void Button1_Click(object sender, EventArgs e)
{
using (var dialog = new Form2())
{
SetGrayscale(this, true);
dialog.ShowDialog();
SetGrayscale(this, false);
}
}
Update
Just setting Form.Enabled = false; is not what I intended. That does not look as good as a grayscale representation of my form.
I think the compiz window decorator for Linux did this with apps that are unresponsive.
As has already been said the way to do this is to overlay another control / form on top of your existing form and have it render a grayscale version of this on top, you could either do this using an additional form placed exactly over the original form, or using something like a Panel positioned on top of all other controls.
Here is a working example of how you might do this when placing another form exactly over the client area of the first. How to use it
using (Grayscale(this))
{
MessageBox.Show("Test");
}
Implementation
public static Form Grayscale(Form tocover)
{
var frm = new Form
{
FormBorderStyle = FormBorderStyle.None,
ControlBox = false,
ShowInTaskbar = false,
StartPosition = FormStartPosition.Manual,
AutoScaleMode = AutoScaleMode.None,
Location = tocover.PointToScreen(tocover.ClientRectangle.Location),
Size = tocover.ClientSize
};
frm.Paint += (sender, args) =>
{
var bmp = GetFormImageWithoutBorders(tocover);
bmp = ConvertToGrayscale(bmp);
args.Graphics.DrawImage(bmp, args.ClipRectangle.Location);
};
frm.Show(tocover);
return frm;
}
private static Bitmap ConvertToGrayscale(Bitmap source)
{
var bm = new Bitmap(source.Width, source.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = source.GetPixel(x, y);
var luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}
return bm;
}
private static Bitmap GetControlImage(Control ctl)
{
var bm = new Bitmap(ctl.Width, ctl.Height);
ctl.DrawToBitmap(bm, new Rectangle(0, 0, ctl.Width, ctl.Height));
return bm;
}
private static Bitmap GetFormImageWithoutBorders(Form frm)
{
// Get the form's whole image.
using (Bitmap wholeForm = GetControlImage(frm))
{
// See how far the form's upper left corner is
// from the upper left corner of its client area.
Point origin = frm.PointToScreen(new Point(0, 0));
int dx = origin.X - frm.Left;
int dy = origin.Y - frm.Top;
// Copy the client area into a new Bitmap.
int wid = frm.ClientSize.Width;
int hgt = frm.ClientSize.Height;
var bm = new Bitmap(wid, hgt);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.DrawImage(wholeForm, 0, 0,
new Rectangle(dx, dy, wid, hgt),
GraphicsUnit.Pixel);
}
return bm;
}
}
Note that:
The implementation of Paint is fairly poor - really it should use double buffering so that the grayscale image is pre-rendered to a buffered graphics context so the Paint method just needs to paint the pre-drawn buffer contents. See Custom Drawing Controls in C# – Manual Double Buffering
ConvertToGrayscale is a tad on the slow side, but can probably be sped up
Things will go wrong if someone manages to move the original form for any reason
The image is static, if the base control gets redrawn then ideally the top form should redraw too. I'm not sure how best to detect when a portion of another form has been invalidated.
If I find the time I'll try and fix some of those problems, but the above at least gives you the general idea.
Note that in WPF this would be a lot easier.
Sources:
How to convert a colour image to grayscale
Get the image of a control or form, or a form's client area in C#
I don't think there is a way to do it directly - I think all forms are rendered with sRGB.
A hacky way could be to overlay the form with a copy of it as an image (this is simple to do with Control.DrawToBitMap) and then pass it through a simple GDI matrix to desaturate https://web.archive.org/web/20141230145627/http://bobpowell.net/grayscale.aspx.
Try something like this which would work for most simple controls (you would need to recurse into containers to switch all controls correctly).
private void button1_Click(object sender, EventArgs e)
{
using (var dialog = new Form())
{
Dictionary<Control, Tuple<Color, Color>> oldcolors = new Dictionary<Control, Tuple<Color, Color>>();
foreach (Control ctl in this.Controls)
{
oldcolors.Add(ctl, Tuple.Create(ctl.BackColor, ctl.ForeColor));
// get rough avg intensity of color
int bg = (ctl.BackColor.R + ctl.BackColor.G + ctl.BackColor.B) / 3;
int fg = (ctl.ForeColor.R + ctl.ForeColor.G + ctl.ForeColor.B) / 3;
ctl.BackColor = Color.FromArgb(bg, bg, bg);
ctl.ForeColor = Color.FromArgb(fg, fg, fg);
}
dialog.ShowDialog();
foreach (Control ctl in this.Controls)
{
ctl.BackColor = oldcolors[ctl].Item1;
ctl.ForeColor = oldcolors[ctl].Item2;
}
}
}

How to draw a screenshot "preview" window?

I have a Winforms application that the user uses to take a region based screenshot. I want to have a small preview pane, but i'm not sure how to do it. So far i tried recreating a bitmap on mouse move and its just way too laggy to be usable. So i thought if i used a pre-defined image (screenshot of the whole screen) and moved it inside the picturebox based off the mouse location so that you get a zoomed in look at the screen (to select the precise pixels you want to take the screenshot with easier). I'm not sure how i can implement this, i am also pretty new to drawing so i will show you what i have now.
private void falseDesktop_MouseMove(object sender, MouseEventArgs e)
{
zoomBox.Image = showZoomBox(e.Location);
zoomBox.Invalidate();
}
private Image showZoomBox(Point curLocation)
{
int x = 0;
int y = 0;
if (curLocation.X - 12 <= 0)
{
x = curLocation.X - 12;
}
else
{
x = curLocation.X;
}
if (curLocation.Y - 11 <= 0)
{
y = curLocation.Y - 11;
}
else
{
y = curLocation.Y;
}
Point start = new Point(curLocation.X - 12, curLocation.Y - 11);
Size size = new Size(24, 22);
Rectangle rect = new Rectangle(start, size);
Image selection = cropImage(falseDesktop.Image, rect);
return selection;
}
private static Image cropImage(Image img, Rectangle cropArea)
{
if (cropArea.Width != 0 && cropArea.Height != 0)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
bmpImage.Dispose();
return (Image)(bmpCrop);
}
return null;
}
EDIT:
Here is a mock up like requested:
The black part of this image is a panel, of course the text being a label and the area where you see the image (stack overflow) would be the picturebox (called zoomBox) the lines on top of the zoomBox would be a guide and where the 2 lines intersect would be the mouse position. Hope this is a better assist to help you understand my issue.
Another thing that might help explain my issue is The form actually fills the entire screen with a "false desktop". its a picturebox that covers the entire screen with a screenshot of the desktop when "printscreen" was pressed. So I want this little "preview pane" to basically be a zoomed in location of where the mouse is.
This is a little bit laggy, but worth a try:
It's a WInForms app in a single file showing how a "live" zoom might work. It doesn't paint the cross hairs etc. that's up to you.
Key Parts:
_scale
PictureBoxSizeMode.StretchImage
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class Form1 : Form {
private Bitmap _myImage = new Bitmap(#"C:\Users\Public\Pictures\Sample Pictures\LightHouse.jpg");
private int _scale = 10; // keep this < 15
private PictureBox pboxMain;
private PictureBox pboxZoom;
private System.ComponentModel.IContainer components;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
pboxMain.Image = _myImage;
}
private void pboxMain_MouseMove(object sender, MouseEventArgs e) {
try {
Rectangle rc = new Rectangle(
new Point(e.X - _scale, e.Y - _scale),
new Size(_scale * 2, _scale * 2));
pboxZoom.Image = _myImage.Clone(rc, PixelFormat.DontCare);
}
catch (OutOfMemoryException ex) {/* ignore... */}
}
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent() {
this.pboxMain = new PictureBox();
this.pboxZoom = new PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pboxMain)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pboxZoom)).BeginInit();
this.SuspendLayout();
this.pboxMain.Dock = DockStyle.Fill;
this.pboxMain.Location = new System.Drawing.Point(0, 0);
this.pboxMain.Name = "pboxMain";
this.pboxMain.Size = new System.Drawing.Size(767, 435);
this.pboxMain.TabIndex = 0;
this.pboxMain.TabStop = false;
this.pboxMain.MouseMove += new MouseEventHandler(this.pboxMain_MouseMove);
this.pboxZoom.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))),
((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.pboxZoom.BorderStyle = BorderStyle.FixedSingle;
this.pboxZoom.Location = new System.Drawing.Point(12, 12);
this.pboxZoom.Name = "pboxZoom";
this.pboxZoom.Size = new System.Drawing.Size(106, 83);
this.pboxZoom.SizeMode = PictureBoxSizeMode.StretchImage;
this.pboxZoom.TabIndex = 1;
this.pboxZoom.TabStop = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(767, 435);
this.Controls.Add(this.pboxZoom);
this.Controls.Add(this.pboxMain);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pboxMain)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pboxZoom)).EndInit();
this.ResumeLayout(false);
}
}
This should be of great help TeboScreen: Basic C# Screen Capture
Why reinvent the Wheel :-)

Categories

Resources