我原来问过这样一个问题:
如何将控件的图形截图出来(控件不出现)有位好心的人给了我这样的答案:private void Form1_Load(object sender, EventArgs e)
{
Control ctrl = new Button();
if (!ctrl.IsHandleCreated)
{
ctrl.CreateControl();
}
Bitmap drawSurface = new Bitmap(ctrl.Width, ctrl.Height);using (Graphics g = Graphics.FromImage(drawSurface))
{
IntPtr hDc = g.GetHdc();
SendMessage(ctrl.Handle, WM_PAINT, hDc, IntPtr.Zero);
g.ReleaseHdc(hDc);
}
this.BackgroundImage = drawSurface;
}public const int WM_PAINT = 0x00f;[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
该答案完全正确,但有点不足,就是象textbox、listbox等这些控件截出来的图,没有图形,是透明的现在还想把这些控件也能截出图,应该怎么改?
也就是说代码希望能把所有的控件都能截出图,能做到吗,希望指点,感激不尽~~!!!

解决方案 »

  1.   

    不要放到load事件中去做,或者在之前加上this.Show()这个语句。
      

  2.   

    我是放在一个函数中的:private PictureBox Capture(){
                Control ctrl = new TextBox();
                if (!ctrl.IsHandleCreated)
                {
                    ctrl.CreateControl();
                }
                Bitmap drawSurface = new Bitmap(ctrl.Width, ctrl.Height);
                using (Graphics g = Graphics.FromImage(drawSurface))
                {
                    IntPtr hDc = g.GetHdc();
                    SendMessage(ctrl.Handle, WM_PAINT, hDc, IntPtr.Zero);
                    g.ReleaseHdc(hDc);
                }
                pictureBox1.Image = drawSurface;
                return pictureBox1;
    }
    这样做应该怎么做呢,能不能做?
    this.show()应该放在什么地方呢~~
      

  3.   

    你可以在OnPaint里面调用这个函数~~
      

  4.   

    但这样不行,我需要在某个事件触发的时候,才执行这个函数,不是一来就生成
    如:
    button1.Click(){
     this.Capture();
    }所以不能放在Load或OnPaint中
      

  5.   

    private PictureBox Capture(){
    Control ctrl = new TextBox();
    if (!ctrl.IsHandleCreated)
    {
    ctrl.CreateControl();
    }
    Bitmap drawSurface = new Bitmap(ctrl.Width, ctrl.Height);
    using (Graphics g = Graphics.FromImage(drawSurface))
    {
    IntPtr hDc = g.GetHdc();
    SendMessage(ctrl.Handle, WM_PAINT, hDc, IntPtr.Zero);
    g.ReleaseHdc(hDc);
    }
    pictureBox1.Image = drawSurface;
    return pictureBox1;
    }
    //做的是类似VS。NET的一个东西,左边有工具箱,从工具箱中拖控键到某个控键上,这时需要加载该控键的图片以代替该控键
    private void mvObject_DragDrop(object sender, DragEventArgs e){PictureBox tpPicBox = this.Capture();this.controls.Add(tpPicBox);
    }
      

  6.   

    你先在
    pictureBox1.Image = drawSurface;
    之后,把drawSurface存成文件,看是否正确。
      

  7.   

    private PictureBox Capture(){
    Control ctrl = new TextBox();////添加得代码
    if(ctrl is TextBox){
        this.contrls.add(ctrl);
        this.show();
    }
    if (!ctrl.IsHandleCreated)
    {
    ctrl.CreateControl();
    }
    Bitmap drawSurface = new Bitmap(ctrl.Width, ctrl.Height);
    using (Graphics g = Graphics.FromImage(drawSurface))
    {
    IntPtr hDc = g.GetHdc();
    SendMessage(ctrl.Handle, WM_PAINT, hDc, IntPtr.Zero);
    g.ReleaseHdc(hDc);
    }////添加得代码
    if(ctrl is TextBox){
        this.controls.remove(ctrl);
    }
    ////pictureBox1.Image = drawSurface;
    return pictureBox1;
    }
    这样到时可以得到图,但位置不对,而且我不知道有哪些控键是想TextBox一样得,缺乏通用性~~
    如果只能这样得话,那也没办法,毕竟还是能得到图不知道还有没有更好点得方法`~`~,,谢谢`~`~