469,649 Members | 1,275 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

Easy Question about SDK Code New

jm
I got the code below from the .NET SDK. I am still new to C# and I
don't understand how the code works. And by that I mean I don't
understand how the program knows to execute it. I can find no entry
point besides main. I cannot find an event that calls the code. It
is a simple GDI+ application. I just don't see how it ever executes.
I need nothing about the code itself, just how the thing "turns on."
I don't ever see OnPaint called anywhere. Thanks:
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.GDIPlus.GDIPBrushes {
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

public class BrushesSample : System.Windows.Forms.Form {
/// <summary>
/// Required designer variable.
/// </summary>+
private System.ComponentModel.Container components;

private System.Drawing.Brush backgroundBrush;

public BrushesSample() {

//
// Required for Windows Form Designer support
//
InitializeComponent();

this.SetStyle(System.Windows.Forms.ControlStyles.O paque,
true);

//Load the image to be used for the background from the
exe's resource fork
Image backgroundImage = new
Bitmap(Assembly.GetExecutingAssembly().GetManifest ResourceStream("colorbars.jpg"));

//Now create the brush we are going to use to paint the
background
backgroundBrush = new TextureBrush(backgroundImage);
}

protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.AntiAlias;

//Fill the background use the texture brush
//and then apply a white wash
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180,
Color.White)), ClientRectangle);

//Add a Red rectangle and a yellow one that overlaps it
g.FillRectangle(new SolidBrush(Color.Red), 20, 20, 50,
50);
g.FillRectangle(new SolidBrush(Color.FromArgb(180,
Color.Yellow)), 40, 40, 50, 50);

//Add a circle that is filled with a translucent hatch
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal,
Color.Green, Color.FromArgb(100, Color.Yellow));
g.FillEllipse(hb, 250, 10, 100, 100);

//Now create a rectangle filled with a gradient brush
Rectangle r = new Rectangle(300, 250, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r,
Color.Red, Color.Yellow,LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);

//Now add a shape drawn with a path gradient brush
GraphicsPath path = new GraphicsPath(new Point[] {
new
Point(40, 140),
new
Point(275, 200),
new
Point(105, 225),
new
Point(190, 300),
new
Point(50, 350),
new
Point(20, 180),
},
new byte[] {

(byte)PathPointType.Start,

(byte)PathPointType.Bezier,

(byte)PathPointType.Bezier,

(byte)PathPointType.Bezier,

(byte)PathPointType.Line,

(byte)PathPointType.Line,
}
);

PathGradientBrush pgb = new PathGradientBrush(path);
pgb.SurroundColors = new Color[] {
Color.Green,
Color.Yellow,
Color.Red,
Color.Blue,
Color.Orange,
Color.White,
};

g.FillPath(pgb, path);

//Now add a simple rectangle that has been rotated
g.RotateTransform(-30);
g.FillRectangle(new SolidBrush(Color.SlateBlue), 100, 250,
75, 100);
g.ResetTransform();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
components.Dispose();
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent () {
this.components = new System.ComponentModel.Container ();
this.Text = "GDI+ Brush Samples";
this.Size = new System.Drawing.Size(450, 400);
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main() {
Application.Run(new BrushesSample());
}
}
}
Nov 16 '05 #1
2 1165
"jm" <jo*************@yahoo.com> wrote in message news:c6**************************@posting.google.c om...
I got the code below from the .NET SDK. I am still new to C# and I
don't understand how the code works. And by that I mean I don't
understand how the program knows to execute it. I can find no entry
point besides main.
Main( ) is a perfectly good entry point, the application only needs
one, after all. :-)

Let's start there. It calls the Run method on Application (there's no
need to create an Application object because Run( ) is static) and
passes it a new BrushesSample. In doing so, it had to call the
BrushesSample( ) constructor to create an instance of Brushes-
Sample (and that default, parameterless constructor in turn calls
InitializeComponent( ) which assigns initial values to some,
in this case a small number of, inherited properties.)

