我原来问过这样一个问题:
如何将控件的图形截图出来(控件不出现)有位好心的人给了我这样的答案: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等这些控件截出来的图,没有图形,是透明的现在还想把这些控件也能截出图,应该怎么改?
也就是说代码希望能把所有的控件都能截出图,能做到吗,希望指点,感激不尽~~!!!
如何将控件的图形截图出来(控件不出现)有位好心的人给了我这样的答案: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等这些控件截出来的图,没有图形,是透明的现在还想把这些控件也能截出图,应该怎么改?
也就是说代码希望能把所有的控件都能截出图,能做到吗,希望指点,感激不尽~~!!!
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()应该放在什么地方呢~~
如:
button1.Click(){
this.Capture();
}所以不能放在Load或OnPaint中
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);
}
pictureBox1.Image = drawSurface;
之后,把drawSurface存成文件,看是否正确。
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一样得,缺乏通用性~~
如果只能这样得话,那也没办法,毕竟还是能得到图不知道还有没有更好点得方法`~`~,,谢谢`~`~