|
Hi,
I have a problem in GDI+, I put a code snippet (draws a rectangle with a text inside) in the onpaint event handler of my simple form but when I resize the form, the handler redraws my rectangle? Is there a way to determine if there is a rectangle or some other thing on the form's drawing area to not to redraw the rectangle? How can I solve this problem? (my code is below...)
Thanks in advance...
Cem Louis
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GDI_Test01
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.form1_Paint);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void form1_Paint(object sender, PaintEventArgs e)
{
// Obtain the Graphics object
Graphics g = this.CreateGraphics();
int rect_width = 110; int rect_height = 20;
Rectangle rect = new Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;
// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect, strFormat1);
verdanaFont.Dispose();
g.Dispose();
}
}
}
|
|
|
|
|
A couple of things.
First, the problem is not that the rectangle is being drawn again when the form is resized, it's that the old one is not being erased.
To solve this use the Graphics object passed into the paint event handler (e.Graphics) instead of creating a Graphics object.
Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the line ResizeRedraw = true; to your form's constructor. You can see for yourself the difference this makes.
One last thing, since you're doing this painting in the form itself, you can override the OnPaint method rather than handling the paint event.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Hi Charlie,
Thank you for your reply, I didn't know the ResizeRedraw property before.
Regards,
Cem Louis
|
|
|
|
|
Hi.
Can someone show me how to fill 50% of the below drawing with a solid color.
From buttom to top.
Thanks
Regards r9
The drawing:
private System.Drawing.Graphics graphicsObj = null;
private System.Drawing.Pen penBlackWidth1 = new System.Drawing.Pen(System.Drawing.Color.Black,1);
private Point[] curvePoints = null;
private SolidBrush lightGrayBrush = new SolidBrush(Color.LightGray);
private Point point1;
private Point point2;
private Point point3;
private Point point4;
private SolidBrush blackBrush = new SolidBrush(Color.Black);
...................................................
try
{
graphicsObj = e.Graphics;
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 175, 20);
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 45, 120);
graphicsObj.DrawLine(penBlackWidth1, 175, 20, 175, 120);
graphicsObj.DrawLine(penBlackWidth1, 45,120,90,160);
graphicsObj.DrawLine(penBlackWidth1, 175, 120, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 90, 160, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 483, 345);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 483, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 597, 345);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 592, 335);
graphicsObj.DrawLine(penBlackWidth1, 597, 345, 592, 335);
point1 = new Point(592,335);
point2 = new Point(597,345);
point3 = new Point(587,345);
point4 = new Point(592,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
point1 = new Point(478,335);
point2 = new Point(483,345);
point3 = new Point(473,345);
point4 = new Point(478,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Here's a though - look in your mathematics book. If you want exactly 50%, then find the algorithm given specified polygram. This is a forum for C#, not math.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath Stewart
The algorithm is not the problem.
But how do I fill the figure?
(the C# GDI+ code is my problem)
Regards
C#
|
|
|
|
|
Looking at the Graphics class, the logical choices are FillPolygon , FillPath , or FillRegion . Full class documentation - it's a great thing to read.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
This code creates a control (based on the Panel) with a new property named "Colors". Setting the property yo an array of colors will display the colors.
(example:
multiColorPanel1.Colors = new Color[] { Color.Red, Color.White, Color.Blue};
end of example)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Project1
{
/// <summary>
/// Summary description for MultiColorPanel.
/// </summary>
public class MultiColorPanel : System.Windows.Forms.Panel
{
private Color[] colors = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MultiColorPanel()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
public Color[] Colors
{
get
{
return colors;
}
set
{
colors = value;
this.Invalidate();
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
if ((colors != null) && (colors.Length > 0))
{
Graphics area = pevent.Graphics;
int height = this.Height / colors.Length;
for (int index = 0; index < colors.Length; index++)
{
Rectangle rect = new Rectangle(0, height * index, this.Width, this.Height);
Brush brush = new SolidBrush(colors[index]);
pevent.Graphics.FillRectangle(brush, rect);
}
}
else
{
base.OnPaintBackground (pevent);
}
}
}
}
|
|
|
|
|
How to referenc functions in other code file/unit?
|
|
|
|
|
Thats a bit vague. Are they .NET assemblies or C code or what?
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
|
|
|
|
|
Sorry, I am new in .Net.
I want to call a functin in c# code file from a form code in same application, I tried this:
//uMyLib.c
// created on 12/02/2004 at 02:16 ?
using System;
namespace TextLib
{
public class TextLib
{
string ReversText(string s)
{
string txt = "";
//for (int i=0; i <= s.Length-1; i++)
for (int i = s.Length-1; i>=0 ;i--)
{
txt += s[i].ToString();
}
return txt;
}
}
}
//==============
// project created on 12/02/2004 at 02:12 Õ
using System;
using System.Windows.Forms;
using uMyLib; // file name
namespace MyFormProject
{
class MainForm : System.Windows.Forms.Form
{
.......
..........
..........
void ButtonClick(object sender, System.EventArgs e)
{
uMyLib.MyLib S = new uMyLib.TextLib();
label.Text = S.ReversText(textBox.Text);
}
Thanks
|
|
|
|
|
klufy wrote:
using uMyLib; // file name
This should not be the name of the file, but the name of the namespace. So rewrite it as:
using TextLib;
Also, is this file in the same project or another project?
If it is in the same project then there is nothing more you should need to do. If it is in another project is the other project in the same solution as this one or not?
If the two projects are in the same solution right click the forms project (which is probably your exe file) and select "Add Reference..." from the context menu, in the dialog select the projects tab and select the project that includes TextLib.
If the project containing TextLib is not in the solution it may be best to add it to the solution (if they are both under your control) or just link to it if not.
To add the other project right-click on the root element in the Solution Explorer (the solution element) and select "Add"-->"Existing Project..." and browse for the project file.
Does this help?
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
|
|
|
|
|
This is fundamental to object-oriented programming and you really should pick up a good book about the .NET Framework (which most entry-level books present information on basic OO designs and programming) from http://www.microsoft.com/mspress[^] or something.
First of all, the files DO NOT matter, so long as their compiled into the same assembly (except in the case of Java, in which only one public class can be in a file and the classname and filename must match, and they get compiled to .class files). Second, you don't reference assembly names in code - the current assembly references other assemblies. In your case, you're using two completely different namespaces, TextLib and MyFormProject . If these are in the same assembly, they typically should share a common namespace! Notice how you keep typing using System.Something ? That's not magic - you're merely telling the compiler which namespaces - which can span multiple assemblies - to look in for classes and other Types. The filename matters not.
So, you can do either of the following:
using TextLib;
TextLib tl = new TextLib();
label.Text = tl.ReversText(textBox.Text); or
TextLib.TextLib tl = new TextLib.TextLib();
label.Text = tl.ReversText(textbox.Text); Again, you really should read a book or two on .NET programming.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks Colin and Heath.
Boath files are in the same project.
My call now is like this:
void ButtonClick(object sender, System.EventArgs e)
{
TextLib.TextLib tl = new TextLib.TextLib();
label.Text = tl.ReverseText(textBox.Text);
}
I got this error :
'TextLib.TextLib.ReverseText(string)' is inaccessible due to its protection level(CS0122)
Thank you again for your advice.
|
|
|
|
|
On your ReverseText Function put it as
public string ReverseText(string s)
{
}
This is because the C# Compiler marks it as private(thus only allowing it to be accessed by the same class it's in)
if you don't specify an access modifier.
Other modifiers are
protected
virtual
abstract
private
public
Look up these modifiers on codeproject for more info =)
------------------
I'm naked under my clothes...
|
|
|
|
|
|
Actually, lookup information on access modifiers here: 3.5.1 Declared Accessibility[^] in the C# language specification on MSDN. In fact, you'd do well to read the whole specification.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all,
I need your EXPERT opinion on something, I am trying to create an application to track the steps a user took while using an application, (kind of like turbo tax or window xp does) and store it in a database. I then need to be able to display the steps from the database, on a menu or a list, and they have to be links(because each link they will be able to fire off a child window).
My question is what would be the best way to perform these tasks? I am a nebie so bare with me .........I am going to use C# for the application part of it, but how do I store the user steps? And how do I display them? What else do I need to create this app, i.e. XML?ASP? I dont what it to be webbased though, I want it to be a stand alone app. Links to examples would be helpful. As a matter of fact, any help would be appreciated. Thanks in advance.
|
|
|
|
|
Just save the keys and mouse clicks in their respective events.
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
It is, simply put, an event based scenario. Depending on what you want to track, you can use standard events (those offered by the Framework classes) or create your own events to deal with your application's business logic (by building your own event handler delegate classes).
Also, depending on the amount of user data you want to track, you can store it in simple XML files (if the amount is small) or in a more sophisticated data store like an Access or SQL server database (if you want to store large quantities or complex relationships of user data).
|
|
|
|
|
hi;
i've some problems with my SMTP client ...
1) is there a better way to get the mail exchange of a server other than nslookup ???
2) how to send to a a recipient not on this server (i connect- with a socket- to yahoo and i want to send a mail to hotmail) ,or i have to connect to the appropriate server for each reciepient ??
3) when i send data to yahoo the no text appeares on the message. in spite it did on hotmail and others ....
thx alot
Mina Aziz
|
|
|
|
|
mina_aziz wrote:
1) is there a better way to get the mail exchange of a server other than nslookup ???
Programmatically. There is a couple examples of this, including A Managed C++ Email Validator Control for ASP.NET
[^]. It's in Managed C++, but if you truly understand .NET programming, you should have a problem at all translating it to C#.
mina_aziz wrote:
2) how to send to a a recipient not on this server (i connect- with a socket- to yahoo and i want to send a mail to hotmail) ,or i have to connect to the appropriate server for each reciepient ??
No, not unless they allow relaying to other domains, which most SMTP servers don't (that's how SPAM spreads and why should their server be burdened with it). The reason that MX records exist is so that SMTP clients connect to the mail server for a domain. The DNS administrator must make sure that the SMTP servers he sets as the MX records for a domain will relay for that domain.
mina_aziz wrote:
3) when i send data to yahoo the no text appeares on the message. in spite it did on hotmail and others
Without an example, who could tell you what's wrong?! Be more specific. More than likely, you're using an invalid MIME type with a multi-part MIME email. For a simple test, all you need to do is this:
$ telnet mail.domain.com 25
HELO host.mydomain.com
MAIL FROM: username@mydomain.com
RCPT TO: username@domain.com
DATA
Subject: Testing
This is a test
.
QUIT This is the most basic email and if it doesn't work, then Yahoo! has a problem (which I HIGHLY doubt).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Folks,
I'm new to web-based programming. I'm wondering if anyone has recommendations or can point me to info on the following?
I'd like to implement a web-based slider control that is similar to Jens Scheidtmann's CRangeSlider. A cleaner version of what I'd eventually want to build is something like a Spotfire slider. I can rely on an explicit button that will do the postback when the user is ready to submit the range they have selected. On top of the slider's two ends, I'd like to place two text boxes (or equivalents). The text would show the dynamically changing low end and high end range values that the user is selecting with the sliders.
For the gui, do I have to do custom draw here? Can I do this purely as an asp.net control? Or do I have to use JavaScript? I don't have any experience with JS, but it seems trivial enough. But if need to do with JS, how do I do the event handling?
Any help, suggestions, worldly wisdom, chocolate chip cookies?
Thanks
|
|
|
|
|
Flying Iguana wrote:
I'm new to web-based programming.
No kidding!
You can't "owner-draw" in HTML. Web pages are constructed of HTML, images, scripts (like JS), and such. If you need runtime drawing of graphics, you either generate them on the server or use ActiveX (requires Windows and a complicated setup with Wine on linux or BSD and Konquerer, and even then it may still not work), Java (requires a JRE on the system and Java to be enabled, plus it's sandboxed), or .NET smart clients (requires .NET on Winodws, Internet Explorer, and is sandboxed as well).
HTTP - the protocol of the web - is also stateless and essentially one way. The client makes a request and the server returns a response. Period. If you need graphics (like charts and what-not), these must be rendered on the server directly to the response stream using the URL requested, or saved as a file with the path included in HTML.
If you need sliders, you're going to have to use Javascript (or any script, though others aren't supported on various platforms like VBScript). ASP.NET describes server-side controls and processing, which javascript only to support a few features (like client-side validation for IE).
New to Javascript, then read. You can find information about JScript - Microsoft's version of Javascript which is mostly compatible (as a language) - at http://msdn.microsoft.com/library/en-us/script56/html/js56jsoriJScript.asp[^]. Understand that most of what you do when scripting Dynamic HTML (DHTML) pages is that the script language manipulates the Document Object Model (DOM). You can read about that for IE (because each browser supports a subset of the standards while adding their own proprietrary extensions) at http://msdn.microsoft.com/workshop/author/dhtml/dhtml_node_entry.asp[^].
If you want more information this, try the ASP.NET or Web Development forums. This is the C# forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i have to print a .pdf file to a HP LaserJet 9000 PCL mfp printer which can print duplex. i am using Acrobat COM object to print, and .net's PrintDocument's PrinterSettings for setting the printer. But it returns
false when i use CanDuplex and does not change the duplex setting when i set PrinterSettings.Duplex. The code can be seen below. does anyone have idea how can i solve this? Or is there are more easy way doing this like using a PrinttoFile printer or etc.?
PrintDocument1.PrinterSettings.PrinterName="HP LaserJet 9000 PCL 6";
if(PrintDocument1.PrinterSettings.CanDuplex)
{ axPdf1.printPages(1,1); PrintDocument1.PrinterSettings.Duplex=System.Drawing.Printing.Duplex.Horizontal;
axPdf1.printPages(2,3);
}
i want to thank Heath Stewart for his assistance at printing pdf files.
|
|
|
|
|