This commit is contained in:
CreepyCrafter24 2020-01-16 19:50:31 +01:00
parent 16a91e0ee9
commit cd89f30f6d
13 changed files with 548 additions and 201 deletions

View File

@ -61,6 +61,9 @@
<Compile Include="HID.cs" /> <Compile Include="HID.cs" />
<Compile Include="IO.cs" /> <Compile Include="IO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RotatingIndicator.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Security.cs" /> <Compile Include="Security.cs" />
<Compile Include="SelectBox.cs"> <Compile Include="SelectBox.cs">
<SubType>Form</SubType> <SubType>Form</SubType>

153
Misc/RotatingIndicator.cs Normal file
View File

@ -0,0 +1,153 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
namespace CC_Functions.Misc
{
/// <summary>
/// Animated control similar to update screens in Windows 8 and 10
/// </summary>
public sealed class RotatingIndicator : Control
{
private const double IndicatorOffset = Math.PI / 16;
private const int MaximumIndicators = 6;
private const int SizeFactor = 20;
private const double StartAt = (2 * Math.PI) / 3;
private const double TimerInterval = 100.0;
private readonly Indicator[] indicators = new Indicator[MaximumIndicators];
private readonly Timer timer;
private int indicatorCenterRadius;
private int indicatorDiameter;
/// <summary>
/// Instantiates the control
/// </summary>
public RotatingIndicator()
{
for (int i = 0; i < 6; i++)
indicators[i] = new Indicator(StartAt + (i * IndicatorOffset));
SetStyle(
ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
ForeColorChanged += (sender, e) => Invalidate();
SizeChanged += (sender, e) =>
{
int indicatorRadius = (int) Math.Round(Height / (double) SizeFactor);
indicatorDiameter = 2 * indicatorRadius;
Height = SizeFactor * indicatorRadius;
Width = Height;
int outerRadius = Height / 2;
int innerRadius = outerRadius - indicatorDiameter;
indicatorCenterRadius = innerRadius + indicatorRadius;
Invalidate();
};
OnSizeChanged(null);
timer = new Timer();
timer.Elapsed += (sender, e) =>
{
try
{
if (InvokeRequired)
Invoke((Action) Refresh);
else Refresh();
}
catch
{
// ignored
}
};
timer.Interval = TimerInterval;
timer.Enabled = true;
}
/// <summary>
/// Start/stops indicator animation
/// </summary>
[Category("Appearance")]
[Description("Start/stops indicator animation")]
[DefaultValue(true)]
[Bindable(true)]
public bool Animate
{
get => timer.Enabled;
set => timer.Enabled = value;
}
/// <summary>
/// Specifies indicator rotational refresh
/// </summary>
[Category("Appearance")]
[Description("Specifies indicator rotational refresh")]
[DefaultValue(200)]
[Bindable(true)]
public double RefreshRate
{
get => timer.Interval;
set
{
timer.Interval = Math.Max(Math.Min(value, 200), 10);
Invalidate();
}
}
/// <summary>
/// Disposes used objects and the control itself
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing) timer.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// Paints the control and cycles the animation
/// </summary>
/// <param name="e">Arguments specifying the painting target</param>
protected override void OnPaint(PaintEventArgs e)
{
GraphicsContainer state = e.Graphics.BeginContainer();
e.Graphics.TranslateTransform(-Left, -Top);
Rectangle clip = e.ClipRectangle;
clip.Offset(Left, Top);
PaintEventArgs pea = new PaintEventArgs(e.Graphics, clip);
InvokePaintBackground(Parent, pea);
InvokePaint(Parent, pea);
e.Graphics.EndContainer(state);
e.Graphics.Clear(BackColor);
Brush brush = new SolidBrush(ForeColor);
for (int i = MaximumIndicators - 1; i >= 0; i--)
{
double degrees = indicators[i].Radians;
if (degrees < 0.0)
degrees += 2 * Math.PI;
int dx = (int) Math.Round(indicatorCenterRadius * Math.Cos(degrees)) + indicatorCenterRadius;
int dy = indicatorCenterRadius - (int) Math.Round(indicatorCenterRadius * Math.Sin(degrees));
e.Graphics.FillEllipse(brush,
new Rectangle(new Point(dx, dy), new Size(indicatorDiameter, indicatorDiameter)));
degrees -= indicators[i].Speed * IndicatorOffset;
if (indicators[i].Speed > 1.0) indicators[i].Speed += 0.25;
if (degrees < 0.0) indicators[i].Speed = 1.25;
else if (degrees < StartAt) indicators[i].Speed = 1.0;
indicators[i].Radians = degrees;
}
brush.Dispose();
}
}
internal struct Indicator
{
public Indicator(double radians)
{
Radians = radians;
Speed = 1.0;
}
public double Radians { get; set; }
public double Speed { get; set; }
}
}

View File

