Well, I made a script to just jump to work in several games, and I would like my program to have a button to activate and deactivate it so that it doesn't disturb me when I'm not playing (when minimizing the game), I managed to make the script run when I clicked on the activate button, but I don't have the slightest idea of how to put it to disable / pause on the "Desativar" button, and when I click again on "Activate" where it left off.
I'm going to apologize for my English if something is wrong
ps: I'm a newbie
Code:
public partial class Pular : Form
{
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Keys vKey);
[DllImport("user32.dll")]
static extern void mouse_event(int a, int b, int c, int d, int swed);
int m3DOWN = 0x0020;
int m3UP = 0x0040;
public Pular()
{
InitializeComponent();
btnDesativar.Enabled = false;
}
private void Pular_Load(object sender, EventArgs e)
{
}
void jump()
{
while (true)
{
if (GetAsyncKeyState(Keys.Space) <0)
{
mouse_event(m3DOWN, 0, 0, 0, 0);
Thread.Sleep(9);
mouse_event(m3UP, 0, 0, 0, 0);
Thread.Sleep(9);
}
Thread.Sleep(10);
}
}
private void btnAtivar_Click(object sender, EventArgs e)
{
btnAtivar.Enabled = false;
btnDesativar.Enabled = true;
Thread jp = new Thread(jump) { IsBackground = true };
jp.Start();
}
private void btnDesativar_Click(object sender, EventArgs e)
{
btnDesativar.Enabled = false;
btnAtivar.Enabled = true;
}
Related
Currently doing a WPF app where you will be able to open apps suchs as IE and file explorer (more to come), and when you start it, a button is created and put in a custom appbar/taskbar.
So to the problem, we are using Process.Start() to start the apps, so we are currently checking this
this.ProgramProcess.EnableRaisingEvents = true;
this.ProgramProcess.Exited += (sender, e) => { RemoveRunningProgram((Process)sender); };
and the RemoveRunningProgram method looks like this
private void RemoveRunningProgram(Process process)
{
App.Current.Dispatcher.Invoke((Action)delegate { TaskbarWindow.RunningPrograms.Remove(this); });
}
So the main thought is, if you close a window by the user pressing the X button in the top right corner to close the window, the appbar button/icon will disappear.
So couple of the problems;
If you open 1 IE browser, it works fine, but if you open more than one, you will have to close all windows, for all the icons to disappear.
It takes a LONG time for the computer to see if the file explorer is closed or not, sometimes not even notice it.
Am I thinking at the right spot where I should make adjustments?
RunningProgram.cs:
public class RunningProgram : Button
{
private IntPtr ProgramHandle { get; set; }
private string ProgramName { get; set; }
public Process ProgramProcess { get; set; }
public List<Process> procList = new List<Process>();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
public RunningProgram(IntPtr programHandle, string programName, Thickness margin, string imagePath, Process programProcess)
{
this.ProgramHandle = programHandle;
this.ProgramName = programName;
this.Margin = margin;
this.Background = SetButtonBackground(imagePath);
this.Width = 70;
this.Height = 70;
this.Click += new RoutedEventHandler(ShowWindow_Click);
this.BorderBrush = Brushes.Transparent;
this.ProgramProcess = programProcess;
this.procList.Add(ProgramProcess);
this.ProgramProcess.EnableRaisingEvents = true;
this.ProgramProcess.Exited += (sender, e) => { RemoveRunningProgram((Process)sender); };
}
private void p_Exited(object sender, EventArgs e)
{
RemoveRunningProgram((Process)sender);
}
private void RemoveRunningProgram(Process process)
{
App.Current.Dispatcher.Invoke((Action)delegate { TaskbarWindow.RunningPrograms.Remove(this); });
}
private void ShowWindow_Click(object sender, RoutedEventArgs e)
{
ShowWindow(ProgramHandle, 3);
SetForegroundWindow(ProgramHandle);
GetForegroundWindow();
}
}
Thanks in advance
I am coding on an Auto Clicker in C# but I have two problems. Firstly, every time I start the program and click 'Start' it works but I cannot stop it. The program just not responses anymore. Secondly, when I try to press the 'F7' button, it should automatically start and stop, but it doesn't.
Can anybody help me and btw I am a bit newer to C#.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoClicker
{
public partial class Form1 : Form
{
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
public int zahl;
public bool isPressed = false;
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Keys vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
Thread CL = new Thread(clicker);
CL.Start();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
zahl = Convert.ToInt32(Math.Round(numericUpDown1.Value, 0));
}
private void clicker(){
while (true) {
if (isPressed == true)
{
mouse_event(dwFlags: MOUSEEVENTF_LEFTUP, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
Thread.Sleep(1);
mouse_event(dwFlags: MOUSEEVENTF_LEFTDOWN, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
Thread.Sleep(zahl);
}
if (isPressed != true)
{
break;
}
Thread.Sleep(2);
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
isPressed = true;
buttonStart.Enabled = false;
buttonStop.Enabled = true;
clicker();
}
private void buttonStop_Click(object sender, EventArgs e)
{
isPressed = false;
buttonStart.Enabled = true;
buttonStop.Enabled = false;
clicker();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
if (buttonStart.Enabled == true)
{
if (GetAsyncKeyState(Keys.F7) < 0)
{
isPressed = false;
}
}else if(buttonStart.Enabled != true)
{
if (GetAsyncKeyState(Keys.F7) < 0)
{
isPressed = false;
}
}
Thread.Sleep(1);
}
}
}
}
I want to make an autoclicker that will click when i press "F" button; But each time I try to press "F" I get invoke error.
I tried to run Click thread in another void but it didn't work.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
UserActivityHook actHook;
static bool autoclickerToggle = false;
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public Form1()
{
InitializeComponent();
actHook = new UserActivityHook(); // crate an instance with global hooks
// hang on events
actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);
}
public void MouseMoved(object sender, MouseEventArgs e) { }
public void MyKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F)
{
AutoclickerToggle();
}
}
public void MyKeyPress(object sender, KeyPressEventArgs e) { }
void AutoclickerToggle()
{
if (autoclickerToggle)
{
autoclickerToggle = false;
}
else
{
autoclickerToggle = true;
Thread Click = new Thread(() => Clicker());
Click.Start();
}
}
public void MyKeyUp(object sender, KeyEventArgs e) { }
public void Clicker()
{
while (autoclickerToggle)
{
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Thread.Sleep(100);
}
}
As I said I've got this error when pressing "F" (used translator there, because I had this error in polish language) "Calling PInvoke 'Test! Test.Form1 :: mouse_event 'has upset the balance of the stack. The likely cause is a mismatch between the managed PInvoke signature and the unmanaged target signature. Verify that the called convention and signature parameters of the PInvoke function match the unmanaged destination signature"
When dragging a borderless form to the top of the screen; if the Y coordinate is negative, it sets it back to 0. What I'm looking to do is be able to drag the form above the top, where the Y coordinate would be negative, the same way you can with every other side of the screen.
Here is what I have tried:
public partial class BorderlessForm : Form
{
public BorderlessForm()
{
InitializeComponent();
}
private bool _isNegative;
private Point _negative;
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
protected override void OnResizeEnd(EventArgs e)
{
if (_isNegative) {
Location = _negative;
}
//base.OnResizeEnd(e);
}
protected override void OnMove(EventArgs e)
{
if (Location.Y < 0) {
_isNegative = true;
_negative = Location;
}
else {
_isNegative = false;
}
//base.OnMove(e);
}
}
This was the best I could come up with after thinking on it for a while. The problem is that when the mouse is released and the form is finished moving, OnMove is called before OnResizeEnd, and _isNegative is then set back to false. At least, that is what I assume is happening.
Do I have the right idea, or is there some better way to go about this?
So, I thought more about what isaeid said, and was able to come up with a solution. Still not sure if this is the best way to go about it, but here it is:
public partial class ImageForm : PerPixelAlphaForm
{
public ImageForm()
{
InitializeComponent();
}
private bool _isNegative;
private Point _negative;
private bool _flag;
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
NativeMethods.ReleaseCapture();
NativeMethods.SendMessage(Handle, NativeMethods.WM_NCLBUTTONDOWN, NativeMethods.HT_CAPTION, 0);
}
}
protected override void OnResizeEnd(EventArgs e)
{
if (_isNegative) {
Location = _negative;
_isNegative = false;
}
}
protected override void OnMove(EventArgs e)
{
if (Location.Y < 0) {
_isNegative = true;
_flag = true;
_negative = Location;
}
else {
if (_flag) {
_flag = false;
return;
}
_isNegative = false;
}
}
}
You can change some events like this:
protected override void OnResizeEnd(EventArgs e)
{
if (_isNegative)
{
Location = _negative;
}
oldRight = this.Right;
oldBottom = this.Bottom;
//base.OnResizeEnd(e);
}
protected override void OnMove(EventArgs e)
{
if ( this.Right == oldRight || this.Bottom == oldBottom)
return;
if (Location.Y < 0)
{
_isNegative = true;
_negative = Location;
}
else
{
_isNegative = false;
}
//base.OnMove(e);
}
When top or left of window is changed, dotnet determines location of window is changed and calls onmove event, i consider if right or bottom of the window is not changed so window's location is not changed.
Add this codes too:
int oldBottom, oldRight;
private void BorderlessForm_Load(object sender, EventArgs e)
{
oldRight = this.Right;
oldBottom = this.Bottom;
}
I have a program WFA that also has and command Window. I open the window with AllocConsole(); When I close the console window, I use FreeConsole(); but when I open it again with AllocConsole(); I wanna write and read from it and it throws an exeption.
The code:
namespace WindowsFormsApplication2
{
class classx
{
[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
[DllImport("kernel32")]
public static extern bool AttachConsole();
[DllImport("kernel32")]
public static extern bool GetConsoleWindow();
public static bool z = false;
[DllImport("kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add);
public delegate bool HandlerRoutine(uint dwControlType);
}
public partial class Form1 : Form
{
NotifyIcon icontask;
Icon iconone_active;
Icon iconone_inactive;
/*Icon icontwo;
Icon iconthree;
Icon iconfour;
Icon iconfive;*/
Thread Threadworkermy;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
iconone_active = new Icon(".../iconone_active.ico");
iconone_inactive = new Icon(".../iconone_inactive.ico");
icontask = new NotifyIcon();
icontask.Icon = iconone_active;
icontask.Visible = true;
Threadworkermy = new Thread(new ThreadStart(checkActivityThread));
Threadworkermy.Start();
MenuItem Nameapp = new MenuItem("xr");
MenuItem quitappitem = new MenuItem("quit program");
MenuItem OpenGUI = new MenuItem("Open GUI");
MenuItem Advancedmodewindow = new MenuItem("x");
ContextMenu contextmenu = new ContextMenu();
quitappitem.Click += quitappitem_click;
OpenGUI.Click += OpenGUI_click;
Advancedmodewindow.Click += Advancedmodewindow_click;
contextmenu.MenuItems.Add(Nameapp);
contextmenu.MenuItems[0].Enabled = false;
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(OpenGUI);
contextmenu.MenuItems.Add(Advancedmodewindow);
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(quitappitem);
icontask.ContextMenu = contextmenu;
icontask.Icon = iconone_active;
icontask.Visible = true;
}
private void Advancedmodewindow_click(object sender, EventArgs e)
{
classx.AllocConsole();
Console.WriteLine("X");
classx.FreeConsole();
}
private void OpenGUI_click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
}
private void quitappitem_click(object sender, EventArgs e)
{
Threadworkermy.Abort();
icontask.Dispose();
this.Close();
}
public void checkActivityThread()
{
try
{
while(true)
{
Thread.Sleep(100);
}
} catch(ThreadAbortException tbe)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
}
}
Exception that it throws out 'System.IO.IOException' in mscorlib.dll
Additional information: The handle is invalid.
To those who will be saying to change the type, I can't. (it needs to be WFA application)
there seems to be an issue with destroying the consolewindow, so you could just hide it.
For hiding the window you need an additional DllImport from user32.dll and change the returnvalue of GetConsoleWindow to IntPtr:
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
Now check if a console-handle already exists. If it does show the console otherwise create the consolewindow:
private void Advancedmodewindow_click(object sender, EventArgs e)
{
IntPtr handle = classx.GetConsoleWindow();
if (handle == IntPtr.Zero)
{
classx.AllocConsole();
handle = classx.GetConsoleWindow();
}
else
{
//shows the window with the given handle
classx.ShowWindow(handle, 8);
}
Console.WriteLine("X");
//hides the window with the given handle
classx.ShowWindow(handle, 0);
}
The original solution can be found here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/cdee5d88-3325-47ce-9f6b-83aa4447f8ca/console-exception-on-windows-8?forum=clr