|
Hey, I have button in a Form, I implemented its Click handler as when its clicked an X is drawn.... I obtained the Graphics using the CreateGraphics() method of the button used it to call the DrawLine methods..
When I click it, it draws the line properly but when I move the mouse outside the button's boundary it vanishes. why?
The code for the click event handler is
private void button1_Click(object sender, EventArgs e)
{
Graphics g = button1.CreateGraphics();
g.DrawLine(new Pen(Color.Black), 20, 20, 200, 200);
g.DrawLine(new Pen(Color.Black), 200, 20, 20, 200);
}
|
|
|
|
|
When you say an X is drawn, do you mean on the button or on the form?
Also, you have to realize that any event that alters the form visually like a drag & drop or a mouse movement, you would need to "invalidate" the present form so that it would redraw itself.
|
|
|
|
|
I get the graphics from the button's CreateGraphics method and draw with it, this means that I'm drawing on the button right?
When I move the mouse inside the button's boundary it doesn't redraws, why? as soon as it gets outside the button, the drawing is gone Here, what is being redrawn? the button or the form?
|
|
|
|
|
Colin gave you the answer, much better than I did.
|
|
|
|
|
Ahmed Manzoor wrote: I get the graphics from the button's CreateGraphics method and draw with it, this means that I'm drawing on the button right?
Yes.
Ahmed Manzoor wrote: When I move the mouse inside the button's boundary it doesn't redraws, why? as soon as it gets outside the button, the drawing is gone
Because when you click a button, or hover over it the drawing of the button changes to give feedback to the user. For example when you click a button it looks pushed down, when you release your mouse button it pops back up again. Each of those drawing operations overwrite any previous drawing operations.
You draw onto the button directly. As soon as the user does anything that requires the button to redraw (clicking the button, hovering over the button, stopping hovering over the button, minimising the window, resizing the windows, moving the window, etc. etc.) then your drawing is gone and replaced with what ever the button control needs to draw.
Ahmed Manzoor wrote: Here, what is being redrawn? the button or the form?
Depending on what triggered redrawing it could be a number of things. That is why there is an OnPaint method. Regardless of what caused the button, or its parent control, or its parent form to be redrawn, OnPaint will get called.
You must therefore override the Button's OnPaint method and supply your additional drawing code in there.
|
|
|
|
|
Colin Angus Mackay wrote: Ahmed Manzoor wrote:
I get the graphics from the button's CreateGraphics method and draw with it, this means that I'm drawing on the button right?
Yes.
No.
It means that you are drawing on the screen where the button happens to be. The button is not at all aware of your drawings.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: It means that you are drawing on the screen where the button happens to be.
That is true in all cases regardless of the control. You only ever draw on the screen where other things happen to be.
Guffa wrote: The button is not at all aware of your drawings
I can draw on my kitchen table, it isn't aware of my drawing either. I suppose it would have been more correct to say "over" rather than "on", but since it is a 2D surface you can't really have "over".
|
|
|
|
|
Because what you have drawn does not persist on the control you drew it on.
Basically, when you move your mouse off the button, the button redraws itself, thus overwriting what you drew.
If you want to draw on things you have to do it by overriding the OnPaint method. You have to remember what you drew before because each time the control is invalidated you have to draw it again (The system will call OnPaint each time the control is invalidated)
|
|
|
|
|
Ahmed Manzoor wrote: Graphics g = button1.CreateGraphics();
I'd love to know what sample it is that causes people to use this method where they shouldn't. As someone else said, you need to draw in your paint event.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
|
Wow - now THAT is pathetic.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Ok, I don't want it to be drawn, the first time, only when the button is clicked...
But still you won, now you may give me another way to do it and I got it....
Thanks y'all
|
|
|
|
|
Hello everyone,
I want to monitor current network utilization of current machine (Windows Server 2003) to see how busy the server is with network traffic. Any samples?
BTW: the background is, I am writing a tool which will copy large file only when server network utilization is not very high (i.e. I do not want to impact current product working network utilization and want to find some relatively free network utilization time slot to copy files).
thanks in advance,
George
|
|
|
|
|
I'm probably stating the obvious here, but perhaps you could use WMI[^] to do this? See this[^] CP article as an example.
/ravi
|
|
|
|
|
Thanks Ravi,
Good documents!
regards,
George
|
|
|
|
|
|
Good to learn from you, thanks Hamid!
regards,
George
|
|
|
|
|
I glad of I heared it.
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
Thanks again!
have a nice day, Hamid!
regards,
George
|
|
|
|
|
Hello everyone,
I have source codes for both managed code part and native code part of my project. My project is a console application wrote in managed code (C#), but invokes native C++ COM function through interop.
My question is, how to debug them together? What I expect is I could run from managed code, and step through manged code, and when managed code invokes native COM code through interop, I could also step into native part from managed code.
Any solutions?
thanks in advance,
George
|
|
|
|
|
|
Thanks Abhijit,
I have followed your steps but met with such error when pressing F5 in the managed console project.
The error message is,
--------------------
Error while trying to run project: Unable to start program xxx.exe
The debugger does not support debugging managed and native code at the same
time on this platform.
--------------------
I am using Visual Studio 2008 on Windows Server 2003 x64 edition. Any hints
what is wrong?
regards,
George
|
|
|
|
|
|
Thanks Abhijit,
The document about debugging you recommended is for smart device. But I am debugging on Windows Server 2003 x64 platform.
Any other comments or ideas about how to debug?
BTW: The article you wrote looks cool! Great!! I have a related question, what is the differences between we assign keep-alive and not keep-alive in HTTP connection to a server?
regards,
George
|
|
|
|
|
Here's a neat debugging trick I learned a while back for debugging a component that's different than the component that starts execution. (I'm not sure if it applies to your situation. It might.)
1. Open the component's project in Visual Studio.
2. Double click Properties in Solution Explorer.
3. Click the Debug tab.
4. Under Start Action, click Start external program.
5. Enter the path to the executable that starts execution (and later calls the component you're trying to debug.)
6. Press F5 to start debugging.
This lets you set breakpoints in the component you're trying to debug. Hope it helps.
|
|
|
|