473,769 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# grotty graphics

If I look for guidance on this topic I find simple examples like this:

public class Form2:Form {
public Form2() {
this.Text = "FORM2";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandl er(Draw_Graphic s);
}

public void Draw_Graphics(o bject sender,PaintEve ntArgs e)
{
Graphics g = e.Graphics;
System.Drawing. Bitmap bmap = new System.Drawing. Bitmap(500, 400);
for(int i = 0; i < 500; i++)
for(int j = 0; j < 50; j++)
bmap.SetPixel(i , j, Color.Red);
g.DrawImage(bma p,200,100);
}
};

static void Main()
{
Application.Run (new Form2());
}

This works as advertised but there is some sly sleight of hand going on
here.
Apparently the Form2 constructor raises a signal that is caught by the
handler Draw_Graphics() .
What was the signal and who raised it?
I ask because if I replace the innocuous-looking statement
"Application.Ru n" by a simple call to the constructor it executes that but
no event is raised and the handler does not run.
What is the dark secret here? What does Application.Run () actually do?

Now I want to run a dialog box first and then perform the above, so I say:

Application.Run (new Form1())
Application.Exi t();
Application.Run (new Form2())

No dice! I get Form1 and the Form2 constructor but no signal.

Ray Reeves


Mar 3 '07 #1
7 1612
<"rayreeves" <ray<mond>re*** *@verizon.net>w rote:
If I look for guidance on this topic I find simple examples like this:

public class Form2:Form {
public Form2() {
this.Text = "FORM2";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandl er(Draw_Graphic s);
}

public void Draw_Graphics(o bject sender,PaintEve ntArgs e)
{
Graphics g = e.Graphics;
System.Drawing. Bitmap bmap = new System.Drawing. Bitmap(500, 400);
for(int i = 0; i < 500; i++)
for(int j = 0; j < 50; j++)
bmap.SetPixel(i , j, Color.Red);
g.DrawImage(bma p,200,100);
}
};

static void Main()
{
Application.Run (new Form2());
}

This works as advertised but there is some sly sleight of hand going on
here.
Apparently the Form2 constructor raises a signal that is caught by the
handler Draw_Graphics() .
No, the Windows message loop requests that the form is painted, and the
constructor to Form2 registers a handler for the paint event.
What was the signal and who raised it?
I ask because if I replace the innocuous-looking statement
"Application.Ru n" by a simple call to the constructor it executes that but
no event is raised and the handler does not run.
What is the dark secret here? What does Application.Run () actually do?
It starts a Windows message loop.
Now I want to run a dialog box first and then perform the above, so I say:

Application.Run (new Form1())
Application.Exi t();
Application.Run (new Form2())

No dice! I get Form1 and the Form2 constructor but no signal.
Well, you don't want Application.Exi t() in there - that will exit the
application (as the name suggests) unless you have any other threads
running.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 3 '07 #2
"rayreeves" <ray<mond>re*** *@verizon.netwr ote:
If I look for guidance on this topic I find simple examples like this:

public class Form2:Form {
public Form2() {
this.Text = "FORM2";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandl er(Draw_Graphic s);
}

public void Draw_Graphics(o bject sender,PaintEve ntArgs e)
{
Graphics g = e.Graphics;
System.Drawing. Bitmap bmap = new System.Drawing. Bitmap(500, 400);
for(int i = 0; i < 500; i++)
for(int j = 0; j < 50; j++)
bmap.SetPixel(i , j, Color.Red);
g.DrawImage(bma p,200,100);
}
};

static void Main()
{
Application.Run (new Form2());
}

This works as advertised but there is some sly sleight of hand going on
here.
Yes. It's Win32's windowing system.
Apparently the Form2 constructor raises a signal that is caught by the
handler Draw_Graphics() .
No. Application.Run () contains a loop (called the "message loop" or
"message pump") which does basically two things in its body:

1) Retrieve a message from Windows (see e.g. GetMessage and PeekMessage
in MSDN docs)
2) Dispatch that message from Windows (see e.g. DispatchMessage in MSDN
docs)

Messages are things like WM_PAINT, which is a request to paint a window
(for example, the window might have been obscured by another window, and
is now visible). Messages contain several parameters: the message code
(WM_PAINT in this case), the window it applies to, and two parameters
whose meaning changes based on message kind (wParam and lParam).

Retrieving the message and dispatching the message are done by the
application so that it happens on a thread the application controls.

Dispatching the message sends it off to something called a window
procedure, which is a function pointer (a bit like a delegate)
associated with that window in the OS. When the window is first created,
the creator of the window gets a chance to specify this window procedure
(see CreateWindow in MSDN docs).

