|
And what do you mean by an "enter Form". It's rather ambiquous, and serialization doesn't really care what kind of object you're serializing so long as its attributed directly with SerializableAttribute . That attribute is not inheritted. Since the Form class is not attributed with SerializableAttribute , then you need to attribute your derivative class.
But ask yourself why you need to serialize it and what, specifically, you need to serialize. Much of the form is specific to that instance and should not be serialized (like the Handle ) if you plan on either re-creating it or saving it across multiple sessions (a session being separate logins, processes, etc.).
To control how your form (or any object) is serialized, implement ISerializable .
For more information on serialization, read Serializing Objects[^] in the .NET Framework SDK online. The SDK is also installed by default with VS.NET and can be installed stand-alone from http://msdn.microsoft.com/netframework[^].
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi everybody I have a problem with DataGrid Control, That is:
I try to extract the column number and the row number by double click anywhere on the Datagrid
But there is no response by DoubleClick event when I try to DoublClick any Cell in the DataGrid ,the response occurs when I just click the Header Column or Header Row please can I get some help.
|
|
|
|
|
1.Firstly, DoubleClick event should fire, without any doubt. I do not find any problem in it.
2.Click or DoubleClick Event Handler of the DataGrid or other Controls has EventArg argument, you would not be getting the colum number / row number information from the EventArg . You should use either
MouseDown or MouseUp event Handler which has MouseEventArgs in the arguments.
MouseDown Event and HitTest method will get you the desired.
Refer the below link for details.
DataGrid HitTest Method[^]
|
|
|
|
|
sir,
i need to get system informations such as number of services,etc.i write queries such as select * from Win 32_service etc.But i don't know more about this table.I want to use some where clause also in the above query.for eg: i want to get all services whose mode is manual.I need column names for Win32_service
help mee
|
|
|
|
|
|
thank you very much,Mr.Nick Parker.You gave me the correct link
|
|
|
|
|
|
Hi,
I am writing a forms based application, which has been working fine, until I moved my project to another computer with the same version of Visual studio. Now my initial form does not show up at all.
This is what I have done:
I have a console application that starts three forms, one after the other. Each form implements two methods invloved in starting the form in its own separate thread.
class UIForm : ....
{
Thread formThread = null;
public void OpenForm()
{
// formThread is a private member of the current form
formThread = new Thread( new ThreadStart( this.ShowForm ) );
formThread.Start();
}
private void ShowForm()
{
Application.Run( this );
}
.
.
.
}
The console application then starts the form like this:
UIForm theForm = new UIForm();
theForm.OpenForm();
This worked fine until I moved my project to another machine.
Any help, comments or advise is much appreciated!!
Thanks!
|
|
|
|
|
Given my limited understanding of all things .Net, take what I say with a grain of salt.
The thread that creates the window must be the one that updates the window. The thread that creates the window must also participate in a message loop. Although you may get away with violating these rules some of the time, it is guarenteed to bite you in the butt sometime in the future.
Unless you are doing something really special, I would have one thread handle all of the UI and have the other threads doing non-UI stuff. Use Form.Invoke() to handle communication between the threads.
|
|
|
|
|
I agree to what you say, but I don't see how I violate any of the rules in the startup phase. The form is started in its own thread, which should then be bound to the message loop. Or have I misunderstood things?
|
|
|
|
|
You created the window using one thread then executed another to make it visible. This violates the first principle I stated above.
Try creating the window with the new thread.
|
|
|
|
|
Thank you very much! I discovered your solution by trial and error, but I did not understand why. Now I now that there is something behind it. Again, thank you.
|
|
|
|
|
Hello, i'm having a problem getting an byte[] out of an arraylist...
I use an enumerator so the code is as followed:
bIpAdres = Convert.ToByte(ipsEnum.Current);
bIpAdres however is a byte[], and the values in the collection are also arrays, but writing
Convert.ToByte[]
doesn't work.
How should I do this?
Grtz, Bert
|
|
|
|
|
While the answer is in the documentation, the name should say enough: Convert.ToByte converts an argument to a single byte . Notice how it's singular?
If ipsEnum.Current represents a byte[] array, then simply cast it:
bIpAdres = (byte[])ipsEnum.Current;
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
So if I understand you correctly you have an ArrayList that contains a byte[] as elements. If so and you are confident that the ArrayList does in fact contain the values then the following will work.
foreach(byte[] arr in arrList)
{
//Do something
};
Alternatively,
foreach(object obj in arrList)
{
byte[] arr = (byte[])obj;
};
|
|
|
|
|
I've got a (managed)directX function i'm trying to call that takes a stream to an bitmap/image file or object, but i'd like to have the image file handled directly, rather than using a stream to a file.
But i'm wondering whether serialisation is the right way to go. Will this remove the image formatting? Is there a way to cast a Bitmap or Image into a byte[] structure?
I really should know this, but all this matrix maths and texture alignment has melted my brain
Cheers
Cata
|
|
|
|
|
Save the Bitmap to a MemoryStream , then call MemoryStream.ToArray to get a byte[] array.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello,
I run the program below to handle winmessages from an application written in a 4GL. If I close the 4GL window which uses this program, the C# program receives the WM_DESTROY message, but rebuilding the C# program after that fails because it is 'in use'. I suspect that I have to dispose some objects on WM_DESTROY? How can I code for that? I just started learning C#, if you can point me to some info about this I would be happy too.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class GenericWindow : NativeWindow
{
public const int WM_ERASEBKGND = 0x14;
public const int WM_DESTROY = 0x0002;
private const int GWL_WNDPROC = -4;
private const int WM_SYNCPAINT = 0x0088;
private const int WM_NCPAINT = 0x0085;
private const int WM_PAINT = 0x000F;
private IntPtr oldWndProc = IntPtr.Zero;
private Win32WndProc newWndProc = null;
private int hIcon;
#region Imported User32.DLL functions
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern int GetDC(int hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int ReleaseDC(int hWnd,int hDc);
[DllImport("User32.dll")]
private static extern int DestroyIcon(int hIcon);
[DllImport("user32.dll")]
public static extern int DrawIcon(int hdc, int x,int y, int hIcon);
[DllImport("user32.dll", EntryPoint="CallWindowProc")]
private static extern int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, Win32WndProc newProc);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
#endregion
// A delegate that matches Win32 WNDPROC:
private delegate int Win32WndProc(IntPtr hWnd, int Msg, int wParam, int lParam);
public GenericWindow()
{
}
public void NotifyWindow(int windowHandle)
{
this.AssignHandle((IntPtr)windowHandle);
oldWndProc = GetWindowLong((IntPtr)windowHandle,GWL_WNDPROC);
newWndProc = new Win32WndProc(MyWndProc);
SetWindowLong((IntPtr)windowHandle, GWL_WNDPROC,newWndProc);
PaintExtras();
}
private int MyWndProc(IntPtr hWnd, int Msg, int
wParam, int lParam)
{
int pHandle;
switch(Msg)
{
case WM_PAINT:
pHandle = CallWindowProc(oldWndProc, hWnd,Msg, wParam, lParam);
PaintExtras();
return pHandle;
case WM_DESTROY:
pHandle = CallWindowProc(oldWndProc, hWnd,Msg, wParam, lParam);
//MessageBox.Show("dest");
return pHandle;
default:
return CallWindowProc(oldWndProc, hWnd, Msg,
wParam, lParam);
}
}
public void PaintExtras()
{
int hdc = GetDC(this.Handle.ToInt32());
if (hdc != 0)
{
hIcon = ExtractIcon(this.Handle.ToInt32(),"C:\\progress10\\wrk\\experim\\down.ico",0);
int ret = DrawIcon (hdc, 1, 1, hIcon);
ret = DestroyIcon (hIcon);
ret = ReleaseDC(this.Handle.ToInt32(),hdc);
}
}
}
}
regards,
Stefan.
|
|
|
|
|
Why not just override NativeWindow.WndProc instead of defining your own window procedure? The WndProc services the same purpose - to subclass windows. You would call base.WndProc in your override to create the message chain.
You should call DestroyHandle when finished with the NativeWindow . To do this via the disposable pattern, you must implement the disposable pattern, which dictates when and what gets released. Native handles should always be released, and managed resources should be released only when the IDisposable.Dispose implementation is called. Simple, the disposable patter looks like this:
class MyClass : IDisposable
{
~MyClass()
{
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
} The NativeWindow class is used quite a bit in the BCL. I recommend using a disassembler (if you know how to read IL; if not, it's always a good idea to learn) like ildasm.exe that ships with the .NET Framework SDK, or a decompiler like .NET Reflector to see how it's used throughout the Framework Class Library.
[EDIT: Mistakenly typed "override" instead of "virtual". Fixed.]
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your reaction. Maybe I do not understand your proposal to solve this problem, as said I am just a C# beginner. You can find my changed code hereunder. If I try to rebuild it I get compiler errors:
'WindowsApplication1.GenericWindow.Dispose(bool)': no suitable method found to override
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
GenericWindow --- The base class 'System.Windows.Forms.NativeWindow' cannot be designed.
Any idea?
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class GenericWindow : NativeWindow, IDisposable
{
private delegate Int32 EnumChildProc(IntPtr hWnd, IntPtr lParam);
private const int GWL_WNDPROC = -4;
private const int WM_PAINT = 0x000F;
private IntPtr oldWndProc = IntPtr.Zero;
private Win32WndProc newWndProc = null;
private int hIcon;
private int XPos;
private int YPos;
#region Imported User32.DLL functions
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern int GetDC(int hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int ReleaseDC(int hWnd,int hDc);
[DllImport("User32.dll")]
private static extern int DestroyIcon(int hIcon);
[DllImport("user32.dll")]
public static extern int DrawIcon(int hdc, int x,int y, int hIcon);
[DllImport("user32.dll", EntryPoint="CallWindowProc")]
private static extern int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, Win32WndProc newProc);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newProc);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
#endregion
// A delegate that matches Win32 WNDPROC:
private delegate int Win32WndProc(IntPtr hWnd, int Msg, int wParam, int lParam);
public GenericWindow()
{
}
~GenericWindow()
{ MessageBox.Show("destr");
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources. Ex: NativeWindow.DestroyHandle
this.DestroyHandle();
}
public void NotifyWindow(int windowHandle)
{
this.AssignHandle((IntPtr)windowHandle);
oldWndProc = GetWindowLong((IntPtr)windowHandle,GWL_WNDPROC);
newWndProc = new Win32WndProc(MyWndProc);
SetWindowLong((IntPtr)windowHandle, GWL_WNDPROC,newWndProc);
PaintExtras(1,1);
}
private int MyWndProc(IntPtr hWnd, int Msg, int
wParam, int lParam)
{
int pHandle;
switch(Msg)
{
case WM_PAINT:
pHandle = CallWindowProc(oldWndProc, hWnd,Msg, wParam, lParam);
PaintExtras(XPos, YPos);
return pHandle;
default:
return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam);
}
}
private void PaintExtras(int xPos, int yPos)
{
XPos = xPos;
YPos = yPos;
int hdc = GetDC(this.Handle.ToInt32());
if (hdc != 0)
{
hIcon = ExtractIcon(this.Handle.ToInt32(),"C:\\progress10\\wrk\\experim\\down.ico",0);
int ret = DrawIcon (hdc, xPos, yPos, hIcon);
ret = DestroyIcon (hIcon);
ret = ReleaseDC(this.Handle.ToInt32(),hdc);
}
}
}
}
regards,
Stefan.
|
|
|
|
|
I'm sorry - that should be "virtual" not "override". I get so used to typing the latter.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you. The code compiles now, but I still get errors stating that files are in use on rebuilding after I closed the 4GL window. With spy++ I see as last received messages WM_ESTROY and WM_NCDESTROY.
I placed a debugging message in de decontructor ~GenericWindow(), it does not show up after the closingaction. Calling Dispose() from within my 4GL does not help either. Any idea how to get this working?
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class GenericWindow : NativeWindow, IDisposable
{
private delegate Int32 EnumChildProc(IntPtr hWnd, IntPtr lParam);
private const int GWL_WNDPROC = -4;
private const int WM_PAINT = 0x000F;
private IntPtr oldWndProc = IntPtr.Zero;
private Win32WndProc newWndProc = null;
private int hIcon;
private int XPos;
private int YPos;
#region Imported User32.DLL functions
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern int GetDC(int hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int ReleaseDC(int hWnd,int hDc);
[DllImport("User32.dll")]
private static extern int DestroyIcon(int hIcon);
[DllImport("user32.dll")]
public static extern int DrawIcon(int hdc, int x,int y, int hIcon);
[DllImport("user32.dll", EntryPoint="CallWindowProc")]
private static extern int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, Win32WndProc newProc);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newProc);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
#endregion
// A delegate that matches Win32 WNDPROC:
private delegate int Win32WndProc(IntPtr hWnd, int Msg, int wParam, int lParam);
public GenericWindow()
{
}
~GenericWindow()
{ MessageBox.Show("destr");
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources. Ex: NativeWindow.DestroyHandle
this.DestroyHandle();
}
public void NotifyWindow(int windowHandle)
{
this.AssignHandle((IntPtr)windowHandle);
oldWndProc = GetWindowLong((IntPtr)windowHandle,GWL_WNDPROC);
newWndProc = new Win32WndProc(MyWndProc);
SetWindowLong((IntPtr)windowHandle, GWL_WNDPROC,newWndProc);
PaintExtras(1,1);
}
private int MyWndProc(IntPtr hWnd, int Msg, int
wParam, int lParam)
{
int pHandle;
switch(Msg)
{
case WM_PAINT:
pHandle = CallWindowProc(oldWndProc, hWnd,Msg, wParam, lParam);
PaintExtras(XPos, YPos);
return pHandle;
default:
return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam);
}
}
public void setPos(int xPos, int yPos)
{
XPos = xPos;
YPos = yPos;
}
private void PaintExtras(int xPos, int yPos)
{
XPos = xPos;
YPos = yPos;
int hdc = GetDC(this.Handle.ToInt32());
if (hdc != 0)
{
hIcon = ExtractIcon(this.Handle.ToInt32(),"C:\\progress10\\wrk\\experim\\down.ico",0);
int ret = DrawIcon (hdc, xPos, yPos, hIcon);
ret = DestroyIcon (hIcon);
ret = ReleaseDC(this.Handle.ToInt32(),hdc);
}
}
}
}
regards,
Stefan.
|
|
|
|
|
Something else I noticed right off the bat:
[ClassInterface(ClassInterfaceType.AutoDual)] NEVER use this. Auto-generated class interfaces are the worst thing ever added to the Framework, in my opinion. If you know anything about COM, you should agree. While .NET isn't prone to problems caused my moving members around, native code (like the COM infrastructure) is. Always declare your class interfaces explicitly, always use a hard-coded, unchanging guid via the GuidAttribute on both your class and COM-exposed interfaces, and never change published interfaces. Derive new ones, implement those as the first interface of your class (which the CLR treats as your class interface when you use ClassInterfaceType.None with the ClassInterfaceAttribute ) successively when releasing new interfaces. This is how you'd do it in COM and need to replicate that behavior in .NET. While .NET doesn't care, COM sees a different story and clients will start breaking left and right. That is not the problem at hand, however.
If you are exposing this to COM and using your class, the make sure you're releasing the object by calling IUnknown::Release in your native code. If not, the DLL is not unloaded and will be in use (soft-locked).
And once again, you should not sub-class the Window how you're currently doing it. This is most likely causing the problem. Simply override WndProc (just like you'd do on any other Windows Forms control (since most encapsulates native Common Controls) when a particular message isn't handled in a .NET-friendly manner. Catch the WM_PAINT message like in the following and handle that however is necessary:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PAINT)
{
}
base.WndProc(ref m);
} I very much doubt the Framework is expecting you to sub-class the Windows by re-assigning its window procedure. Just don't. You're performing extra work - and putting extra work on the CPU for marshaling - when it's not necessary. The window procedure is already encapsulated and exposed for you: NativeWindow.WndProc .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks a lot for your extensive explication. I will google around in the coming time to get some code to make the release-call (and the rest) work, and look for some extra info on using com.
I hope I can get back to you if I do not (fully) succeed.
regards,
Stefan.
|
|
|
|
|
I have a dataTable tbl. If my app. start it's emty. I can fill it manually but i can also save the table of the last session. I do that with XmlWrite().
To load the table i use the XmlRead() method. After I done this tbl_ColumnChanged stops raising events although my Comlumns are changing.
I'm debugging for a while to find out the raison why it stops raising event but it has not been very succesfull till now. Can someone tell me why?
Dogan
|
|
|
|
|