To Mangkee(方程)using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.Threading; using System.Runtime.InteropServices; using System.Drawing.Imaging; namespace currentmoseposition { /// <summary> /// Form1 的摘要说明。 /// </summary> public class positionwindownname : System.Windows.Forms.Form { private void BackgroundProcess() { IntPtr windowHWND; string findwindowname;
while(true) { Monitor.Enter(this);
findwindowname=textBox2.Text;
IntPtr ParenthWnd = new IntPtr(0); IntPtr hWnd = new IntPtr(0); ParenthWnd=FindWindow(lpszParentClass,findwindowname); windowHWND=ParenthWnd; Point xywindowposition; xywindowposition.x=Cursor.Position.X; xywindowposition.y=Cursor.Position.Y; positionwindow=WindowFromPoint(xywindowposition); textBox4.Text=Convert.ToString(positionwindow);
int getname=GetWindowText(positionwindow,title,256);
有没有用到多线程,发生了死锁现象。
开始可以运行的,死锁的可能不大吧~TO:top1000(天天向上)
你所说的异常都包括什么呢!TO:njbaige(白鸽)
开始运行是都很正常,大概一个小时后就不会动了,去任务管理器看的时候,已经停止响应了!初次做东西,还请各位帮忙,赐教!谢了!
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace currentmoseposition
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class positionwindownname : System.Windows.Forms.Form
{ private void BackgroundProcess()
{
IntPtr windowHWND;
string findwindowname;
while(true)
{ Monitor.Enter(this);
findwindowname=textBox2.Text;
IntPtr ParenthWnd = new IntPtr(0);
IntPtr hWnd = new IntPtr(0);
ParenthWnd=FindWindow(lpszParentClass,findwindowname);
windowHWND=ParenthWnd; Point xywindowposition;
xywindowposition.x=Cursor.Position.X;
xywindowposition.y=Cursor.Position.Y;
positionwindow=WindowFromPoint(xywindowposition);
textBox4.Text=Convert.ToString(positionwindow);
int getname=GetWindowText(positionwindow,title,256);
textBox2.Text=Convert.ToString(title); IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero); // 从指定设备的句柄创建新的 Graphics 对象 Graphics gfxDisplay = Graphics.FromHdc(hdlDisplay); // 创建只有一个象素大小的 Bitmap 对象 Bitmap bmp = new Bitmap(1, 1, gfxDisplay); // 从指定 Image 对象创建新的 Graphics 对象 Graphics gfxBmp = Graphics.FromImage(bmp); // 获得屏幕的句柄 IntPtr hdlScreen = gfxDisplay.GetHdc(); // 获得位图的句柄 IntPtr hdlBmp = gfxBmp.GetHdc(); // 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中 BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, MousePosition.X, MousePosition.Y, 13369376); // 释放屏幕句柄 gfxDisplay.ReleaseHdc(hdlScreen); // 释放位图句柄 gfxBmp.ReleaseHdc(hdlBmp);
/* textBox3.BackColor = bmp.GetPixel(0, 0); // 获取像素的颜色 */ textBox3.Text = "0x" + bmp.GetPixel(0, 0).ToArgb().ToString("x").ToUpper(); gfxDisplay.Dispose();
gfxBmp.Dispose(); bmp.Dispose(); // 释放 bmp 所使用的资源 /*
if (!ParenthWnd.Equals(IntPtr.Zero))
{
MessageBox.Show("i find a window"+textBox2.Text);
t1.Suspend(); } */
// 向列表框增加一个项目
textBox1.Text=Convert.ToString(Cursor.Position.X)+" "+Convert.ToString(Cursor.Position.Y); Monitor.Exit(this);
Thread.Sleep(20); // 指定线程休眠的时间
}
}
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.TextBox textBox9;
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private System.ComponentModel.Container components = null;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point position);
[DllImport("user32.dll")]
public static extern int GetWindowText( IntPtr hWnd, StringBuilder title, int size);
[ DllImport ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
int dwRop // 光栅的操作值
);
[ DllImport ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);
StringBuilder title = new StringBuilder(256);
StringBuilder module = new StringBuilder(256);
public struct Point
{
public int x;
public int y;
} string lpszParentClass =null;
Thread t1; IntPtr positionwindow; 看看这个小工具为什么也会一段时间后停止运行呢~
谢谢了!
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.textBox7 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox8 = new System.Windows.Forms.TextBox();
this.textBox9 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(0, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(432, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(0, 56);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(192, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "textBox3";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(0, 80);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(432, 21);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "textBox4";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(0, 112);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(432, 21);
this.textBox5.TabIndex = 4;
this.textBox5.Text = "textBox5";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(176, 0);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 5;
this.textBox6.Text = "textBox6";
//
// textBox7
//
this.textBox7.Location = new System.Drawing.Point(304, 0);
this.textBox7.Name = "textBox7";
this.textBox7.TabIndex = 6;
this.textBox7.Text = "textBox7";
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 144);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(0, 144);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(216, 21);
this.textBox8.TabIndex = 8;
this.textBox8.Text = "textBox8";
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(216, 56);
this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(216, 21);
this.textBox9.TabIndex = 9;
this.textBox9.Text = "textBox9";
//
// positionwindownname
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 190);
this.Controls.Add(this.textBox9);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox7);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "positionwindownname";
this.Text = "positionwindownname";
this.TopMost = true;
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.Enter += new System.EventHandler(this.Form1_Enter);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new positionwindownname());
} private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text=Convert.ToString(Cursor.Position.X)+" "+Convert.ToString(Cursor.Position.Y); } private void Form1_Load(object sender, System.EventArgs e)
{
// 说明为窗体类成员
t1 = new Thread(new ThreadStart(BackgroundProcess));
t1.Start(); // 以上2行放置在窗体的load事件中
}
public static string GetPositionColor(int x,int y) //得到当前指定的颜色 返回值是这种颜色的RGB值的 字符串形式
{
// 创建显示器的DC IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero); // 从指定设备的句柄创建新的 Graphics 对象 Graphics gfxDisplay = Graphics.FromHdc(hdlDisplay); // 创建只有一个象素大小的 Bitmap 对象 Bitmap bmp = new Bitmap(1, 1, gfxDisplay); // 从指定 Image 对象创建新的 Graphics 对象 Graphics gfxBmp = Graphics.FromImage(bmp); // 获得屏幕的句柄 IntPtr hdlScreen = gfxDisplay.GetHdc(); // 获得位图的句柄 IntPtr hdlBmp = gfxBmp.GetHdc(); // 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中 BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, x, y, 13369376); // 释放屏幕句柄 gfxDisplay.ReleaseHdc(hdlScreen); // 释放位图句柄 gfxBmp.ReleaseHdc(hdlBmp);
/* lblColor.BackColor = bmp.GetPixel(0, 0); // 获取像素的颜色
*/ string color = "0x" + bmp.GetPixel(0, 0).ToArgb().ToString("x").ToUpper(); gfxDisplay.Dispose();
gfxBmp.Dispose(); bmp.Dispose(); // 释放 bmp 所使用的资源
return color; } private void Form1_Closed(object sender, System.EventArgs e)
{
t1.Abort();
} private void textBox1_TextChanged(object sender, System.EventArgs e)
{
} private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
} private void Form1_Enter(object sender, System.EventArgs e)
{
textBox5.Text=textBox2.Text;
} private void button1_Click_1(object sender, System.EventArgs e)
{
Point xywindowposition;
xywindowposition.x=Convert.ToInt16(textBox6.Text);
xywindowposition.y=Convert.ToInt16(textBox7.Text);
positionwindow=WindowFromPoint(xywindowposition);
textBox8.Text=Convert.ToString(positionwindow); textBox9.Text=GetPositionColor(Convert.ToInt16(textBox6.Text),Convert.ToInt16(textBox7.Text));
int getname=GetWindowText(positionwindow,title,256);
textBox5.Text=Convert.ToString(title);
}
}
}麻烦了~
好像你是调用了user32.dll 中的函数,做底层的Win32 操作,在线程中调试错误不是很方便
(有错了也反映不出来),你可以先将线程取消调,直接通过一个方法,运行,那样出错在
什么地方,提示信息也会有的,在方法中调试OK 了,再放到线程中去关键是你的方法BackgroundProcess 中会出问题这个只是我的看法了,也帮不上多少忙,见凉~~
汗颜……
不过看你用到了线程,应该是这里出的问题。慢慢调试一下吧。
一部分一部分的调试。
不要整个程序调试。