|
I've got that!
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
|
|
|
|
|
You also need ControlStyles.AllPaintingInWmPaint , which is documented in the ControlStyles enumeration documentation.
Also, that enum is attributed with the FlagsAttribute , which means you can save your code some time and just use:
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); This gets compiled as a constant value so SetStyle is only called once.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Do I do one or the other or do I set the styles AND do the manual double buffering witht the bitmaps and the two graphics objects?
|
|
|
|
|
Have you enabled double buffering in your form?
Do this in your initialisation (I put it in my Form constructor after the IntializeComonent() call.
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
|
|
|
|
|
Nope, that didn't help.
|
|
|
|
|
How do you call your DrawGraph( ) function?
|
|
|
|
|
The DrawGraph() function is called from OnPaint()
Invalidate is called every time the draw graph button is clicked or a track bar is moved changing one of the values in the graph.
protected override void OnPaint(PaintEventArgs e)
{
if(this.m_bmImageOffScreen==null)
this.m_bmImageOffScreen=new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
this.m_gOffScreen =Graphics.FromImage(this.m_bmImageOffScreen);
//this.m_gBuffer.Clear(this.BackColor);
if(this.m_bgraphDrawn)
this.DrawGraph();
else
{
this.textBox1.Text = "0";
this.textBox2.Text = "0";
this.textBox3.Text = "0";
this.textBox4.Text = "0";
this.textBox5.Text = "0";
this.m_bgraphDrawn = true;
this.DrawGraph();
}
this.m_gOffScreen.Dispose();
e.Graphics.DrawImageUnscaled(this.m_bmImageOffScreen,30,50);
}
private void trackBar5_Scroll(object sender, System.EventArgs e)
{
// Display the trackbar value in the text box.
textBox5.Text = "" + trackBar5.Value;
value5 += trackBar1.Value;
//this.redrawChart();
//this.DrawGraph();
Invalidate();
}
|
|
|
|
|
what is a better way to implement a server with multiple clients ,using an Event driven handling or to init new
threads to handle each Event?
|
|
|
|
|
I guess it depends on what you mean by "server", which is a rather generic term. If you're referring to a socket listener, then waiting for a connection and passing the new request off to a thread is a good idea. You can also use the event-driven method and use BeginInvoke (it may not show up in IntelliSense, but it's there) which invokes the handler asynchronously.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
According to MSDN doc, when I derive a class that implements IDisposable, I need to override protected virtual void Dispose(bool disposing). And in this function, if disposing is true, I need to call Dispose() on all managed objects that I own (in addition to releasing unmanaged resources).
Now, when I create a new Form object, the wizard gives me the overridden Dispose(bool) method with the only statement being:
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
However, if I add a control, say a button named button1, it doesn't add the statement button1.Dispose() into the above method. Is this a mistake? I mean, do I need to manually add this statement, or is it already called implicitly somewhere?
|
|
|
|
|
First, the Dispose(bool) method doesn't NOT need to be overridden and is not declared in IDisposable . IDisposable declares one method: void Dispose() . Dispose(bool) is a virtual method inheritted from Control . You can implement IDisposable on anything. You don't need to override Dispose(bool) unless you contain components that need to be disposed, like AxHost controls (ActiveX controls in a managed environment) and stuff like that.
Managed objects are managed by the garbage collector. They will be garbage collected when necessary. You typically implement IDisposable (or override Dispose(bool) in your controls) when you need to clean-up unmanaged resources like window handles, file handles, and the like. These things are unmanaged, i.e. they are not managed by the garbage collector (GC). Objects that don't need to free unmanaged resources can implement and use IDisposable to free managed resources, but should use GC.SurpressFinalize(this) in the Dispose() implementation to prevent the GC from wasting time cleaning it up (since you already did).
Other reasons for implementing IDisposable and for callers to call Dispose is to release resources immediately since the GC doesn't run continuously. This makes sure that resources (even managed resources) are released when you want them to be. For things like a Button , it typically doesn't matter. The GC will get around to it. Dispose it only if it's absolutely necessary (otherwise you degrade the performance of your application since the GC is taking time away from your program's execution).
See Programming for Garbage Collection[^] in the .NET Framework SDK for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Actually I wrote the email after reading the Proramming for Garbage Collection article. I guess the reason why I need to Dispose() managed objects like Button() is if holds unmanaged resources, such as maybe icon, brush, etc. But now I realise that I probably don't need to call Dispose() directly on these controls, because when I call base.Dispose(bool) in the Dispose(bool) override that I have in my form, it will presumably call Dispose() for every control that it has (in the Controls collection). Other components such as Timer will be disposed when I call components.Dispose(). This is a guess, which I will verify later on.
|
|
|
|
|
You're really worrying about it too much. The controls and many of the other classes in the FCL will clean themselves up just fine in due time when the GC comes around to collect them. Unless you're strapped for memory (which could be signs of a bad design as well), don't worry about it. If a class should be explicitly disposed, it will typically tell you, like the Graphics class if you create it yourself (never dispose of it from the Paint event or the OnPaint override - the callee will do that when it's ready).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all,
This might be a silly question but here it goes. I have some dll which i've heard is made with c++ but i would like to use them in my C# code. I hope that the functions i am looking for is in those dll's. I am not sure.
the dll's are located here : http://www.pictureflow.com/YarcPlus/Dist/YarcPlusCanonSDK_V10.exe
Hope that somebody can give a answer/example how to do this.
Best Regards
Mihai Marinescu
|
|
|
|
|
That's an EXE, not a DLL, and can't be mapped into memory so you can't use it. Judging by the name, however, it looks like a self-extracting ZIP or installation program, so extract/install it and find the DLLs you need, which should be documented with the functions you need. Then see the DllImportAttribute documentation in the .NET Framework SDK for information about how to declare native functions using P/Invoke. You'll also find more information on MSDN by reading Consuming Unmanaged DLL Functions[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You have to import the functions using the DllImport attribute, like this:
[DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd);
But, of course, you have to know the function names ahead of time.
RageInTheMachine9532
|
|
|
|
|
Hello,
I am sure similar problem must have been here many times, but I am unable to find it, so I am writing about it again. I have a class SSocket, a child of System.Net.Sockets.Socket, the only difference is that it has two static methods, so in worst case it won't be problem to change the architecture a little.
When I retreive a client socket from socket.Accept() and try to cast it to my SSocket variable, exception is thrown, saying the cast is invalid. Can anyone say, what am I doing wrong?
This is how the code looks like:
class SSocket : Socket{
SSocket(..parameters..) : Socket (..same parameters as passed to child constructor..)
public static void GetCommand()
{...}
public static void SendCommand(...){...}
}
...
{
SSocket clientSocket = (SSocket)serverSocket.accept(); <- exception is always thrown
Thanks
John
|
|
|
|
|
The return, of course, is a Socket , not an SSocket , so you can't cast because it isn't that type. Casting to a parent always works, and in a polymorphic design if a method returns the parent class of a deriviative object but the object is a type of the derivative, you can then cast down, like so:
public class Parent
{
public abstract Parent ReturnMe();
static void Main()
{
Child child = (Child)new Parent();
Console.WriteLine(child.ReturnMe());
}
}
public class Child : Parent
{
public override Parent ReturnMe()
{
return this;
}
} That's not the case here.
I recommend encpasulating the Socket instead of deriving from it. This also gives you more control over what the caller can do so you only expose what you want to expose.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I was already quite reconciled with encapsulating the Socket instead, but wanted to know the reason, why the downcasting wasn't possible.
So, to be sure I do understand it - the child class must not introduce any new members or methods in order to be able to accept it's parent?
John
|
|
|
|
|
It doesn't matter if all you do is extend a class and don't add anything more, it's still not the same Type (that's what it all comes down to). Take the following simplistic test:
using System;
public class Test
{
static void Main()
{
Test t = new Test();
Test2 t2 = (Test2)t;
}
}
public class Test2 : Test
{
} It compiles, but you get an InvalidCastException when run. The reason is that t is not an instance of Test2 , so you can't cast it. If I said Test t = new Test2() , then it'd work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a C(++) souce code line:
UserTri* pNewVariable;
...where UserTri is a class. All this should declare a new variable of UserTri type.
My question is: does this above equals to this below in C# ?
UserTri pNewVariable;
The "*" after the UserTri made me think that it might be a pointer, but then again in C# all classes are reference types so the "pointer operation" is actually just hidden here (in C#). True ?
|
|
|
|
|
|
If your C++ code is managed C++: yes, in fact it is. If your C++ code is unmanaged C++: no, it's not the same.
Pointers in managed C++ are managed by the garbage collector, as are the reference types in C#. Hence you don't have to (and you will not be able to)delete them manually. Pointers in unmanaged C++, however, are completely at your mercy, so you have to take care of them via the delete operator.
|
|
|
|
|
Yes (but maybe no, if UserTri is actually a value type (like struct) rather than 'true' class).
I'm an Assembler programmer at heart, and in cases like this, ask myself, "Is this storing an address (of, say, 4 bytes), or the value?" The C++ code is storing an address. The C# code stores address of classes & copy of variable for value types (simple data types & structs).
I LOVE pointers, and regard C#'s reference (and C++'s, for that matter) as 'pointers in disguise'.
Regards
Brewman
|
|
|
|
|
I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?
|
|
|
|
|