已经知道图片控件的名称(例如"S_192_168_0_138_3A_4D")
如何取得该控件?
就是想根据图片控件的名称更改控件的一些属性,非常的急,在线等,简单问题,得到答案马上结贴.

解决方案 »

  1.   

    如果是进程内,只要遍历主窗体的Controls集合就行了.
    如果是进程外,就要调用API函数FindWindow()了
      

  2.   

    进程内的,遍历主窗体的Controls集合的方法是什么样的,请举个例子,非常的急啊.
    其实简单地说就是代码生成了很多个图片控件,每个控件我都定义了一个不同的名称,现在想根据这个名称更改该控件的属性.
      

  3.   

    使用foreach在controls内递归遍历吧。
      

  4.   

    for (int i=0;i<this.Controls.Count;i++)
    {
    if (this.Controls[i].Name=="XXX")
    {
    PictureBox p=(PictureBox)this.Controls[i];
    ...
    }
    }
      

  5.   

    foreach (Control ctr in this.Controls) {
    if (ctr.Name == "YourControlName")
    return ctr;    
    }
      

  6.   

    private IntPtr hwnd; //查找结果private void FindSubControl(Control.ControlCollection con)
    {
    foreach (Control control in con)
    {
    if (control.GetType().ToString() == "System.Windows.Forms.PictureBox")
    {
    this.hwnd = control.Handle; //如果已找到即赋句柄
    }
    else if (control.HasChildren)
    FindSubControl (control.Controls);
    }
    }private void button1_Click(object sender, System.EventArgs e)
    {
    FindSubControl( this.Controls );
    }
      

  7.   

    //纠正一下private IntPtr hwnd; //查找结果private void FindSubControl(Control.ControlCollection con)
    {
    foreach (Control control in con)
    {
    if (control.GetType().ToString() == "System.Windows.Forms.PictureBox")
    {
    if (control.Name == "xxx")
    {
    this.hwnd = control.Handle; //如果已找到即赋句柄
    return;
    }
    }
    else if (control.HasChildren) //如果当前不是,并且有子控件继续查找
    FindSubControl (control.Controls);
    }
    }private void button1_Click(object sender, System.EventArgs e)
    {
    FindSubControl( this.Controls ); //开始查找
    }
      

  8.   

    不用获取句柄(Handle)
    void Control FindControl(string ctrlName)
    {
     foreach (Control ctrl in this.Controls) {
      if (ctrl.Name == ctrlName))
       return ctrl;    
     }
     return null;
    }不过如果你的控件不是只接放在窗体上的(如 放在窗体内的另一个Panel中),就要这样了:void Control FindControl(Control parent, string ctrlName)
    {
     foreach (Control ctrl in parent.Controls) {
      if (ctrl.Name == ctrlName))
       return ctrl;
      else
      {
        Control searched = FindControl(ctrl, ctrlName);
         if(searched != null) return searched;
      }
     }
     return null;
    }