private void saveMacroToolStripMenuItem_Click(object sender, EventArgs e) { string path = string.Empty; using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "Macro File|*.bmr"; if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } path = sfd.FileName; } using (FileStream fs = new FileStream(path, FileMode.Create)) using (BinaryWriter br = new BinaryWriter(fs)) { br.Write(CurrentMacro.Count); foreach (IMacroEvent me in CurrentMacro) { Type eventType = me.GetType(); if (eventType == typeof(MacroEvent_Delay)) { MacroEvent_Delay dEvent = (MacroEvent_Delay)me; br.Write((byte)MacroEventType.Delay); br.Write(dEvent.DelayMS); } if (eventType == typeof(MacroEvent_Keyboard)) { MacroEvent_Keyboard kEvent = (MacroEvent_Keyboard)me; br.Write((byte)MacroEventType.Keyboard); br.Write((int)kEvent.EventType); br.Write(kEvent.Event.vKeyCode); br.Write(kEvent.Event.scanCode); br.Write(kEvent.Event.Flags); } if (eventType == typeof(MacroEvent_Mouse)) { MacroEvent_Mouse mEvent = (MacroEvent_Mouse)me; br.Write((byte)MacroEventType.Mouse); br.Write((int)mEvent.Button); br.Write(mEvent.Event.mouseData); br.Write(mEvent.Event.Location.X); br.Write(mEvent.Event.Location.Y); br.Write(mEvent.Event.Flags); } } } MessageBox.Show("Macro saved"); }
private IntPtr MouseEventCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (!(nCode < 0)) { lock (this) { eMouseButton button = (eMouseButton)wParam; if (button == eMouseButton.MOVE && !AcceptMouseMovement && mouseDelayEnabled) { return(WinApi.CallNextHookEx(MouseHook, nCode, wParam, lParam)); } MouseEvent eventData = (MouseEvent)Marshal.PtrToStructure(lParam, typeof(MouseEvent)); DateTime cTime = DateTime.Now; int delay = (cTime - delayBegin).Milliseconds; MacroEvent_Delay dEvent = new MacroEvent_Delay(delay); MacroEvent_Mouse mEvent = new MacroEvent_Mouse(eventData, button); if (PopulateInternalEventList) { EventList.Add(dEvent); EventList.Add(mEvent); } if (OnEvent != null) { OnEvent(dEvent); OnEvent(mEvent); } AcceptMouseMovement = false; delayBegin = cTime; } } return(WinApi.CallNextHookEx(MouseHook, nCode, wParam, lParam)); }
private void loadAMacroToolStripMenuItem_Click(object sender, EventArgs e) { string filePath = string.Empty; using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "Macro File|*.bmr"; if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } filePath = ofd.FileName; } try { IMacroEvent[] loadedEvents = null; using (FileStream fs = new FileStream(filePath, FileMode.Open)) using (BinaryReader br = new BinaryReader(fs)) { loadedEvents = new IMacroEvent[br.ReadInt32()]; int eventIndex = 0; while (fs.Position != fs.Length) { MacroEventType mType = (MacroEventType)br.ReadByte(); if (mType == MacroEventType.Delay) { loadedEvents[eventIndex] = new MacroEvent_Delay(br.ReadInt32()); } if (mType == MacroEventType.Keyboard) { eKeyboardEvent kbEvent = (eKeyboardEvent)br.ReadInt32(); KeyboardEvent eventData = new KeyboardEvent(); eventData.vKeyCode = br.ReadUInt32(); eventData.scanCode = br.ReadUInt32(); eventData.Flags = br.ReadUInt32(); eventData.Time = 0; eventData.dwExtraInfo = WinApi.GetMessageExtraInfo(); loadedEvents[eventIndex] = new MacroEvent_Keyboard(kbEvent, eventData); } if (mType == MacroEventType.Mouse) { eMouseButton mButton = (eMouseButton)br.ReadInt32(); MouseEvent mEvent = new MouseEvent(); mEvent.mouseData = br.ReadUInt32(); mEvent.Location = new mPoint(); mEvent.Location.X = br.ReadInt32(); mEvent.Location.Y = br.ReadInt32(); mEvent.Flags = br.ReadUInt32(); loadedEvents[eventIndex] = new MacroEvent_Mouse(mEvent, mButton); } eventIndex++; } } ClearCurrent(); foreach (IMacroEvent lEvent in loadedEvents) { AddEvent(lEvent); } MessageBox.Show("Loaded successfully"); } catch { MessageBox.Show("Failed to load macro."); } }