¿Como puedo mejorar programa? findwnidow y setwindowpositon
Publicado por juan (3 intervenciones) el 10/08/2018 21:55:14
Hola estoy intentando hacer un programa que aporte información extra a otro programa quiero que por ejemplo un form transparente con un label se ponga encima de una wentana de otro programa parece que mas o menos lo tengo hecho pero me gustaría hacerlo bien.
Según tengo entendido se puede hacer un hook a una ventana y hacer una especie de evento por si cambia de posición en lugar de hacer un bucle que pida la posición de la ventana constantemente que¿podría ser mejor?
También me gustaría que el form con el label se situara encima de la ventana de ese programa y no de todos
que es como lo tengo puesto con this.TopMost =true;
Según tengo entendido se puede hacer un hook a una ventana y hacer una especie de evento por si cambia de posición en lugar de hacer un bucle que pida la posición de la ventana constantemente que¿podría ser mejor?
También me gustaría que el form con el label se situara encima de la ventana de ese programa y no de todos
que es como lo tengo puesto con this.TopMost =true;
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
namespace prj_prueba
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindowA")]
private static extern IntPtr FindWindow(string sClass, string sWindow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
// private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, long x , long y , long cx,long cy);
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_SHOWWINDOW = 0x0040;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost =true;
Form2 frm = new Form2();
frm.Show();
}
ThreadStart childref;
Thread childThread;
private void button1_Click(object sender, EventArgs e)
{
childref = new ThreadStart(seguir);
childThread = new Thread(childref);
childThread.Start();
}
private void seguir()
{
while (1 == 1)
{
RECT r = new RECT();
IntPtr hWnd = FindWindow(null, "Form2");
if (!hWnd.Equals(IntPtr.Zero))
{
// SW_SHOWMAXIMIZED to maximize the window
// SW_SHOWMINIMIZED to minimize the window
// SW_SHOWNORMAL to make the window be normal size
GetWindowRect(hWnd, out r);
//MessageBox.Show("bottom" + r.Bottom + "top" + r.Top + "right" + r.Right + "left" + r.Left);
// Rectangle screen = Screen.FromHandle(hWnd).Bounds;
// pt = new Point(screen.Left + screen.Width / 2 - (r.Right - r.Left) / 2, screen.Top + screen.Height / 2 - (r.Bottom - r.Top) / 2);
}
hWnd = FindWindow(null, "Form1");
if (!hWnd.Equals(IntPtr.Zero))
{
// SW_SHOWMAXIMIZED to maximize the window
// SW_SHOWMINIMIZED to minimize the window
// SW_SHOWNORMAL to make the window be normal size
//SetWindowPos(hWnd,IntPtr.Zero, r.Left, r.Top, r.Right, r.Bottom);
//MessageBox.Show("bottom" + r.Bottom + "top" + r.Top + "right" + r.Right + "left" + r.Left);
//SetWindowPos(hWnd, IntPtr.Zero, pt.X, pt.Y, 0, 0);
SetWindowPos(hWnd, IntPtr.Zero, r.Left, r.Top, r.Right, r.Bottom, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
childThread.Abort();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
childThread.Abort();
}
}
}
Valora esta pregunta


0