The window procedure for .NET controls is Control.WndProc . Basically,
different descendants of Control have 'case' statements that switch on
the message type and call different methods based on the message.

In Control.WndProc , there's an entry in this 'case' statement that calls
a private method called WmPaint, which in turn calls
PaintWithErrorH andling, which itself calls OnPaint, which finally calls
the 'Paint' event if it is non-null.

I'm leaving out many details, but that's the gist of it.
Now I want to run a dialog box first and then perform the above, so I say:

Application.Run (new Form1())
This sets up a message loop and keeps going until the application exits
(with Application.Exi t) or the main form (the argument) is closed.
Application.Exi t();
Because there's no loop to exit, this does nothing. The documentation
for this method says:

"Informs all message pumps that they must terminate, and then closes all
application windows after the messages have been processed."

Because there's no message pump active (Application.Ru n() is not
running), and there's no forms on screen at this point, the method won't
do anything.
Application.Run (new Form2())
This shows a second form, in a new loop.

-- Barry

--
http://barrkel.blogspot.com/
Mar 3 '07 #3
Jon Skeet [C# MVP] <sk***@pobox.co mwrote:
Well, you don't want Application.Exi t() in there - that will exit the
application (as the name suggests) unless you have any other threads
running.
Doh! Ignore this bit, and read Barry's answer instead, which is much
more sensible.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 3 '07 #4

"Barry Kelly" <ba***********@ gmail.comwrote in message
news:e8******** *************** *********@4ax.c om...
"
>Application.Ru n(new Form2())

This shows a second form, in a new loop.

-- Barry

--
http://barrkel.blogspot.com/
Thanks for the comprehensive and detailed response.
Unfortunately, the second form Form2 does not appear, unless it is the only
form. It would be nice to know if a Paint event actually occurred and who
caught it.

I don't even really want to paint on a Form but it's not apparent to me that
I can just create a window. I would also like to SetPixel directly in the
window without using BitMaps so that I can watch the picture grow, but that
doesn't seem possible either.

Ray Reeves
Mar 4 '07 #5
"rayreeves" <ray<mond>re*** *@verizon.netwr ote:
Thanks for the comprehensive and detailed response.
Unfortunately, the second form Form2 does not appear, unless it is the only
form. It would be nice to know if a Paint event actually occurred and who
caught it.
The second form should appear after you close the first. It does in this
simple test program:

---8<---
using System;
using System.Windows. Forms;

class App
{
public static void Main()
{
Application.Run (new Form());
Application.Exi t();
Application.Run (new Form());
}
}
--->8---
I don't even really want to paint on a Form but it's not apparent to me that
I can just create a window. I would also like to SetPixel directly in the
window without using BitMaps so that I can watch the picture grow, but that
doesn't seem possible either.
Windows doesn't use a "retained mode" graphics infrastructure.
Everything is lost if the window is obscured in any way. You need to
draw to a bitmap, and keep the bitmap around. For example:

---8<---
using System;
using System.Drawing;
using System.Windows. Forms;

class MyForm : Form
{
Bitmap _image = new Bitmap(500, 500);
Button _testButton = new Button();
Random r = new Random();

public MyForm()
{
Paint += MyPaintEvent;
using (Graphics g = Graphics.FromIm age(_image))
g.FillRectangle (Brushes.White, 0, 0, 500, 500);
ClientSize = new Size(500, 500);
_testButton.Par ent = this;
_testButton.Tex t = "Click Here";
_testButton.Cli ck += MyClickEvent;
DoubleBuffered = true;
}

void MyClickEvent(ob ject sender, EventArgs e)
{
// Random color for RGB fields, and 'OR' in 255 for Alpha.
Color c = Color.FromArgb( r.Next(0x100000 0) | (0xFF << 24));
using (Graphics g = Graphics.FromIm age(_image))
{
using (Pen p = new Pen(c))
g.DrawLine(p, r.Next(500), r.Next(500),
r.Next(500), r.Next(500));
}
_image.SetPixel (r.Next(500), r.Next(500), c);
// Invalidate() needed to get windows to send paint message
// to repaint form.
Invalidate();
}

void MyPaintEvent(ob ject sender, PaintEventArgs e)
{
e.Graphics.Draw Image(_image, 0, 0);
}

public static void Main()
{
Application.Run (new MyForm());
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Mar 4 '07 #6
I do exactly that in my code but don't succeed. Somehow my Dialog Form is
intercepting the Paint event, if there is one. Its true that a simpler Form1
does work properly for me..I'll have to try whitteling down my Dialog bit by
bit.
I am very much obliged for your kind attention and detailed help.

Ray Reeves

"Barry Kelly" <ba***********@ gmail.comwrote in message
news:ea******** *************** *********@4ax.c om...
"rayreeves" <ray<mond>re*** *@verizon.netwr ote:
>Thanks for the comprehensive and detailed response.
Unfortunatel y, the second form Form2 does not appear, unless it is the
only
form. It would be nice to know if a Paint event actually occurred and who
caught it.

The second form should appear after you close the first. It does in this
simple test program:

---8<---
using System;
using System.Windows. Forms;

class App
{
public static void Main()
{
Application.Run (new Form());
Application.Exi t();
Application.Run (new Form());
}
}
--->8---
>I don't even really want to paint on a Form but it's not apparent to me
that
I can just create a window. I would also like to SetPixel directly in the
window without using BitMaps so that I can watch the picture grow, but
that
doesn't seem possible either.

Windows doesn't use a "retained mode" graphics infrastructure.
Everything is lost if the window is obscured in any way. You need to
draw to a bitmap, and keep the bitmap around. For example:

---8<---
using System;
using System.Drawing;
using System.Windows. Forms;

class MyForm : Form
{
Bitmap _image = new Bitmap(500, 500);
Button _testButton = new Button();
Random r = new Random();

public MyForm()
{
Paint += MyPaintEvent;
using (Graphics g = Graphics.FromIm age(_image))
g.FillRectangle (Brushes.White, 0, 0, 500, 500);
ClientSize = new Size(500, 500);
_testButton.Par ent = this;
_testButton.Tex t = "Click Here";
_testButton.Cli ck += MyClickEvent;
DoubleBuffered = true;
}

void MyClickEvent(ob ject sender, EventArgs e)
{
// Random color for RGB fields, and 'OR' in 255 for Alpha.
Color c = Color.FromArgb( r.Next(0x100000 0) | (0xFF << 24));
using (Graphics g = Graphics.FromIm age(_image))
{
using (Pen p = new Pen(c))
g.DrawLine(p, r.Next(500), r.Next(500),
r.Next(500), r.Next(500));
}
_image.SetPixel (r.Next(500), r.Next(500), c);
// Invalidate() needed to get windows to send paint message
// to repaint form.
Invalidate();
}

void MyPaintEvent(ob ject sender, PaintEventArgs e)
{
e.Graphics.Draw Image(_image, 0, 0);
}

public static void Main()
{
Application.Run (new MyForm());
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/

Mar 4 '07 #7
What I have found is that if the Form1 Dialog Box contains plain buttons
then when they are pressed, even if they do nothing, the Paint signal to
Form2 is pre-empted! Closing the Form1 window is the only way to get to
Form2.
Grotty!

Ray Reeves
Mar 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3378
by: JBiagio | last post by:
Hello All, I am attempting to learn a bit about the GDI+ transforms and charting data and I feel like I'm getting a handle on how the transforms work. My chart object has a large "canvas" bitmap that all data is scaled to and rendered on, and I display a smaller "window" of the canvas that the user can page through left and right. I've been able to scale my data (for the purposes of this discussion, x values are 0 - 2PI and y values...
12
2387
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store it as member variable of the form , make
2
3601
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph = new Graphics //SC // first of all there is no constructor for Graphics // so in the Form_Paint I use instead:
14
3103
by: Pmb | last post by:
At the moment I'm using Borland's C++ (http://www.borland.com/products/downloads/download_cbuilder.html#) I want to be able to take an array of points and plot them on the screen. Is there a way to do this? E.g. I want to be able to graph a function. At this point I'm not up to a level in C++ where I want to start learning Visual C++ so I don't want to go that route. Thanks
5
27168
by: Charles A. Lackman | last post by:
Hello, I have created a complete PrintDocument and need to create an image from it. How is this done? e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality e.Graphics.DrawString(Line1.Text, FontLine1, TheBrush, Thelocation1, 390 + yPos, AStringFormat) e.Graphics.DrawString(Line2.Text, FontLine2, TheBrush, Thelocation2, TheHeight1 + (390 + yPos))
6
3245
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument (much like the current model), but my PrintDocument would have a Pages collection such that each time you need to have an additional page, you would just add another page to the collection and then use the page object for the actual drawing etc. ...
12
3600
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet utilities that allows you to view them with live rotation in a web browser. Also, it includes a introductory tutorial to POV-Ray.
8
3133
by: Abhiraj Chauhan | last post by:
I need someone to make an example of how to create a graphics window in VB.net 2008. I understand the basics of how to draw a rectangle and lines etc. What I need is an example of how to make a window that goes in a form that does the following: 1. When the application is run, the form has a graphics area within the form (not the entire form) colored white. 2. Two text boxes (not in the graphics area) for data entry used to define...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.