Program that I create is use for recording the mouse and keyboard events. But when I create the part for saving the record and use for play it back later the error below occur.
public List<MacroEvent> events = new List<MacroEvent>();
Stream stream = File.Open("Macro.bin", FileMode.Create);
BinaryFormatter bin= new BinaryFormatter();
bin.Serialize(stream, events);
stream.Close();
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: Type 'System.Windows.Forms.MouseEventArgs' in Assembly "System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken = b77a5c561934e089" is not marked as serializable.
A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
StackTrace: ' at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at GlobalMacroRecorder.MacroForm.SaveMacro(Object sender, EventArgs e) in c:\Users\Cy\Desktop\Automated Setup Solution\AutomatedSetup\MacroForm.cs:line 465
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at GlobalMacroRecorder.Program.Main() in c:\Users\Cy\Desktop\Automated Setup Solution\AutomatedSetup\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()'
The thread 0x1ad0 has exited with code 0 (0x0).
[Serializable]
public class MacroEvent
{
public MacroEventType MacroEventType;
public EventArgs EventArgs;
public int TimeSinceLastEvent;
public MacroEvent(MacroEventType macroEventType, EventArgs eventArgs, int timeSinceLastEvent)
{
MacroEventType = macroEventType;
EventArgs = eventArgs;
TimeSinceLastEvent = timeSinceLastEvent;
}
}
You cannot do that, because the MouseEventArgs is not serializable. You will need to create a serializable class with properties (X,Y) similar to that of MouseEventArgs and then serialize that class to binary file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.