|
Hi,
1.
yes all painting should be performed in Paint handlers; one of the reasons is exactly the problem you encounter: it is the Paint handlers that will get fired by Windows when ever there is reason to repaint (part of) a Control (a Form is a Control); restoring from Minimized is a very good reason to repaint everything.
2.
Graphics.FromHwnd() creates a NEW Graphics, which you must Dispose() off when you are done. It is a slow and costly operation that can be avoided.
3.
Why take the long route of taking a handle, asking for a new Graphics, then drawing; all you have to do is set up some data fields that describe the rectangle you want, then Invalidate() the Control (in your case PictureBox), so its Paint handler gets executed again (and draws a rectangle according to the relevant data fields). The Graphics you find in the PaintEventArgs is free, it is not expensive, and you SHOULD NOT Dispose() of that one, since you did not cause its creation, so it isn't your Graphics object.
|
|
|
|
|
4.
The graphics object that you get in the Paint event is clipped to the screen area that you may draw on. This keeps you from drawing on top of other windows when your window is partially covered.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi guys,
Sorry I'm late, I had a busy day.
I got it, thank both of you a lot. I just put all of my code in Pain event and that's work
Just another question:
I want to draw my rectangle by a button in my form, I thought about this kind of code:
void pictureBox_Pain(object sender, PainEventArgs e)
{
if(sender is Button)
{
}
}
Am I right? Or there's another (faster, better,...) way?
Thanks
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
No, that is not the correct way.
If you want to draw something on a control, that is done in two steps:
1. Store information about what to draw in some variable(s) in the form. Call the Invalidate method on the control that you are drawing on.
2. In the Paint event for the control you use the information that you stored to do the actual drawing.
Example:
private bool _showRectangle = false;
void someButton_Click(object sender, EventArgs e) {
_showRectangle = true;
pictureBox.Invalidate();
}
void picturebox_Paint(object sender, PaintEventArgs e) {
if (_showRectangle) {
e.Graphics.DrawRectangle(Pens.Black,10,10,10,10);
}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks friend,
Helped me a lot
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
Evening all !
I'm working on my first ever uni assignment in c# and would like to smarten up the application a bit.
Can anyone point me in the direction of a good site/tutorial etc for implemnting some graphics ?
I have a few main form, some panels etc - do i just assign pictures to them the same way as i have done for a picture box ?
Also, is there any way of changing a panel so it's not just a square/rectangle shape ?
Many thanks
Neil
|
|
|
|
|
What do you mean by 'implimenting some graphics' ? If you have bitmaps you can skin buttons with them or draw them on your form if you're just looking to improve the way it looks.
To draw a picture on the background of a form, handle it's OnPaint event, this is passed a graphics object and you can draw your bitmap to that.
You can search the web for ways to make forms an irregular shape, there's almost certainly example projects here on Code Project.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
|
I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.
ExamFile.Title = "Test Exam File";
ExamFile.FileName = "my First Exam.mfe";
ExamFile.Exam[0].ExamName = "This is my First Exam";
ExamFile.Exam[1].ExamName = "This is my Second Exam";
ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
ExamFile.Exam[0].Question[0].Type = "Multiple";
ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
ExamFile.Exam[0].Question[1].Type = "Single";
ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
ExamFile.Exam[1].Question[0].Type = "Multiple";
Thanks
|
|
|
|
|
Something like..
public class ExamFileBase
{
public string Title { get; set; }
public string FileName { get; set; }
public List<exambase> Exam { get; set; };
public ExamFileBase()
{
Title = "Default title";
FileName = "default_filename.mfe";
Exam = new List<exambase>();
}
}
public class ExamBase
{
public string ExamName { get; set; }
public List<questionbase> Question { get; set; }
public ExamBase()
{
ExamName = "Default exam";
Question = new List<questionbase>();
}
}
public class QuestionBase
{
public string Remark { get; set; }
public string Type { get; set; }
}
</questionbase></questionbase></exambase></exambase>
Only problem is, for the List<t> properties, you'd have to initiate a class before accessing it.
IE:
ExamFileBase ExamFile = new ExamFileBase();
ExamFile.Exam.Add(new ExamBase());
Unless you made them static so each was initialized auto-matically.
|
|
|
|
|
From your code it's evident that you want ExamFile to be a collection of Exam objects. Exams have names and a collection of Question(s), and Question has two string attributes, Remark and Type.
This object model is easily expressed in C#:
public class ExamFile
{
public string Title { get; set; }
public string Name { get; set; }
readonly public List<exam> Exams = new List<exam>();
}
public class Exam : List<question>
{
public string Name { get; set; }
readonly public List<question> Questions = new List<question>();
}
public class Question
{
public string Remark { get; set; }
public string Type { get; set; }
}
</question></question></question></exam></exam>
You could construct the object graph and then set properties as in your posted code, like this:
ExamFile file = new ExamFile();
file.Add(new Exam());
file.Add(new Exam());
file.Exams[0].Questions.Add(new Question());
file.Exams[0].Questions.Add(new Question());
file.Exams[1].Questions.Add(new Question());
file.Exams[1].Questions.Add(new Question());
and then do as in your posted code
file.Title = "Test";
...
file.Exams[1].Questions[1].Remark = "tough question";
However, in most cases you'd create a question object and set it's properties before adding it to an exam. Also note that the types themselves should usually have constructors that take some parameters. This has two side-effects: Less code to write to use the type, which is not very important but a marginal benefit, and a guarantee that an instance of a type cannot be created without certain minimum information.
Types should typically be designed to be flexible and general. Coding convenience can be achieved by writing little "helper methods" separately from the type itself. In this case, you could do something like this:
void addQuestion(Exam e, string remark, string type)
{
Question q = new Question();
q.Remark = remark;
q.Type = type;
e.Questions.Add(q);
}
Exam addExam(ExamFile f, string name)
{
Exam e = new Exam();
e.Name = name;
f.Exams.Add(e);
return e;
}
ExamFile file = new ExamFile();
f.Title = "test";
f.Name = "my first exam.mfe";
Exam e = addExam(file, "First Exam");
addQuestion(e, "remark #1", "Mutiple");
addQuestion(e, "remark #2", "I don't know");
and so on.
|
|
|
|
|
Hello,
You question is very large. I will try to answer you with a simple and short answer, and if you feel you need more explanations, feel free to post back here.
You will need more than a single object to hold your information. For instance, you could use a ExamFile, Exam and Question objects. In the ExamFile object, you could have a List<exam>, and in Exam object you could have a List<question>.
Here is a concrete example of a data structure to hold your information :
using System;
using System.Collections.Generic;
public class ExamFile
{
public String Title { get; set; }
public String FileName { get; set; }
public List<exam> Exams { get; set; }
}
public class Exam
{
public String ExamName { get; set; }
public List<question> Questions { get; set; }
}
public class Question
{
public String Remark { get; set; }
public String Type { get; set; }
}</question></exam>
|
|
|
|
|
I want to build a generic web analytic application using C#
I want to do it by making an addin to IE (and using windows hooks to catch all the user's events)
I tried to find some examples on how to build an addin to IE using C# but I didnt find anything
can someone please point me to where I can find some examples to get me started?
thanks
|
|
|
|
|
i have a c# executable which connects to an sql database and uses active directory for authentication. it runs fine from any folders as long as it's from any of my local drives but it silently doesn't do anything if i run from a network folder. anyone has an explanation for this? and how to fix it? thanks.
----------------------------------------------------------
"unzip; strip; touch; finger; mount; fsck; more; yes; unmount; sleep" - my daily unix command list
|
|
|
|
|
The reason is that Network drives are not automatically on trusted zone. One way to fix this is to increase assembly trust with .NET Framework configuration utility. Also you can sign the assembly and based on the signature add privileges etc.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
|
Even though this has changed in .NET 3.5 SP1, you may still want to read up on "Code Access Security" to understand why your code doesn't work from a network share.
|
|
|
|
|
Hello,
I want to capture the pc desktop screen when any event is fired like start menu is clicked or "my documets" is opened. So for that any listener is needed to listen continuous system events?
And how i recognize when any event is fired?
Help me if anyone knows.
|
|
|
|
|
Why have you asked the same thing three times ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hello,
I have made different queries, 1. how can i open start command using mouse coordinates, 2. is there any listener is needed to listen continuously and perform task when any event is fired like open c:drive.
So please help me out for that. It will be great for me.
|
|
|
|
|
Hello,
I had capture the windows screen of my pc, if i click on "start", the start ,menu should be opened.
Will you please help me for doing this?
|
|
|
|
|
Hello,
I have developed the basic screen capturing of the remote pc to my pc, now when i click on start menu from my pc i need to open start menu on that remote pc n my pc also. So i have the coordinate of start menu but for remote pc how can i use that coordinte?
Please help me out if anyone knows .
Thank you.
|
|
|
|
|
I want to research broadcast, how can I get the VNC open source?
|
|
|
|
|
Try Google.
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|