This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapImage.cs
140 lines (116 loc) · 2.56 KB
/
MapImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MovableMap
{
public partial class MapImage : Form
{
// For Images
private PictureBox pbx;
private Bitmap bmpScreenshot;
private Graphics gfxScreenshot;
// For Timer
private Timer fpsTimer;
// For Focus
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private string targetTitle = "notepad";
private Process returnProcess = null;
private int counter = 0;
public MapImage()
{
InitializeComponent();
returnProcess = findProcess(targetTitle);
}
private Process findProcess(string targetTitle)
{
return Process.GetProcesses().FirstOrDefault(pr => pr.MainWindowTitle.ToLower().Contains(targetTitle.ToLower()));
}
private void setActive(Process process)
{
if (process != null)
{
SetForegroundWindow(process.MainWindowHandle);
}
}
private void MapImage_Load(object sender, EventArgs e)
{
pbx = new PictureBox
{
Name = "mapBox",
Size = new Size(420, 420),
Location = new Point(0, 0),
Visible = true
};
this.Controls.Add(pbx);
InitializeTimer();
}
private void DrawMap()
{
bmpScreenshot = new Bitmap(420, 420, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(4700, 1020, 0, 0, new Size(420, 420));
pbx.Image = bmpScreenshot;
}
private void InitializeTimer()
{
fpsTimer = new Timer();
fpsTimer.Interval = 1000 / 60;
fpsTimer.Enabled = true;
fpsTimer.Tick += new System.EventHandler(fpsTimer_Tick);
}
private void fpsTimer_Tick(object sender, System.EventArgs e)
{
DrawMap();
// TODO: delete (?)
counter += 1;
if (counter >= 120)
{
TopMost = false;
TopMost = true;
counter = 0;
}
}
private void StartTimer()
{
fpsTimer.Enabled = true;
}
private void StopTimer()
{
fpsTimer.Enabled = false;
}
public void UpdateForm(int xPos, int yPos, bool hide)
{
if (xPos < 0 || xPos > 5000)
{
xPos = 2000;
}
if (yPos < 0 || yPos > 1200)
{
yPos = 500;
}
this.Location = new Point(xPos, yPos);
if (hide)
{
Opacity = 0;
StopTimer();
}
else
{
Opacity = 1;
StartTimer();
}
}
private void MapImage_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void MapImage_Activated(object sender, EventArgs e)
{
setActive(returnProcess);
}
}
}