What is BrushesSample? BrushesSample is a subclass of the
Form base class. BrushesSample just overrides some of the
virtual methods of Form, like OnPaint( ), with the custom function-
ality that BrushesSample requires.
I cannot find an event that calls the code.
Form is bristling with events, check the .NET Framework SDK
documentation for a listing (the following link in 1.1 should open
this in help, it's a long URL so you may need to correct it for
linewrap):

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemWindowsFormsFormMembersTopic.htm

Application.Run( ) registered the "window class" (this goes
back to antiquated Win32 programming days, no need to
concern yourself with it) and a windows event handler to
dispatch windows message (WM_PAINT, etc.) to the
Managed Form subclass.

This may initially appear "disconnected," but the Form is
very much driven by the message-passing in Windows.
I don't ever see OnPaint called anywhere.


Whenever the user covers up a portion of the window
on the screen with another window, re-sizes it, minimizes
it and restores it; Windows sends the native event handler
method a WM_PAINT message. This message and the
client area that needs to get painted (invalidated) is all
processed deep within the Framework and then OnPaint
method gets called for you.

When getting started, the deep underpinnings aren't as
relevant as getting familiar with the many, many methods,
events and properties of Form (and the Controls you can
use on it), at least once you get over the initial cognitive
dissonance over the apparent disconnectness of event
driven UI programming.

We can all appreciate no longer having to write complex
switching logic and Windows message event handlers
anymore, .NET has taken us away from all of that. :-)
Derek Harmon
Nov 16 '05 #2
jm
"Derek Harmon" <lo*******@msn.com> wrote in message news:<#z**************@TK2MSFTNGP11.phx.gbl>...
"jm" <jo*************@yahoo.com> wrote in message news:c6**************************@posting.google.c om...
I got the code below from the .NET SDK. I am still new to C# and I
don't understand how the code works. And by that I mean I don't
understand how the program knows to execute it. I can find no entry
point besides main.


Main( ) is a perfectly good entry point, the application only needs
one, after all. :-)

Let's start there. It calls the Run method on Application (there's no
need to create an Application object because Run( ) is static) and
passes it a new BrushesSample. In doing so, it had to call the
BrushesSample( ) constructor to create an instance of Brushes-
Sample (and that default, parameterless constructor in turn calls
InitializeComponent( ) which assigns initial values to some,
in this case a small number of, inherited properties.)

What is BrushesSample? BrushesSample is a subclass of the
Form base class. BrushesSample just overrides some of the
virtual methods of Form, like OnPaint( ), with the custom function-
ality that BrushesSample requires.
I cannot find an event that calls the code.


Form is bristling with events, check the .NET Framework SDK
documentation for a listing (the following link in 1.1 should open
this in help, it's a long URL so you may need to correct it for
linewrap):

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemWindowsFormsFormMembersTopic.htm

Application.Run( ) registered the "window class" (this goes
back to antiquated Win32 programming days, no need to
concern yourself with it) and a windows event handler to
dispatch windows message (WM_PAINT, etc.) to the
Managed Form subclass.

This may initially appear "disconnected," but the Form is
very much driven by the message-passing in Windows.
I don't ever see OnPaint called anywhere.


Whenever the user covers up a portion of the window
on the screen with another window, re-sizes it, minimizes
it and restores it; Windows sends the native event handler
method a WM_PAINT message. This message and the
client area that needs to get painted (invalidated) is all
processed deep within the Framework and then OnPaint
method gets called for you.

When getting started, the deep underpinnings aren't as
relevant as getting familiar with the many, many methods,
events and properties of Form (and the Controls you can
use on it), at least once you get over the initial cognitive
dissonance over the apparent disconnectness of event
driven UI programming.

We can all appreciate no longer having to write complex
switching logic and Windows message event handlers
anymore, .NET has taken us away from all of that. :-)
Derek Harmon

So, essentially, I override the method OnPaint which is always called
and therefore it painted.
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Oliver Bryant | last post: by
4 posts views Thread by I_have_nothing | last post: by
10 posts views Thread by Brian | last post: by
3 posts views Thread by Jason | last post: by
409 posts views Thread by jacob navia | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.