使用SendMessage方法对窗体上的控件进行截图,该方法的思想就是把控件的句柄拿到,对控件发送WM_PAINT消息,并且把希望得到图形对象的句柄当作wParam参数传过去,这样就可以在图形对象得到想要得图形。

使用SendMessage方法对窗体上的控件进行截图,该方法的思想就是把控件的句柄拿到,对控件发送WM_PAINT消息,并且把希望得到图形对象的句柄当作wParam参数传过去,这样就可以在图形对象得到想要得图形。

 

[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);

 

// 定义WM_PAINT消息

public const int WM_PAINT = 0x00f;

 

private void button1_Click(object sender, System.EventArgs e)

                {

                        // 得到控件

                        Control ctrl = this.radioButton1;

                        // 建立图形对象

                        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);

                        }

                        // 将图显示在PictureBox

                        this.pictureBox1.Image = drawSurface;

                }

 

你可能感兴趣的:(object,user,button,图形)