@ -160,8 +160,11 @@
// //
// wnd_action_pos_h_label // wnd_action_pos_h_label
// //
this.wnd_action_pos_h_label.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_pos_h_label.AutoSize = true; this.wnd_action_pos_h_label.AutoSize = true;
this.wnd_action_pos_h_label.Location = new System.Drawing.Point(135, 395); this.wnd_action_pos_h_label.Location = new System.Drawing.Point(135, 284);
this.wnd_action_pos_h_label.Name = "wnd_action_pos_h_label"; this.wnd_action_pos_h_label.Name = "wnd_action_pos_h_label";
this.wnd_action_pos_h_label.Size = new System.Drawing.Size(19, 15); this.wnd_action_pos_h_label.Size = new System.Drawing.Size(19, 15);
this.wnd_action_pos_h_label.TabIndex = 19; this.wnd_action_pos_h_label.TabIndex = 19;
@ -169,8 +172,11 @@
// //
// wnd_action_pos_w_label // wnd_action_pos_w_label
// //
this.wnd_action_pos_w_label.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_pos_w_label.AutoSize = true; this.wnd_action_pos_w_label.AutoSize = true;
this.wnd_action_pos_w_label.Location = new System.Drawing.Point(135, 367); this.wnd_action_pos_w_label.Location = new System.Drawing.Point(135, 256);
this.wnd_action_pos_w_label.Name = "wnd_action_pos_w_label"; this.wnd_action_pos_w_label.Name = "wnd_action_pos_w_label";
this.wnd_action_pos_w_label.Size = new System.Drawing.Size(21, 15); this.wnd_action_pos_w_label.Size = new System.Drawing.Size(21, 15);
this.wnd_action_pos_w_label.TabIndex = 18; this.wnd_action_pos_w_label.TabIndex = 18;
@ -178,22 +184,31 @@
// //
// wnd_action_pos_h_bar // wnd_action_pos_h_bar
// //
this.wnd_action_pos_h_bar.Location = new System.Drawing.Point(159, 395); this.wnd_action_pos_h_bar.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_pos_h_bar.Location = new System.Drawing.Point(159, 284);
this.wnd_action_pos_h_bar.Name = "wnd_action_pos_h_bar"; this.wnd_action_pos_h_bar.Name = "wnd_action_pos_h_bar";
this.wnd_action_pos_h_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_h_bar.Size = new System.Drawing.Size(121, 45);
this.wnd_action_pos_h_bar.TabIndex = 21; this.wnd_action_pos_h_bar.TabIndex = 21;
// //
// wnd_action_pos_w_bar // wnd_action_pos_w_bar
// //
this.wnd_action_pos_w_bar.Location = new System.Drawing.Point(159, 367); this.wnd_action_pos_w_bar.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_pos_w_bar.Location = new System.Drawing.Point(159, 256);
this.wnd_action_pos_w_bar.Name = "wnd_action_pos_w_bar"; this.wnd_action_pos_w_bar.Name = "wnd_action_pos_w_bar";
this.wnd_action_pos_w_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_w_bar.Size = new System.Drawing.Size(121, 45);
this.wnd_action_pos_w_bar.TabIndex = 20; this.wnd_action_pos_w_bar.TabIndex = 20;
// //
// wnd_action_pos_y_label // wnd_action_pos_y_label
// //
this.wnd_action_pos_y_label.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_pos_y_label.AutoSize = true; this.wnd_action_pos_y_label.AutoSize = true;
this.wnd_action_pos_y_label.Location = new System.Drawing.Point(7, 395); this.wnd_action_pos_y_label.Location = new System.Drawing.Point(7, 284);
this.wnd_action_pos_y_label.Name = "wnd_action_pos_y_label"; this.wnd_action_pos_y_label.Name = "wnd_action_pos_y_label";
this.wnd_action_pos_y_label.Size = new System.Drawing.Size(17, 15); this.wnd_action_pos_y_label.Size = new System.Drawing.Size(17, 15);
this.wnd_action_pos_y_label.TabIndex = 15; this.wnd_action_pos_y_label.TabIndex = 15;
@ -201,8 +216,11 @@
// //
// wnd_action_pos_x_label // wnd_action_pos_x_label
// //
this.wnd_action_pos_x_label.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_pos_x_label.AutoSize = true; this.wnd_action_pos_x_label.AutoSize = true;
this.wnd_action_pos_x_label.Location = new System.Drawing.Point(7, 367); this.wnd_action_pos_x_label.Location = new System.Drawing.Point(7, 256);
this.wnd_action_pos_x_label.Name = "wnd_action_pos_x_label"; this.wnd_action_pos_x_label.Name = "wnd_action_pos_x_label";
this.wnd_action_pos_x_label.Size = new System.Drawing.Size(17, 15); this.wnd_action_pos_x_label.Size = new System.Drawing.Size(17, 15);
this.wnd_action_pos_x_label.TabIndex = 13; this.wnd_action_pos_x_label.TabIndex = 13;
@ -210,21 +228,28 @@
// //
// wnd_action_pos_y_bar // wnd_action_pos_y_bar
// //
this.wnd_action_pos_y_bar.Location = new System.Drawing.Point(20, 395); this.wnd_action_pos_y_bar.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_pos_y_bar.Location = new System.Drawing.Point(20, 284);
this.wnd_action_pos_y_bar.Name = "wnd_action_pos_y_bar"; this.wnd_action_pos_y_bar.Name = "wnd_action_pos_y_bar";
this.wnd_action_pos_y_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_y_bar.Size = new System.Drawing.Size(121, 45);
this.wnd_action_pos_y_bar.TabIndex = 17; this.wnd_action_pos_y_bar.TabIndex = 17;
// //
// wnd_action_pos_x_bar // wnd_action_pos_x_bar
// //
this.wnd_action_pos_x_bar.Location = new System.Drawing.Point(20, 367); this.wnd_action_pos_x_bar.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_pos_x_bar.Location = new System.Drawing.Point(20, 256);
this.wnd_action_pos_x_bar.Name = "wnd_action_pos_x_bar"; this.wnd_action_pos_x_bar.Name = "wnd_action_pos_x_bar";
this.wnd_action_pos_x_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_x_bar.Size = new System.Drawing.Size(121, 45);
this.wnd_action_pos_x_bar.TabIndex = 16; this.wnd_action_pos_x_bar.TabIndex = 16;
// //
// wnd_action_pos // wnd_action_pos
// //
this.wnd_action_pos.Location = new System.Drawing.Point(101, 337); this.wnd_action_pos.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.wnd_action_pos.Location = new System.Drawing.Point(101, 226);
this.wnd_action_pos.Name = "wnd_action_pos"; this.wnd_action_pos.Name = "wnd_action_pos";
this.wnd_action_pos.Size = new System.Drawing.Size(87, 27); this.wnd_action_pos.Size = new System.Drawing.Size(87, 27);
this.wnd_action_pos.TabIndex = 14; this.wnd_action_pos.TabIndex = 14;
@ -349,7 +374,8 @@
// //
// wnd_action_destroy // wnd_action_destroy
// //
this.wnd_action_destroy.Location = new System.Drawing.Point(7, 337); this.wnd_action_destroy.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.wnd_action_destroy.Location = new System.Drawing.Point(7, 226);
this.wnd_action_destroy.Name = "wnd_action_destroy"; this.wnd_action_destroy.Name = "wnd_action_destroy";
this.wnd_action_destroy.Size = new System.Drawing.Size(87, 27); this.wnd_action_destroy.Size = new System.Drawing.Size(87, 27);
this.wnd_action_destroy.TabIndex = 2; this.wnd_action_destroy.TabIndex = 2;
@ -359,7 +385,8 @@
// //
// wnd_action_front // wnd_action_front
// //
this.wnd_action_front.Location = new System.Drawing.Point(196, 337); this.wnd_action_front.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.wnd_action_front.Location = new System.Drawing.Point(196, 226);
this.wnd_action_front.Name = "wnd_action_front"; this.wnd_action_front.Name = "wnd_action_front";
this.wnd_action_front.Size = new System.Drawing.Size(84, 27); this.wnd_action_front.Size = new System.Drawing.Size(84, 27);
this.wnd_action_front.TabIndex = 10; this.wnd_action_front.TabIndex = 10;
@ -369,8 +396,11 @@
// //
// wnd_action_enabled // wnd_action_enabled
// //
this.wnd_action_enabled.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_enabled.AutoSize = true; this.wnd_action_enabled.AutoSize = true;
this.wnd_action_enabled.Location = new System.Drawing.Point(7, 275); this.wnd_action_enabled.Location = new System.Drawing.Point(7, 167);
this.wnd_action_enabled.Name = "wnd_action_enabled"; this.wnd_action_enabled.Name = "wnd_action_enabled";
this.wnd_action_enabled.Size = new System.Drawing.Size(68, 19); this.wnd_action_enabled.Size = new System.Drawing.Size(68, 19);
this.wnd_action_enabled.TabIndex = 9; this.wnd_action_enabled.TabIndex = 9;
@ -380,7 +410,10 @@
// //
// wnd_action_title_get // wnd_action_title_get
// //
this.wnd_action_title_get.Location = new System.Drawing.Point(237, 270); this.wnd_action_title_get.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_title_get.Location = new System.Drawing.Point(237, 159);
this.wnd_action_title_get.Name = "wnd_action_title_get"; this.wnd_action_title_get.Name = "wnd_action_title_get";
this.wnd_action_title_get.Size = new System.Drawing.Size(43, 27); this.wnd_action_title_get.Size = new System.Drawing.Size(43, 27);
this.wnd_action_title_get.TabIndex = 8; this.wnd_action_title_get.TabIndex = 8;
@ -474,7 +507,7 @@
this.wnd.Controls.Add(this.wnd_select_self); this.wnd.Controls.Add(this.wnd_select_self);
this.wnd.Location = new System.Drawing.Point(14, 135); this.wnd.Location = new System.Drawing.Point(14, 135);
this.wnd.Name = "wnd"; this.wnd.Name = "wnd";
this.wnd.Size = new System.Drawing.Size(287, 456); this.wnd.Size = new System.Drawing.Size(287, 345);
this.wnd.TabIndex = 6; this.wnd.TabIndex = 6;
this.wnd.TabStop = false; this.wnd.TabStop = false;
this.wnd.Text = "CC-Functions.W32.Wnd32"; this.wnd.Text = "CC-Functions.W32.Wnd32";
@ -491,8 +524,11 @@
// //
// wnd_action_overlay // wnd_action_overlay
// //
this.wnd_action_overlay.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_overlay.AutoSize = true; this.wnd_action_overlay.AutoSize = true;
this.wnd_action_overlay.Location = new System.Drawing.Point(162, 275); this.wnd_action_overlay.Location = new System.Drawing.Point(162, 167);
this.wnd_action_overlay.Name = "wnd_action_overlay"; this.wnd_action_overlay.Name = "wnd_action_overlay";
this.wnd_action_overlay.Size = new System.Drawing.Size(66, 19); this.wnd_action_overlay.Size = new System.Drawing.Size(66, 19);
this.wnd_action_overlay.TabIndex = 25; this.wnd_action_overlay.TabIndex = 25;
@ -502,9 +538,13 @@
// //
// wnd_action_style // wnd_action_style
// //
this.wnd_action_style.Anchor =
((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.wnd_action_style.FormattingEnabled = true; this.wnd_action_style.FormattingEnabled = true;
this.wnd_action_style.Location = new System.Drawing.Point(7, 306); this.wnd_action_style.Location = new System.Drawing.Point(7, 195);
this.wnd_action_style.Name = "wnd_action_style"; this.wnd_action_style.Name = "wnd_action_style";
this.wnd_action_style.Size = new System.Drawing.Size(238, 23); this.wnd_action_style.Size = new System.Drawing.Size(238, 23);
this.wnd_action_style.TabIndex = 24; this.wnd_action_style.TabIndex = 24;
@ -513,8 +553,11 @@
// //
// wnd_action_visible // wnd_action_visible
// //
this.wnd_action_visible.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.wnd_action_visible.AutoSize = true; this.wnd_action_visible.AutoSize = true;
this.wnd_action_visible.Location = new System.Drawing.Point(90, 275); this.wnd_action_visible.Location = new System.Drawing.Point(90, 167);
this.wnd_action_visible.Name = "wnd_action_visible"; this.wnd_action_visible.Name = "wnd_action_visible";
this.wnd_action_visible.Size = new System.Drawing.Size(60, 19); this.wnd_action_visible.Size = new System.Drawing.Size(60, 19);
this.wnd_action_visible.TabIndex = 23; this.wnd_action_visible.TabIndex = 23;
@ -524,9 +567,12 @@
// //
// wnd_action_icon // wnd_action_icon
// //
this.wnd_action_icon.Anchor =
((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.wnd_action_icon.BackColor = System.Drawing.SystemColors.ControlLight; this.wnd_action_icon.BackColor = System.Drawing.SystemColors.ControlLight;
this.wnd_action_icon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.wnd_action_icon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.wnd_action_icon.Location = new System.Drawing.Point(253, 303); this.wnd_action_icon.Location = new System.Drawing.Point(253, 193);
this.wnd_action_icon.Name = "wnd_action_icon"; this.wnd_action_icon.Name = "wnd_action_icon";
this.wnd_action_icon.Size = new System.Drawing.Size(27, 27); this.wnd_action_icon.Size = new System.Drawing.Size(27, 27);
this.wnd_action_icon.TabIndex = 22; this.wnd_action_icon.TabIndex = 22;

View File

@ -12,26 +12,18 @@ namespace CC_Functions.W32.Test
public partial class MainForm : Form public partial class MainForm : Form
{ {
private static Wnd32 tmpWnd32_obj; private static Wnd32 tmpWnd32_obj;
private Wnd32 tmpWnd
{
get => tmpWnd32_obj;
set {
tmpWnd32_obj = value;
Wnd_action_title_get_Click(null, null);
}
}
private static MainForm mainF; private static MainForm mainF;
private static Form frm; private static Form frm;
private static Label lab; private static Label lab;
private readonly KeyboardHook kHook; private readonly KeyboardHook kHook;
private readonly MouseHook mHook; private readonly MouseHook mHook;
Label[] readerLabels; private readonly Label[] readerLabels;
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
mainF = this; mainF = this;
tmpWnd32_obj = Wnd32.fromForm(this); tmpWnd32_obj = Wnd32.FromForm(this);
#if DEBUG #if DEBUG
tmpWnd32_obj.MakeOverlay(); tmpWnd32_obj.MakeOverlay();
#endif #endif
@ -45,7 +37,7 @@ namespace CC_Functions.W32.Test
wnd_action_pos_w_bar.Maximum = Screen.PrimaryScreen.Bounds.Width; wnd_action_pos_w_bar.Maximum = Screen.PrimaryScreen.Bounds.Width;
wnd_action_pos_h_bar.Maximum = Screen.PrimaryScreen.Bounds.Height; wnd_action_pos_h_bar.Maximum = Screen.PrimaryScreen.Bounds.Height;
wnd_action_style.DataSource = Enum.GetValues(typeof(FormWindowState)); wnd_action_style.DataSource = Enum.GetValues(typeof(FormWindowState));
wnd_action_style.SelectedItem = tmpWnd32_obj.state; wnd_action_style.SelectedItem = tmpWnd32_obj.State;
readerLabels = Enum.GetValues(typeof(Keys)).OfType<Keys>().OrderBy(s => s.ToString()).Select(s => readerLabels = Enum.GetValues(typeof(Keys)).OfType<Keys>().OrderBy(s => s.ToString()).Select(s =>
{ {
Label lab = new Label {Tag = s}; Label lab = new Label {Tag = s};
@ -57,6 +49,16 @@ namespace CC_Functions.W32.Test
screen_get_Click(null, null); screen_get_Click(null, null);
} }
private Wnd32 tmpWnd
{
get => tmpWnd32_obj;
set
{
tmpWnd32_obj = value;
Wnd_action_title_get_Click(null, null);
}
}
public void set_up_box(ComboBox box, Type enumT) public void set_up_box(ComboBox box, Type enumT)
{ {
box.DataSource = Enum.GetNames(enumT); box.DataSource = Enum.GetNames(enumT);
@ -72,32 +74,36 @@ namespace CC_Functions.W32.Test
public object get_box_value(ComboBox box) => ((object[]) box.Tag)[box.SelectedIndex]; public object get_box_value(ComboBox box) => ((object[]) box.Tag)[box.SelectedIndex];
private void Power_execute_Click(object sender, EventArgs e) => RaiseEvent((ShutdownMode)get_box_value(power_mode_box), (ShutdownReason)get_box_value(power_reason_box), 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)); (ShutdownMod) get_box_value(power_mod_box));
private void Wnd_select_self_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromForm(this); private void Wnd_select_self_Click(object sender, EventArgs e) => tmpWnd = Wnd32.FromForm(this);
private void wnd_select_list_Click(object sender, EventArgs e) => tmpWnd = SelectBox.Show(Wnd32.Visible, "Please select a window") ?? tmpWnd; private void wnd_select_list_Click(object sender, EventArgs e) =>
tmpWnd = SelectBox.Show(Wnd32.Visible, "Please select a window") ?? tmpWnd;
private void Wnd_select_title_button_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromMetadata(null, wnd_select_title_box.Text); private void Wnd_select_title_button_Click(object sender, EventArgs e) =>
tmpWnd = Wnd32.FromMetadata(null, wnd_select_title_box.Text);
private void Wnd_selet_class_button_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromMetadata(wnd_select_class_box.Text); private void Wnd_selet_class_button_Click(object sender, EventArgs e) =>
tmpWnd = Wnd32.FromMetadata(wnd_select_class_box.Text);
private void Wnd_action_title_set_Click(object sender, EventArgs e) => tmpWnd.title = wnd_select_title_box.Text; private void Wnd_action_title_set_Click(object sender, EventArgs e) => tmpWnd.Title = wnd_select_title_box.Text;
private void Wnd_action_title_get_Click(object sender, EventArgs e) private void Wnd_action_title_get_Click(object sender, EventArgs e)
{ {
if (!tmpWnd.stillExists) if (!tmpWnd.StillExists)
tmpWnd = Wnd32.fromForm(this); tmpWnd = Wnd32.FromForm(this);
wnd_select_title_box.Text = tmpWnd.title; wnd_select_title_box.Text = tmpWnd.Title;
wnd_action_enabled.Checked = tmpWnd.enabled; wnd_action_enabled.Checked = tmpWnd.Enabled;
wnd_select_selected.Text = "Selected: " + tmpWnd.hWnd; wnd_select_selected.Text = "Selected: " + tmpWnd.HWnd;
wnd_action_style.SelectedIndex = (int) tmpWnd.state; wnd_action_style.SelectedIndex = (int) tmpWnd.State;
wnd_select_class_box.Text = tmpWnd.className; wnd_select_class_box.Text = tmpWnd.ClassName;
wnd_action_visible.Checked = tmpWnd.shown; wnd_action_visible.Checked = tmpWnd.Shown;
try try
{ {
wnd_action_icon.BackgroundImage = tmpWnd.icon.ToBitmap(); wnd_action_icon.BackgroundImage = tmpWnd.Icon.ToBitmap();
} }
catch catch
{ {
@ -106,7 +112,7 @@ namespace CC_Functions.W32.Test
try try
{ {
wnd_action_pos_x_bar.Value = tmpWnd.position.X; wnd_action_pos_x_bar.Value = tmpWnd.Position.X;
} }
catch catch
{ {
@ -114,7 +120,7 @@ namespace CC_Functions.W32.Test
try try
{ {
wnd_action_pos_y_bar.Value = tmpWnd.position.Y; wnd_action_pos_y_bar.Value = tmpWnd.Position.Y;
} }
catch catch
{ {
@ -122,7 +128,7 @@ namespace CC_Functions.W32.Test
try try
{ {
wnd_action_pos_w_bar.Value = tmpWnd.position.Width; wnd_action_pos_w_bar.Value = tmpWnd.Position.Width;
} }
catch catch
{ {
@ -130,7 +136,7 @@ namespace CC_Functions.W32.Test
try try
{ {
wnd_action_pos_h_bar.Value = tmpWnd.position.Height; wnd_action_pos_h_bar.Value = tmpWnd.Position.Height;
} }
catch catch
{ {
@ -138,20 +144,20 @@ namespace CC_Functions.W32.Test
} }
private void Wnd_action_enabled_CheckedChanged(object sender, EventArgs e) => private void Wnd_action_enabled_CheckedChanged(object sender, EventArgs e) =>
tmpWnd.enabled = wnd_action_enabled.Checked; tmpWnd.Enabled = wnd_action_enabled.Checked;
private void Wnd_action_visible_CheckedChanged(object sender, EventArgs e) => private void Wnd_action_visible_CheckedChanged(object sender, EventArgs e) =>
tmpWnd.shown = wnd_action_visible.Checked; tmpWnd.Shown = wnd_action_visible.Checked;
private void Wnd_action_front_Click(object sender, EventArgs e) => tmpWnd.isForeground = true; private void Wnd_action_front_Click(object sender, EventArgs e) => tmpWnd.IsForeground = true;
private void Wnd_action_destroy_Click(object sender, EventArgs e) => tmpWnd.Destroy(); private void Wnd_action_destroy_Click(object sender, EventArgs e) => tmpWnd.Destroy();
private void Wnd_select_mouse_Click(object sender, EventArgs e) private void Wnd_select_mouse_Click(object sender, EventArgs e)
{ {
WindowState = FormWindowState.Minimized; WindowState = FormWindowState.Minimized;
tmpWnd = Wnd32.fromForm(this); tmpWnd = Wnd32.FromForm(this);
tmpWnd.enabled = false; tmpWnd.Enabled = false;
frm = new Form(); frm = new Form();
frm.BackColor = Color.White; frm.BackColor = Color.White;
frm.Opacity = 0.4f; frm.Opacity = 0.4f;
@ -162,16 +168,16 @@ namespace CC_Functions.W32.Test
lab = new Label(); lab = new Label();
frm.Controls.Add(lab); frm.Controls.Add(lab);
frm.Show(); frm.Show();
Wnd32.fromForm(frm).overlay = true; Wnd32.FromForm(frm).Overlay = true;
} }
private void Frm_Click(object sender, EventArgs e) private void Frm_Click(object sender, EventArgs e)
{ {
frm.Hide(); frm.Hide();
frm.WindowState = FormWindowState.Minimized; frm.WindowState = FormWindowState.Minimized;
Wnd32 tmp = Wnd32.fromPoint(MousePosition); Wnd32 tmp = Wnd32.FromPoint(MousePosition);
tmpWnd.enabled = true; tmpWnd.Enabled = true;
tmpWnd.isForeground = true; tmpWnd.IsForeground = true;
tmpWnd = tmp; tmpWnd = tmp;
mainF.WindowState = FormWindowState.Normal; mainF.WindowState = FormWindowState.Normal;
frm.Close(); frm.Close();
@ -179,7 +185,7 @@ namespace CC_Functions.W32.Test
private void Frm_MouseMove(object sender, MouseEventArgs e) private void Frm_MouseMove(object sender, MouseEventArgs e)
{ {
lab.Text = Wnd32.fromPoint(MousePosition).ToString(); lab.Text = Wnd32.FromPoint(MousePosition).ToString();
lab.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5); lab.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
} }
@ -194,7 +200,8 @@ namespace CC_Functions.W32.Test
private void MHook_OnMouse(MouseHookEventArgs args) => private void MHook_OnMouse(MouseHookEventArgs args) =>
mouse_log.Text = args.Message + " -|- " + args.Point + "\r\n" + mouse_log.Text; mouse_log.Text = args.Message + " -|- " + args.Point + "\r\n" + mouse_log.Text;
private void Mouse_log_TextChanged(object sender, EventArgs e) => mouse_log.Lines = mouse_log.Lines.Take(9).ToArray(); private void Mouse_log_TextChanged(object sender, EventArgs e) =>
mouse_log.Lines = mouse_log.Lines.Take(9).ToArray();
private void Keyboard_enabled_CheckedChanged(object sender, EventArgs e) private void Keyboard_enabled_CheckedChanged(object sender, EventArgs e)
{ {
@ -207,20 +214,21 @@ namespace CC_Functions.W32.Test
private void KHook_OnKeyPress(KeyboardHookEventArgs args) => private void KHook_OnKeyPress(KeyboardHookEventArgs args) =>
keyboard_log.Text = args.Key + "\r\n" + keyboard_log.Text; keyboard_log.Text = args.Key + "\r\n" + keyboard_log.Text;
private void Keyboard_log_TextChanged(object sender, EventArgs e) => keyboard_log.Lines = keyboard_log.Lines.Take(8).ToArray(); private void Keyboard_log_TextChanged(object sender, EventArgs e) =>
keyboard_log.Lines = keyboard_log.Lines.Take(8).ToArray();
private void Wnd_action_pos_Click(object sender, EventArgs e) => private void Wnd_action_pos_Click(object sender, EventArgs e) =>
tmpWnd.position = new Rectangle(wnd_action_pos_x_bar.Value, wnd_action_pos_y_bar.Value, 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); wnd_action_pos_w_bar.Value, wnd_action_pos_h_bar.Value);
private void Wnd_action_style_SelectedIndexChanged(object sender, EventArgs e) private void Wnd_action_style_SelectedIndexChanged(object sender, EventArgs e)
{ {
Enum.TryParse(wnd_action_style.SelectedValue.ToString(), out FormWindowState status); Enum.TryParse(wnd_action_style.SelectedValue.ToString(), out FormWindowState status);
tmpWnd.state = status; tmpWnd.State = status;
} }
private void wnd_action_overlay_CheckedChanged(object sender, EventArgs e) => private void wnd_action_overlay_CheckedChanged(object sender, EventArgs e) =>
tmpWnd.overlay = wnd_action_overlay.Checked; tmpWnd.Overlay = wnd_action_overlay.Checked;
private void readerUpdate_Tick(object sender, EventArgs e) private void readerUpdate_Tick(object sender, EventArgs e)
{ {
@ -239,37 +247,39 @@ namespace CC_Functions.W32.Test
using IDCDrawer drawer = DeskMan.CreateGraphics(); using IDCDrawer drawer = DeskMan.CreateGraphics();
Graphics g = drawer.Graphics; Graphics g = drawer.Graphics;
Pen eye = new Pen(new SolidBrush(Color.Red), 2); Pen eye = new Pen(new SolidBrush(Color.Red), 2);
g.DrawCurve(eye, new PointF[] { makePoint(20, 50), makePoint(50, 65), makePoint(80, 50) }); g.DrawCurve(eye, new[] {makePoint(20, 50), makePoint(50, 65), makePoint(80, 50)});
g.DrawCurve(eye, new PointF[] { makePoint(20, 50), makePoint(50, 35), makePoint(80, 50) }); g.DrawCurve(eye, new[] {makePoint(20, 50), makePoint(50, 35), makePoint(80, 50)});
g.DrawEllipse(eye, new RectangleF(PointF.Subtract(makePoint(50, 50), makeSizeY(15, 15)), makeSizeY(30, 30))); g.DrawEllipse(eye,
new RectangleF(PointF.Subtract(makePoint(50, 50), makeSizeY(15, 15)), makeSizeY(30, 30)));
} }
public static PointF makePoint(float xPercent, float yPercent) => new PointF(Screen.PrimaryScreen.Bounds.Width * xPercent / 100, public static PointF makePoint(float xPercent, float yPercent) => new PointF(
Screen.PrimaryScreen.Bounds.Height * yPercent / 100); (Screen.PrimaryScreen.Bounds.Width * xPercent) / 100,
(Screen.PrimaryScreen.Bounds.Height * yPercent) / 100);
public static SizeF makeSizeY(float xPercent, float yPercent) => new SizeF(Screen.PrimaryScreen.Bounds.Height * xPercent / 100, public static SizeF makeSizeY(float xPercent, float yPercent) => new SizeF(
Screen.PrimaryScreen.Bounds.Height * yPercent / 100); (Screen.PrimaryScreen.Bounds.Height * xPercent) / 100,
(Screen.PrimaryScreen.Bounds.Height * yPercent) / 100);
private void desk_set_Click(object sender, EventArgs e) private void desk_set_Click(object sender, EventArgs e)
{ {
OpenFileDialog dlg = new OpenFileDialog(); OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images (*.jpg, *.jpeg, *.jpe, *.jfif, *.png)|*.jpg;*.jpeg;*.jpe;*.jfif;*.png"; dlg.Filter = "Images (*.jpg, *.jpeg, *.jpe, *.jfif, *.png)|*.jpg;*.jpeg;*.jpe;*.jfif;*.png";
if (dlg.ShowDialog() == DialogResult.OK) if (dlg.ShowDialog() == DialogResult.OK) DeskMan.Wallpaper = Image.FromFile(dlg.FileName);
{
DeskMan.Wallpaper = Image.FromFile(dlg.FileName);
}
} }
private void screen_get_Click(object sender, EventArgs e) => screen_img.BackgroundImage = ScreenMan.CaptureScreen(); private void screen_get_Click(object sender, EventArgs e) =>
screen_img.BackgroundImage = ScreenMan.CaptureScreen();
private void screen_draw_Click(object sender, EventArgs e) private void screen_draw_Click(object sender, EventArgs e)
{ {
using IDCDrawer drawer = ScreenMan.GetDrawer(false); using IDCDrawer drawer = ScreenMan.GetDrawer(false);
Graphics g = drawer.Graphics; Graphics g = drawer.Graphics;
Pen eye = new Pen(new SolidBrush(Color.Red), 2); Pen eye = new Pen(new SolidBrush(Color.Red), 2);
g.DrawCurve(eye, new PointF[] { makePoint(20, 50), makePoint(50, 65), makePoint(80, 50) }); g.DrawCurve(eye, new[] {makePoint(20, 50), makePoint(50, 65), makePoint(80, 50)});
g.DrawCurve(eye, new PointF[] { makePoint(20, 50), makePoint(50, 35), makePoint(80, 50) }); g.DrawCurve(eye, new[] {makePoint(20, 50), makePoint(50, 35), makePoint(80, 50)});
g.DrawEllipse(eye, new RectangleF(PointF.Subtract(makePoint(50, 50), makeSizeY(15, 15)), makeSizeY(30, 30))); g.DrawEllipse(eye,
new RectangleF(PointF.Subtract(makePoint(50, 50), makeSizeY(15, 15)), makeSizeY(30, 30)));
} }
} }
} }

View File

@ -6,9 +6,13 @@ namespace CC_Functions.W32.DCDrawer
{ {
public class DCBuffered : IDCDrawer public class DCBuffered : IDCDrawer
{ {
private readonly DCUnbuffered drawer;
private readonly BufferedGraphics buffer; private readonly BufferedGraphics buffer;
public DCBuffered(IntPtr ptr) : this(ptr, IntPtr.Zero) {} private readonly DCUnbuffered drawer;
public DCBuffered(IntPtr ptr) : this(ptr, IntPtr.Zero)
{
}
public DCBuffered(IntPtr ptr, IntPtr hWnd) public DCBuffered(IntPtr ptr, IntPtr hWnd)
{ {
drawer = new DCUnbuffered(ptr, hWnd); drawer = new DCUnbuffered(ptr, hWnd);

View File

@ -6,8 +6,8 @@ namespace CC_Functions.W32.DCDrawer
{ {
public class DCUnbuffered : IDCDrawer public class DCUnbuffered : IDCDrawer
{ {
private readonly IntPtr hWnd;
private readonly IntPtr ptr; private readonly IntPtr ptr;
private IntPtr hWnd;
public DCUnbuffered(IntPtr ptr, IntPtr hWnd) public DCUnbuffered(IntPtr ptr, IntPtr hWnd)
{ {

View File

@ -10,34 +10,13 @@ namespace CC_Functions.W32
{ {
public static class DeskMan public static class DeskMan
{ {
public static IDCDrawer CreateGraphics(bool buffered = false)
{
Wnd32 progman = Wnd32.fromMetadata("Progman");
IntPtr result = IntPtr.Zero;
user32.SendMessageTimeout(progman.hWnd, 0x052C, new IntPtr(0), IntPtr.Zero, 0x0, 1000, out result);
IntPtr workerW = IntPtr.Zero;
user32.EnumWindows((tophandle, topparamhandle) =>
{
IntPtr p = user32.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
if (p != IntPtr.Zero)
{
workerW = user32.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero);
}
return true;
}, IntPtr.Zero);
IntPtr dc = user32.GetDCEx(workerW, IntPtr.Zero, 0x403);
if (dc == IntPtr.Zero)
{
throw new Exception("Something went wrong when creatiing the Graphics object");
}
return buffered ? (IDCDrawer)new DCBuffered(dc) : new DCUnbuffered(dc);
}
public static Image Wallpaper public static Image Wallpaper
{ {
get get
{ {
using (var bmpTemp = new Bitmap(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\Themes\TranscodedWallpaper")) using (Bitmap bmpTemp =
new Bitmap(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
@"\Microsoft\Windows\Themes\TranscodedWallpaper"))
{ {
return (Image) bmpTemp.Clone(); return (Image) bmpTemp.Clone();
} }
@ -53,5 +32,22 @@ namespace CC_Functions.W32
File.Delete(tempPath); File.Delete(tempPath);
} }
} }
public static IDCDrawer CreateGraphics(bool buffered = false)
{
Wnd32 progman = Wnd32.FromMetadata("Progman");
IntPtr result = IntPtr.Zero;
user32.SendMessageTimeout(progman.HWnd, 0x052C, new IntPtr(0), IntPtr.Zero, 0x0, 1000, out result);
IntPtr workerW = IntPtr.Zero;
user32.EnumWindows((tophandle, topparamhandle) =>
{
IntPtr p = user32.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
if (p != IntPtr.Zero) workerW = user32.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero);
return true;
}, IntPtr.Zero);
IntPtr dc = user32.GetDCEx(workerW, IntPtr.Zero, 0x403);
if (dc == IntPtr.Zero) throw new Exception("Something went wrong when creatiing the Graphics object");
return buffered ? (IDCDrawer) new DCBuffered(dc) : new DCUnbuffered(dc);
}
} }
} }

View File

@ -6,9 +6,9 @@ namespace CC_Functions.W32
{ {
public static class GenericExtensions public static class GenericExtensions
{ {
public static Wnd32 GetWindow(this IntPtr handle) => Wnd32.fromHandle(handle); public static Wnd32 GetWindow(this IntPtr handle) => Wnd32.FromHandle(handle);
public static Wnd32 GetMainWindow(this Process handle) => Wnd32.getProcessMain(handle); public static Wnd32 GetMainWindow(this Process handle) => Wnd32.GetProcessMain(handle);
public static Wnd32 GetWnd32(this Form frm) => Wnd32.fromForm(frm); public static Wnd32 GetWnd32(this Form frm) => Wnd32.FromForm(frm);
public static bool IsDown(this Keys key) => KeyboardReader.IsKeyDown(key); public static bool IsDown(this Keys key) => KeyboardReader.IsKeyDown(key);
public static Privileges.SecurityEntity GetEntity(this Privileges.SecurityEntity2 entity) => public static Privileges.SecurityEntity GetEntity(this Privileges.SecurityEntity2 entity) =>

View File

@ -6,6 +6,10 @@ namespace CC_Functions.W32.Native
{ {
internal static class user32 internal static class user32
{ {
public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
public delegate IntPtr LowLevelProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd); public static extern IntPtr GetWindowDC(IntPtr hWnd);
@ -116,12 +120,11 @@ namespace CC_Functions.W32.Native
public static extern bool EnumWindows(EnumDelegate lpEnumFunc, IntPtr lParam); public static extern bool EnumWindows(EnumDelegate lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,
IntPtr windowTitle);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam, uint flags, uint timeout, out IntPtr result); public static extern IntPtr SendMessageTimeout(IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam,
uint flags, uint timeout, out IntPtr result);
public delegate IntPtr LowLevelProc(int nCode, IntPtr wParam, IntPtr lParam);
public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Runtime.InteropServices;
using CC_Functions.W32.Native; using CC_Functions.W32.Native;
using static CC_Functions.W32.Privileges; using static CC_Functions.W32.Privileges;

View File

@ -88,6 +88,11 @@ namespace CC_Functions.W32
SE_UNSOLICITED_INPUT_NAME_TEXT SE_UNSOLICITED_INPUT_NAME_TEXT
} }
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int ERROR_NOT_ALL_ASSIGNED = 1300;
internal const uint TOKEN_QUERY = 0x0008;
internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
public static void EnablePrivilege(SecurityEntity securityEntity) public static void EnablePrivilege(SecurityEntity securityEntity)
{ {
if (!Enum.IsDefined(typeof(SecurityEntity), securityEntity)) if (!Enum.IsDefined(typeof(SecurityEntity), securityEntity))
@ -156,11 +161,6 @@ namespace CC_Functions.W32
public static SecurityEntity EntityToEntity(SecurityEntity2 entity) => (SecurityEntity) entity; public static SecurityEntity EntityToEntity(SecurityEntity2 entity) => (SecurityEntity) entity;
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int ERROR_NOT_ALL_ASSIGNED = 1300;
internal const uint TOKEN_QUERY = 0x0008;
internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct LUID internal struct LUID
{ {

View File

@ -8,6 +8,7 @@ namespace CC_Functions.W32
{ {
public static class ScreenMan public static class ScreenMan
{ {
private const int SRCCOPY = 13369376;
public static Image CaptureScreen() => CaptureWindow(user32.GetDesktopWindow()); public static Image CaptureScreen() => CaptureWindow(user32.GetDesktopWindow());
public static Image CaptureWindow(IntPtr handle) public static Image CaptureWindow(IntPtr handle)
@ -44,7 +45,5 @@ namespace CC_Functions.W32
public static Rectangle GetBounds() => Screen.PrimaryScreen.Bounds; public static Rectangle GetBounds() => Screen.PrimaryScreen.Bounds;
public static void Refresh() => shell32.SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); public static void Refresh() => shell32.SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
private const int SRCCOPY = 13369376;
} }
} }

View File

@ -10,123 +10,199 @@ using CC_Functions.W32.Native;
namespace CC_Functions.W32 namespace CC_Functions.W32
{ {
/// <summary>
/// Object representing a window handle in the Windows API. Provides a simplified interface for basic interactions
/// </summary>
public sealed class Wnd32 : IEquatable<Wnd32> public sealed class Wnd32 : IEquatable<Wnd32>
{ {
#region Exposed #region Exposed
#region CreateInstance #region CreateInstance
private Wnd32(IntPtr wndref) => hWnd = wndref; private Wnd32(IntPtr handle) => HWnd = handle;
public static Wnd32 fromHandle(IntPtr handle) => new Wnd32(handle); /// <summary>
/// Base method. Generates a window object from the specified handle
/// </summary>
/// <param name="handle">The handle</param>
/// <returns>The window</returns>
public static Wnd32 FromHandle(IntPtr handle) => new Wnd32(handle);
public static Wnd32 getProcessMain(Process process) => fromHandle(process.MainWindowHandle); /// <summary>
/// Gets the main window of the process
/// </summary>
/// <param name="process">The process</param>
/// <returns>The window. Might be IntPtr.Zero</returns>
public static Wnd32 GetProcessMain(Process process) => FromHandle(process.MainWindowHandle);
public static Wnd32 fromMetadata(string? lpClassName = null, string? lpWindowName = null) => /// <summary>
fromHandle(user32.FindWindow(lpClassName, lpWindowName)); /// Generates a window from metadata. Parameters should be null if they are not used
/// </summary>
/// <param name="lpClassName">
/// The class name of the window. Use the name you found before using the ClassName-parameter of
/// a window
/// </param>
/// <param name="lpWindowName">The windows name (title)</param>
/// <returns>The window. Might be IntPtr.Zero</returns>
public static Wnd32 FromMetadata(string? lpClassName = null, string? lpWindowName = null) =>
FromHandle(user32.FindWindow(lpClassName, lpWindowName));
public static Wnd32 fromPoint(Point point) => fromHandle(user32.WindowFromPoint(point.X, point.Y)); /// <summary>
/// Gets the window that is visible at the specified point
/// </summary>
/// <param name="point">The point to scan</param>
/// <returns>The window. Might be IntPtr.Zero</returns>
public static Wnd32 FromPoint(Point point) => FromHandle(user32.WindowFromPoint(point.X, point.Y));
public static Wnd32 fromForm(Form form) => fromHandle(form.Handle); /// <summary>
/// Gets the window associated with the forms handle
/// </summary>
/// <param name="form">Form to get window from</param>
/// <returns>The window. Might be IntPtr.Zero</returns>
public static Wnd32 FromForm(Form form) => FromHandle(form.Handle);
/// <summary>
/// Gets ALL windows. In most cases you will want to use Wnd32.Visible
/// </summary>
/// <exception cref="Win32Exception"></exception>
public static Wnd32[] All public static Wnd32[] All
{ {
get { WindowHandles = new List<IntPtr>(); get
{
_windowHandles = new List<IntPtr>();
if (!user32.EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero)) if (!user32.EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
throw new Win32Exception("There was a native error. This should never happen!"); throw new Win32Exception("There was a native error. This should never happen!");
return WindowHandles.Select(s => fromHandle(s)).ToArray(); } return _windowHandles.Select(s => FromHandle(s)).ToArray();
}
} }
public static Wnd32[] Visible => All.Where(s => user32.IsWindowVisible(s.hWnd) && !string.IsNullOrEmpty(s.title)).ToArray(); /// <summary>
public static Wnd32 Foreground => fromHandle(user32.GetForegroundWindow()); /// Gets all visible windows with valid titles
public static Wnd32 ConsoleWindow => fromHandle(kernel32.GetConsoleWindow()); /// </summary>
public static Wnd32[] Visible =>
All.Where(s => user32.IsWindowVisible(s.HWnd) && !string.IsNullOrEmpty(s.Title)).ToArray();
/// <summary>
/// Gets the foreground window
/// </summary>
public static Wnd32 Foreground => FromHandle(user32.GetForegroundWindow());
/// <summary>
/// The current programs console window. Do NOT use this if you are not targeting a console app or allocating a console
/// </summary>
public static Wnd32 ConsoleWindow => FromHandle(kernel32.GetConsoleWindow());
#endregion CreateInstance #endregion CreateInstance
#region InstanceActions #region InstanceActions
public string title /// <summary>
/// The windows title
/// </summary>
public string Title
{ {
get get
{ {
int length = user32.GetWindowTextLength(hWnd); int length = user32.GetWindowTextLength(HWnd);
StringBuilder sb = new StringBuilder(length + 1); StringBuilder sb = new StringBuilder(length + 1);
user32.GetWindowText(hWnd, sb, sb.Capacity); user32.GetWindowText(HWnd, sb, sb.Capacity);
return sb.ToString(); return sb.ToString();
} }
set => user32.SetWindowText(hWnd, value); set => user32.SetWindowText(HWnd, value);
} }
public Rectangle position /// <summary>
/// The windows position in screen-space
/// </summary>
public Rectangle Position
{ {
get get
{ {
RECT Rect = new RECT(); RECT rect = new RECT();
user32.GetWindowRect(hWnd, ref Rect); user32.GetWindowRect(HWnd, ref rect);
return new Rectangle(new Point(Rect.Left, Rect.Top), return new Rectangle(new Point(rect.Left, rect.Top),
new Size(Rect.Width, Rect.Height)); new Size(rect.Width, rect.Height));
} }
set set
{ {
RECT Rect = new RECT(); RECT rect = new RECT();
user32.GetWindowRect(hWnd, ref Rect); user32.GetWindowRect(HWnd, ref rect);
user32.MoveWindow(hWnd, value.X, value.Y, value.Width, value.Height, true); user32.MoveWindow(HWnd, value.X, value.Y, value.Width, value.Height, true);
} }
} }
public bool isForeground /// <summary>
/// Gets whether the window is the foreground window or brings it to the front. "False" must not be assigned!
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the value is "False"</exception>
public bool IsForeground
{ {
get => user32.GetForegroundWindow() == hWnd; get => user32.GetForegroundWindow() == HWnd;
set set
{ {
if (value) if (value)
user32.SetForegroundWindow(hWnd); user32.SetForegroundWindow(HWnd);
else else
throw new InvalidOperationException( throw new InvalidOperationException(
"You can't set a Window not to be in the foreground. Move another one over it!"); "You can't set a Window not to be in the foreground. Move another one over it!");
} }
} }
public bool enabled /// <summary>
/// Whether the window is enabled. Functionally similar to WinForms' "Control.Enabled"
/// </summary>
public bool Enabled
{ {
get => user32.IsWindowEnabled(hWnd); get => user32.IsWindowEnabled(HWnd);
set => user32.EnableWindow(hWnd, value); set => user32.EnableWindow(HWnd, value);
} }
public Icon icon /// <summary>
/// Gets the windows icon
/// </summary>
public Icon Icon
{ {
get get
{ {
IntPtr hicon = user32.SendMessage(hWnd, 0x7F, 1, 0); IntPtr hIcon = user32.SendMessage(HWnd, 0x7F, 1, 0);
if (hicon == IntPtr.Zero) if (hIcon == IntPtr.Zero)
hicon = user32.SendMessage(hWnd, 0x7F, 0, 0); hIcon = user32.SendMessage(HWnd, 0x7F, 0, 0);
if (hicon == IntPtr.Zero) if (hIcon == IntPtr.Zero)
hicon = user32.SendMessage(hWnd, 0x7F, 2, 0); hIcon = user32.SendMessage(HWnd, 0x7F, 2, 0);
return Icon.FromHandle(hicon); return Icon.FromHandle(hIcon);
} }
} }
public bool shown /// <summary>
/// Whether the window is visible
/// </summary>
public bool Shown
{ {
get => user32.IsWindowVisible(hWnd); get => user32.IsWindowVisible(HWnd);
set => user32.ShowWindow(hWnd, value ? 9 : 0); set => user32.ShowWindow(HWnd, value ? 9 : 0);
} }
public string className /// <summary>
/// Gets the windows class name, This is basically only useful for finding window class-names of specified programs
/// </summary>
public string ClassName
{ {
get get
{ {
StringBuilder ClassName = new StringBuilder(256); StringBuilder className = new StringBuilder(256);
user32.GetClassName(hWnd, ClassName, ClassName.Capacity); user32.GetClassName(HWnd, className, className.Capacity);
return ClassName.ToString(); return className.ToString();
} }
} }
public FormWindowState state /// <summary>
/// Sets the window state
/// </summary>
public FormWindowState State
{ {
get get
{ {
int style = user32.GetWindowLong(hWnd, -16); int style = user32.GetWindowLong(HWnd, -16);
if ((style & 0x01000000) == 0x01000000) if ((style & 0x01000000) == 0x01000000)
return FormWindowState.Maximized; return FormWindowState.Maximized;
if ((style & 0x20000000) == 0x20000000) if ((style & 0x20000000) == 0x20000000)
@ -138,50 +214,107 @@ namespace CC_Functions.W32
switch (value) switch (value)
{ {
case FormWindowState.Minimized: case FormWindowState.Minimized:
user32.ShowWindow(hWnd, 11); user32.ShowWindow(HWnd, 11);
break; break;
case FormWindowState.Normal: case FormWindowState.Normal:
user32.ShowWindow(hWnd, 1); user32.ShowWindow(HWnd, 1);
break; break;
case FormWindowState.Maximized: case FormWindowState.Maximized:
user32.ShowWindow(hWnd, 3); user32.ShowWindow(HWnd, 3);
break; break;
default:
throw new ArgumentException("The provided WindowState was invalid", "value");
} }
} }
} }
public bool overlay /// <summary>
/// Overlays the window over others
/// </summary>
public bool Overlay
{ {
set set
{ {
Rectangle tmp = position; Rectangle tmp = Position;
user32.SetWindowPos(hWnd, value ? HWND_TOPMOST : HWND_NOTOPMOST, tmp.X, tmp.Y, tmp.Width, tmp.Height, user32.SetWindowPos(HWnd, value ? new IntPtr(-1) : new IntPtr(-2), tmp.X, tmp.Y, tmp.Width, tmp.Height,
value ? SWP_NOMOVE | SWP_NOSIZE : 0); value ? (uint) 3 : 0);
} }
} }
public bool Destroy() /// <summary>
/// Forces the window to close
/// </summary>
/// <exception cref="Exception">Thrown if the window could not be closed</exception>
public void Destroy()
{ {
if (user32.DestroyWindow(hWnd)) if (!user32.DestroyWindow(HWnd))
return true;
throw new Exception("Failed."); throw new Exception("Failed.");
} }
public bool stillExists => user32.IsWindow(hWnd); /// <summary>
/// Whether the IntPtr is a window and still exists
/// </summary>
public bool StillExists => user32.IsWindow(HWnd);
public override string ToString() => hWnd + "; " + title + "; " + position; /// <summary>
/// Creates a user-readable string from the windows hWnd, title and position
/// </summary>
/// <returns>The created string</returns>
public override string ToString() => $"{HWnd}; {Title}; {Position}";
/// <summary>
/// Equality operator, uses the hWnd field
/// </summary>
/// <param name="obj">Object (Window) to compare</param>
/// <returns>Equality result</returns>
public override bool Equals(object obj) => Equals(obj as Wnd32); public override bool Equals(object obj) => Equals(obj as Wnd32);
public bool Equals(Wnd32 other) => other != null && EqualityComparer<IntPtr>.Default.Equals(hWnd, other.hWnd); /// <summary>
/// Equality operator, uses the hWnd field
/// </summary>
/// <param name="other">Window to compare</param>
/// <returns>Equality result</returns>
public bool Equals(Wnd32 other) => !IsNull(other) && other != null && HWnd.Equals(other.HWnd);
public override int GetHashCode() => -75345830 + EqualityComparer<IntPtr>.Default.GetHashCode(hWnd); /// <summary>
/// Equality operator, uses the hWnd field
/// </summary>
/// <returns>Equality result</returns>
public override int GetHashCode() => HWnd.GetHashCode();
public static bool operator ==(Wnd32 left, Wnd32 right) => EqualityComparer<Wnd32>.Default.Equals(left, right); /// <summary>
/// Equality operator, uses the hWnd field
/// </summary>
/// <param name="left">Window to compare</param>
/// <param name="right">Window to compare</param>
/// <returns>Equality result</returns>
public static bool operator ==(Wnd32 left, Wnd32 right) => !AreNull(left, right) && left.HWnd == right.HWnd;
public static bool operator !=(Wnd32 left, Wnd32 right) => !(left == right); /// <summary>
/// Equality operator, uses the hWnd field
/// </summary>
/// <param name="left">Window to compare</param>
/// <param name="right">Window to compare</param>
/// <returns>Equality result</returns>
public static bool operator !=(Wnd32 left, Wnd32 right) => AreNull(left, right) || left.HWnd != right.HWnd;
private static bool AreNull(params Wnd32[] windows) => windows.Any(IsNull);
private static bool IsNull(Wnd32 window)
{
try
{
window.ToString();
return false;
}
catch (NullReferenceException)
{
return true;
}
}
#endregion InstanceActions #endregion InstanceActions
@ -189,20 +322,21 @@ namespace CC_Functions.W32
#region Internal #region Internal
public IntPtr hWnd; /// <summary>
/// The windows' handle
/// </summary>
public readonly IntPtr HWnd;
private static List<IntPtr> _windowHandles;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private static List<IntPtr> WindowHandles;
private static bool FilterCallback(IntPtr hWnd, int lParam) private static bool FilterCallback(IntPtr hWnd, int lParam)
{ {
StringBuilder sbTitle = new StringBuilder(1024); StringBuilder sbTitle = new StringBuilder(1024);
user32.GetWindowText(hWnd, sbTitle, 1024); user32.GetWindowText(hWnd, sbTitle, 1024);
WindowHandles.Add(hWnd); _windowHandles.Add(hWnd);
return true; return true;
} }
#endregion Internal #endregion Internal
} }
} }