This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
CC-Functions/W32.Test/MainForm.cs

423 lines
16 KiB
C#
Raw Normal View History

2020-05-25 19:32:53 +02:00
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using CC_Functions.W32.DCDrawer;
2020-04-04 16:45:45 +02:00
using CC_Functions.W32.Forms;
2019-12-22 16:58:16 +01:00
using CC_Functions.W32.Hooks;
2019-09-07 10:12:24 +02:00
using static CC_Functions.W32.Power;
namespace CC_Functions.W32.Test
{
2020-01-15 20:41:52 +01:00
public partial class MainForm : Form
2019-09-07 10:12:24 +02:00
{
2020-04-04 12:05:42 +02:00
private static Wnd32 _tmpWnd32Obj;
private static MainForm _mainF;
private static Wnd32 _wndSelectMouseCurrent;
2020-04-04 12:05:42 +02:00
private readonly KeyboardHook _kHook;
private readonly MouseHook _mHook;
private readonly Label[] _readerLabels;
2020-01-15 20:41:52 +01:00
public MainForm()
2019-09-07 10:12:24 +02:00
{
2020-04-04 12:05:42 +02:00
_wndSelectMouseCurrent = this.GetWnd32();
2019-09-07 10:12:24 +02:00
InitializeComponent();
2020-04-04 12:05:42 +02:00
_mainF = this;
_tmpWnd32Obj = Wnd32.FromForm(this);
2019-09-07 10:12:24 +02:00
#if DEBUG
2020-05-25 19:32:53 +02:00
_tmpWnd32Obj.Overlay = true;
2019-09-07 10:12:24 +02:00
#endif
set_up_box(power_mode_box, typeof(ShutdownMode));
set_up_box(power_reason_box, typeof(ShutdownReason));
set_up_box(power_mod_box, typeof(ShutdownMod));
2020-04-04 12:05:42 +02:00
_mHook = new MouseHook();
_kHook = new KeyboardHook();
2019-09-07 10:12:24 +02:00
wnd_action_pos_x_bar.Maximum = Screen.PrimaryScreen.Bounds.Width;
wnd_action_pos_y_bar.Maximum = Screen.PrimaryScreen.Bounds.Height;
wnd_action_pos_w_bar.Maximum = Screen.PrimaryScreen.Bounds.Width;
wnd_action_pos_h_bar.Maximum = Screen.PrimaryScreen.Bounds.Height;
2019-09-11 11:22:16 +02:00
wnd_action_style.DataSource = Enum.GetValues(typeof(FormWindowState));
2020-04-04 12:05:42 +02:00
wnd_action_style.SelectedItem = _tmpWnd32Obj.State;
_readerLabels = Enum.GetValues(typeof(Keys)).OfType<Keys>().OrderBy(s => s.ToString()).Select(s =>
2020-01-15 20:41:52 +01:00
{
2020-01-16 19:50:31 +01:00
Label lab = new Label {Tag = s};
2020-01-15 20:41:52 +01:00
readerFlow.Controls.Add(lab);
return lab;
}).ToArray();
Wnd_action_title_get_Click(null, null);
desk_get_Click(null, null);
screen_get_Click(null, null);
time_select.Value = DateTime.Now;
2019-09-07 10:12:24 +02:00
}
2020-04-04 12:05:42 +02:00
private Wnd32 TmpWnd
2020-01-16 19:50:31 +01:00
{
2020-04-04 12:05:42 +02:00
get => _tmpWnd32Obj;
2020-01-16 19:50:31 +01:00
set
{
2020-04-04 12:05:42 +02:00
_tmpWnd32Obj = value;
2020-01-16 19:50:31 +01:00
Wnd_action_title_get_Click(null, null);
}
}
2020-04-04 12:05:42 +02:00
private static void set_up_box(ComboBox box, Type enumT)
2019-09-07 10:12:24 +02:00
{
box.DataSource = Enum.GetNames(enumT);
2019-12-22 16:58:16 +01:00
Array tmp = Enum.GetValues(enumT);
2019-09-07 10:12:24 +02:00
box.Tag = new object[tmp.Length];
2019-12-22 16:58:16 +01:00
int i = 0;
foreach (object o in tmp)
2019-09-07 10:12:24 +02:00
{
((object[]) box.Tag)[i] = o;
2019-09-07 10:12:24 +02:00
i++;
}
}
2020-04-04 12:05:42 +02:00
private static object get_box_value(ListControl box) => ((object[]) box.Tag)[box.SelectedIndex];
2019-09-07 10:12:24 +02:00
2020-01-16 19:50:31 +01:00
private void Power_execute_Click(object sender, EventArgs e) => RaiseEvent(
(ShutdownMode) get_box_value(power_mode_box), (ShutdownReason) get_box_value(power_reason_box),
(ShutdownMod) get_box_value(power_mod_box));
2019-09-07 10:12:24 +02:00
2020-04-04 12:05:42 +02:00
private void Wnd_select_self_Click(object sender, EventArgs e) => TmpWnd = Wnd32.FromForm(this);
2019-09-07 10:12:24 +02:00
2020-01-16 19:50:31 +01:00
private void wnd_select_list_Click(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd = SelectBox.Show(Wnd32.Visible, "Please select a window") ?? TmpWnd;
2020-04-14 14:49:22 +02:00
private void wnd_select_parent_Click(object sender, EventArgs e) => TmpWnd = TmpWnd.Parent;
private void wnd_select_child_Click(object sender, EventArgs e)
{
if (TmpWnd.HWnd != Handle && !this.GetWnd32().Children.Contains(TmpWnd))
{
WindowState = FormWindowState.Minimized;
TmpWnd.Enabled = false;
}
Wnd32.FromForm(this).IsForeground = true;
Form frm = new Form
{
BackColor = Color.White,
Opacity = 0.4f,
FormBorderStyle = FormBorderStyle.None,
WindowState = FormWindowState.Maximized
};
Label lab = new Label {AutoSize = true};
2020-04-14 14:49:22 +02:00
frm.Controls.Add(lab);
Panel pan = new Panel {BackColor = Color.Red};
2020-04-14 14:49:22 +02:00
frm.Controls.Add(pan);
Wnd32[] children = TmpWnd.Children;
void UpdateGui(Point labelPosition)
2020-04-14 14:49:22 +02:00
{
lab.Text = $"{_wndSelectMouseCurrent.Title} ({_wndSelectMouseCurrent.HWnd})";
lab.Location = new Point(labelPosition.X + 5, labelPosition.Y + 5);
pan.Bounds = _wndSelectMouseCurrent.Position;
}
void MouseEventHandler(object sender1, MouseEventArgs e1)
{
Func<Wnd32, bool> checkWnd = s => s.Position.Contains(e1.Location);
2020-04-14 14:49:22 +02:00
if (children.Any(checkWnd))
{
_wndSelectMouseCurrent = children.First(checkWnd);
UpdateGui(Cursor.Position);
2020-04-14 14:49:22 +02:00
}
}
void EventHandler(object sender1, EventArgs e1)
{
frm.Hide();
frm.WindowState = FormWindowState.Minimized;
Wnd32 tmp = _wndSelectMouseCurrent;
TmpWnd.Enabled = true;
TmpWnd.IsForeground = true;
TmpWnd = tmp;
_mainF.WindowState = FormWindowState.Normal;
frm.Close();
}
void KeyEventHandler(object sender1, KeyEventArgs e1)
{
int tmp;
switch (e1.KeyCode)
{
case Keys.Escape:
frm.Close();
break;
case Keys.Up:
case Keys.Left:
tmp = Array.IndexOf(children, _wndSelectMouseCurrent);
if (tmp == 0)
tmp = children.Length;
tmp--;
_wndSelectMouseCurrent = children[tmp];
UpdateGui(_wndSelectMouseCurrent.Position.Location);
2020-04-14 14:49:22 +02:00
break;
case Keys.Down:
case Keys.Right:
tmp = Array.IndexOf(children, _wndSelectMouseCurrent);
tmp++;
if (tmp == children.Length)
tmp = 0;
_wndSelectMouseCurrent = children[tmp];
UpdateGui(_wndSelectMouseCurrent.Position.Location);
2020-04-14 14:49:22 +02:00
break;
}
}
frm.Click += EventHandler;
pan.Click += EventHandler;
lab.Click += EventHandler;
frm.MouseMove += MouseEventHandler;
pan.MouseMove += MouseEventHandler;
lab.MouseMove += MouseEventHandler;
frm.KeyDown += KeyEventHandler;
UpdateGui(Cursor.Position);
2020-04-14 14:49:22 +02:00
frm.Show();
_wndSelectMouseCurrent = frm.GetWnd32();
Cursor.Position = Cursor.Position;
frm.GetWnd32().Overlay = true;
}
2020-04-04 12:22:34 +02:00
2020-01-16 19:50:31 +01:00
private void Wnd_select_title_button_Click(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd = Wnd32.FromMetadata(null, wnd_select_title_box.Text);
2019-09-07 10:12:24 +02:00
2020-04-04 12:05:42 +02:00
private void Wnd_select_class_button_Click(object sender, EventArgs e) =>
TmpWnd = Wnd32.FromMetadata(wnd_select_class_box.Text);
2019-09-07 10:12:24 +02:00
2020-04-04 12:05:42 +02:00
private void Wnd_action_title_set_Click(object sender, EventArgs e) => TmpWnd.Title = wnd_select_title_box.Text;
2019-09-07 10:12:24 +02:00
private void Wnd_action_title_get_Click(object sender, EventArgs e)
{
2020-04-04 12:05:42 +02:00
if (!TmpWnd.StillExists)
TmpWnd = Wnd32.FromForm(this);
wnd_select_title_box.Text = TmpWnd.Title;
wnd_action_enabled.Checked = TmpWnd.Enabled;
wnd_select_selected.Text = $"Selected: {TmpWnd.HWnd}";
wnd_action_style.SelectedIndex = (int) TmpWnd.State;
wnd_select_class_box.Text = TmpWnd.ClassName;
wnd_action_visible.Checked = TmpWnd.Shown;
try
{
2020-04-04 12:05:42 +02:00
wnd_action_icon.BackgroundImage = TmpWnd.Icon.ToBitmap();
}
catch
{
wnd_action_icon.BackgroundImage = null;
}
try
{
2020-04-04 12:05:42 +02:00
wnd_action_pos_x_bar.Value = TmpWnd.Position.X;
}
catch
{
2020-04-04 12:05:42 +02:00
// ignored
}
try
{
2020-04-04 12:05:42 +02:00
wnd_action_pos_y_bar.Value = TmpWnd.Position.Y;
}
catch
{
2020-04-04 12:05:42 +02:00
// ignored
}
try
{
2020-04-04 12:05:42 +02:00
wnd_action_pos_w_bar.Value = TmpWnd.Position.Width;
}
catch
{
2020-04-04 12:05:42 +02:00
// ignored
}
try
{
2020-04-04 12:05:42 +02:00
wnd_action_pos_h_bar.Value = TmpWnd.Position.Height;
}
catch
{
2020-04-04 12:05:42 +02:00
// ignored
}
2020-04-14 14:49:22 +02:00
try
{
wnd_select_parent.Tag = TmpWnd.Parent;
wnd_select_parent.Enabled = true;
}
catch
{
wnd_select_parent.Enabled = false;
}
2019-09-07 10:12:24 +02:00
}
2019-12-22 16:58:16 +01:00
private void Wnd_action_enabled_CheckedChanged(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd.Enabled = wnd_action_enabled.Checked;
2019-09-11 11:22:16 +02:00
2019-12-22 16:58:16 +01:00
private void Wnd_action_visible_CheckedChanged(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd.Shown = wnd_action_visible.Checked;
2019-09-07 10:12:24 +02:00
2020-04-04 12:05:42 +02:00
private void Wnd_action_front_Click(object sender, EventArgs e) => TmpWnd.IsForeground = true;
2019-09-07 10:12:24 +02:00
2020-04-04 12:05:42 +02:00
private void Wnd_action_destroy_Click(object sender, EventArgs e) => TmpWnd.Destroy();
2019-09-07 10:12:24 +02:00
private void Wnd_select_mouse_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
2020-04-04 12:05:42 +02:00
TmpWnd = Wnd32.FromForm(this);
TmpWnd.Enabled = false;
Form frm = new Form
{
BackColor = Color.White,
Opacity = 0.4f,
FormBorderStyle = FormBorderStyle.None,
WindowState = FormWindowState.Maximized
};
Label lab = new Label {AutoSize = true};
2019-09-07 10:12:24 +02:00
frm.Controls.Add(lab);
2020-04-04 12:05:42 +02:00
Panel pan = new Panel {BackColor = Color.Red};
frm.Controls.Add(pan);
2019-09-07 10:12:24 +02:00
void UpdateGui(Point labelPosition)
2020-04-04 12:05:42 +02:00
{
lab.Text = $"{_wndSelectMouseCurrent.Title} ({_wndSelectMouseCurrent.HWnd})";
2020-04-14 14:49:22 +02:00
lab.Location = new Point(labelPosition.X + 5, labelPosition.Y + 5);
2020-04-04 12:05:42 +02:00
pan.Bounds = _wndSelectMouseCurrent.Position;
}
2019-09-07 10:12:24 +02:00
2020-04-14 14:49:22 +02:00
void MouseEventHandler(object sender1, MouseEventArgs e1)
{
_wndSelectMouseCurrent = Wnd32.AllFromPoint(MousePosition, true).First(s => s != frm.GetWnd32());
UpdateGui(Cursor.Position);
2020-04-14 14:49:22 +02:00
}
2020-04-04 12:05:42 +02:00
void EventHandler(object sender1, EventArgs e1)
{
frm.Hide();
frm.WindowState = FormWindowState.Minimized;
Wnd32 tmp = _wndSelectMouseCurrent;
TmpWnd.Enabled = true;
TmpWnd.IsForeground = true;
TmpWnd = tmp;
_mainF.WindowState = FormWindowState.Normal;
frm.Close();
}
2020-04-14 14:49:22 +02:00
void KeyEventHandler(object sender1, KeyEventArgs e1)
{
if (e1.KeyCode == Keys.Escape)
frm.Close();
}
2020-04-04 12:05:42 +02:00
frm.Click += EventHandler;
pan.Click += EventHandler;
lab.Click += EventHandler;
frm.MouseMove += MouseEventHandler;
pan.MouseMove += MouseEventHandler;
lab.MouseMove += MouseEventHandler;
2020-04-14 14:49:22 +02:00
frm.KeyDown += KeyEventHandler;
2020-04-04 12:05:42 +02:00
frm.Show();
_wndSelectMouseCurrent = frm.GetWnd32();
Cursor.Position = Cursor.Position;
frm.GetWnd32().Overlay = true;
2019-09-07 10:12:24 +02:00
}
private void Mouse_enabled_CheckedChanged(object sender, EventArgs e)
{
if (mouse_enabled.Checked)
2020-04-04 12:05:42 +02:00
_mHook.OnMouse += MHook_OnMouse;
2019-09-07 10:12:24 +02:00
else
2020-04-04 12:05:42 +02:00
_mHook.OnMouse -= MHook_OnMouse;
2019-09-07 10:12:24 +02:00
}
2019-12-22 16:58:16 +01:00
private void MHook_OnMouse(MouseHookEventArgs args) =>
2020-04-04 12:05:42 +02:00
mouse_log.Text = $"{args.Message} -|- {args.Point}\r\n{mouse_log.Text}";
2019-09-07 10:12:24 +02:00
2020-01-16 19:50:31 +01:00
private void Mouse_log_TextChanged(object sender, EventArgs e) =>
mouse_log.Lines = mouse_log.Lines.Take(9).ToArray();
2019-09-07 10:12:24 +02:00
private void Keyboard_enabled_CheckedChanged(object sender, EventArgs e)
{
if (keyboard_enabled.Checked)
2020-04-04 12:05:42 +02:00
_kHook.OnKeyPress += KHook_OnKeyPress;
2019-09-07 10:12:24 +02:00
else
2020-04-04 12:05:42 +02:00
_kHook.OnKeyPress -= KHook_OnKeyPress;
2019-09-07 10:12:24 +02:00
}
2019-12-22 16:58:16 +01:00
private void KHook_OnKeyPress(KeyboardHookEventArgs args) =>
2020-04-04 12:05:42 +02:00
keyboard_log.Text = $"{args.Key}\r\n{keyboard_log.Text}";
2019-09-07 10:12:24 +02:00
2020-01-16 19:50:31 +01:00
private void Keyboard_log_TextChanged(object sender, EventArgs e) =>
keyboard_log.Lines = keyboard_log.Lines.Take(8).ToArray();
2019-09-07 10:12:24 +02:00
2019-12-22 16:58:16 +01:00
private void Wnd_action_pos_Click(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd.Position = new Rectangle(wnd_action_pos_x_bar.Value, wnd_action_pos_y_bar.Value,
wnd_action_pos_w_bar.Value, wnd_action_pos_h_bar.Value);
2019-09-07 10:12:24 +02:00
2020-01-15 20:41:52 +01:00
private void Wnd_action_style_SelectedIndexChanged(object sender, EventArgs e)
2019-09-07 10:12:24 +02:00
{
2020-01-15 20:41:52 +01:00
Enum.TryParse(wnd_action_style.SelectedValue.ToString(), out FormWindowState status);
2020-04-04 12:05:42 +02:00
TmpWnd.State = status;
2019-09-07 10:12:24 +02:00
}
2020-01-15 20:41:52 +01:00
private void wnd_action_overlay_CheckedChanged(object sender, EventArgs e) =>
2020-04-04 12:05:42 +02:00
TmpWnd.Overlay = wnd_action_overlay.Checked;
2020-01-15 20:41:52 +01:00
private void readerUpdate_Tick(object sender, EventArgs e)
2019-09-07 10:12:24 +02:00
{
2020-04-04 12:05:42 +02:00
foreach (Label s in _readerLabels)
2020-01-15 20:41:52 +01:00
{
2020-01-16 19:50:31 +01:00
Keys key = (Keys) s.Tag;
2020-01-15 20:41:52 +01:00
s.Text = $"{key.ToString()}: {key.IsDown()}";
}
2019-09-07 10:12:24 +02:00
}
2020-01-15 20:41:52 +01:00
private void desk_get_Click(object sender, EventArgs e) => desk_back.BackgroundImage = DeskMan.Wallpaper;
private void desk_draw_Click(object sender, EventArgs e)
2019-09-07 10:12:24 +02:00
{
2020-01-15 20:41:52 +01:00
using IDCDrawer drawer = DeskMan.CreateGraphics();
Graphics g = drawer.Graphics;
Pen eye = new Pen(new SolidBrush(Color.Red), 2);
2020-04-04 12:05:42 +02:00
g.DrawCurve(eye, new[] {MakePoint(20, 50), MakePoint(50, 65), MakePoint(80, 50)});
g.DrawCurve(eye, new[] {MakePoint(20, 50), MakePoint(50, 35), MakePoint(80, 50)});
2020-01-16 19:50:31 +01:00
g.DrawEllipse(eye,
2020-04-04 12:05:42 +02:00
new RectangleF(PointF.Subtract(MakePoint(50, 50), MakeSizeY(15, 15)), MakeSizeY(30, 30)));
2019-09-07 10:12:24 +02:00
}
2020-04-04 12:05:42 +02:00
private static PointF MakePoint(float xPercent, float yPercent) => new PointF(
Screen.PrimaryScreen.Bounds.Width * xPercent / 100,
Screen.PrimaryScreen.Bounds.Height * yPercent / 100);
2019-09-11 11:22:16 +02:00
2020-04-04 12:05:42 +02:00
private static SizeF MakeSizeY(float xPercent, float yPercent) => new SizeF(
Screen.PrimaryScreen.Bounds.Height * xPercent / 100,
Screen.PrimaryScreen.Bounds.Height * yPercent / 100);
2020-01-15 20:41:52 +01:00
private void desk_set_Click(object sender, EventArgs e)
2019-09-11 11:22:16 +02:00
{
2020-01-15 20:41:52 +01:00
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.jpg, *.jpeg, *.jpe, *.jfif, *.png)|*.jpg;*.jpeg;*.jpe;*.jfif;*.png";
2020-01-16 19:50:31 +01:00
if (dlg.ShowDialog() == DialogResult.OK) DeskMan.Wallpaper = Image.FromFile(dlg.FileName);
2019-09-11 11:22:16 +02:00
}
2019-11-29 21:27:24 +01:00
2020-01-16 19:50:31 +01:00
private void screen_get_Click(object sender, EventArgs e) =>
screen_img.BackgroundImage = ScreenMan.CaptureScreen();
2020-01-15 20:41:52 +01:00
private void screen_draw_Click(object sender, EventArgs e)
{
using IDCDrawer drawer = ScreenMan.GetDrawer(false);
Graphics g = drawer.Graphics;
Pen eye = new Pen(new SolidBrush(Color.Red), 2);
2020-04-04 12:05:42 +02:00
g.DrawCurve(eye, new[] {MakePoint(20, 50), MakePoint(50, 65), MakePoint(80, 50)});
g.DrawCurve(eye, new[] {MakePoint(20, 50), MakePoint(50, 35), MakePoint(80, 50)});
2020-01-16 19:50:31 +01:00
g.DrawEllipse(eye,
2020-04-04 12:05:42 +02:00
new RectangleF(PointF.Subtract(MakePoint(50, 50), MakeSizeY(15, 15)), MakeSizeY(30, 30)));
2020-01-15 20:41:52 +01:00
}
private void time_set_Click(object sender, EventArgs e) => Time.Set(time_select.Value);
2019-09-07 10:12:24 +02:00
}
2019-11-29 21:27:24 +01:00
}