实现用sendMessage()方法模拟循环点击按钮。界面有两个按钮,参数flag刚载入时是false,当我点击第一个按钮时令flag = true,然后执行while(flag == true) { sendMessage(参数省略)},另一个按钮中设置了flag=false,当我点击第一个按钮时执行循环代码,但当点击第二个按钮时循环不停止,好像第二个按钮死了!!!不知道为什么????怎么解决???

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;
    using Microsoft.Win32;namespace one
    {
        public partial class Form1 : Form
        {        [DllImport("User32.dll ")]
            public static extern System.IntPtr FindWindowEx(System.IntPtr parent, System.IntPtr childe, string strclass, string strname);        [DllImport("User32.dll", EntryPoint = "SendMessage")]
            private static extern int SendMessage(
            IntPtr hWnd, // handle to destination window
            int Msg, // message
            IntPtr wParam, // first message parameter
            int lParam // second message parameter
            );
            bool flag = false;       
            public Form1()
            {
                InitializeComponent();        }
             private void button1_Click(object sender, System.EventArgs e)
             {             const int BM_CLICK = 0xF5;
             
              
                 IntPtr hwndChildNext = FindWindowEx(父窗口句柄, System.IntPtr.Zero, "Button", "下页");             while(flag == true) {
                     SendMessage(hwndChildNext, BM_CLICK, System.IntPtr.Zero, 0);
                     Thread.Sleep(1000);
                    
                 }
             
             }                private void button2_Click(object sender, EventArgs e)
            {
                flag = false;
                
            }       
        }
    }
      

  2.   

    SendMessage?是不是在里面死循环了,它干啥用的
      

  3.   

    把BUTTON1里的代码写到一个线程里试试
      

  4.   

    我用Timer控件解决了!!!!谢谢各位了!!