关于打印panel的问题

simon_c 2009-02-16 05:08:05
 [System.Runtime.InteropServices.DllImport("gdi32.dll ")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = panelReports.CreateGraphics();
Size s = panelReports.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, panelReports.ClientRectangle.Width, panelReports.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void stbtnPrint_Click(object sender, EventArgs e)
{
CaptureScreen();
printPreviewDialog1.Width = 800;
printPreviewDialog1.Height = 600;
printPreviewDialog1.Top = 20;
printPreviewDialog1.Left = 10;
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}

打印代码如上,但是如果panel有滚动条出现的话,就只能打印当前窗口的部分了,怎么能打印全了呀
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
simon_c 2009-02-17
  • 打赏
  • 举报
回复
谢谢[wangping_li]
没注意看,加分不及时了,呵
wangping_li 2009-02-16
  • 打赏
  • 举报
回复
ZgkeLib.ControlImage.GetPanel(Panel名称)
这个取出来就是你要打印的东西
wangping_li 2009-02-16
  • 打赏
  • 举报
回复

把下面的代码封装在一个类里,使用的时候如:
ZgkeLib.ControlImage.GetPanel(panel1).Save(@"C:\1.bmp");

这样就把整个panel保存到c盘了,取的图是带了滚动条的,
是根据panel滚动条的长度进行截图



using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ZgkeLib
{
/// <summary>
/// zgke@sina.com
/// qq:116149
/// Panel截图
/// </summary>
public class ControlImage
{
#region API
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
public enum ScrollBarInfoFlags
{
SIF_RANGE = 0x0001,
SIF_PAGE = 0x0002,
SIF_POS = 0x0004,
SIF_DISABLENOSCROLL = 0x0008,
SIF_TRACKPOS = 0x0010,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
}
public enum ScrollBarRequests
{
SB_LINEUP = 0,
SB_LINELEFT = 0,
SB_LINEDOWN = 1,
SB_LINERIGHT = 1,
SB_PAGEUP = 2,
SB_PAGELEFT = 2,
SB_PAGEDOWN = 3,
SB_PAGERIGHT = 3,
SB_THUMBPOSITION = 4,
SB_THUMBTRACK = 5,
SB_TOP = 6,
SB_LEFT = 6,
SB_BOTTOM = 7,
SB_RIGHT = 7,
SB_ENDSCROLL = 8
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si);
[DllImport("user32")]
public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool Rush);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
#endregion

public static Bitmap GetPanel(Panel p_Panel)
{
bool _PanelAotu = p_Panel.AutoScroll;
Size _PanelSize =p_Panel.Size;
p_Panel.Visible = false;

p_Panel.AutoScroll = true;

MoveBar(0, 0, p_Panel);
MoveBar(1, 0, p_Panel);
Point _Point = GetScrollPoint(p_Panel);

p_Panel.Width += _Point.X+5;
p_Panel.Height += _Point.Y+5;

Bitmap _BitMap = new Bitmap(p_Panel.Width, p_Panel.Height);
p_Panel.DrawToBitmap(_BitMap, new Rectangle(0, 0, _BitMap.Width, _BitMap.Height));


p_Panel.AutoScroll = _PanelAotu;
p_Panel.Size = _PanelSize;

p_Panel.Visible = true;

return _BitMap;
}


/// <summary>
/// 获取滚动条数据
/// </summary>
/// <param name="MyControl"></param>
/// <param name="ScrollSize"></param>
/// <returns></returns>
private static Point GetScrollPoint(Control MyControl)
{
Point MaxScroll = new Point();

SCROLLINFO ScrollInfo = new SCROLLINFO();
ScrollInfo.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(ScrollInfo);
ScrollInfo.fMask = (uint)ScrollBarInfoFlags.SIF_ALL;

GetScrollInfo(MyControl.Handle, 1, ref ScrollInfo);
MaxScroll.Y = ScrollInfo.nMax - (int)ScrollInfo.nPage;
if ((int)ScrollInfo.nPage == 0) MaxScroll.Y = 0;
GetScrollInfo(MyControl.Handle, 0, ref ScrollInfo);
MaxScroll.X = ScrollInfo.nMax - (int)ScrollInfo.nPage;
if ((int)ScrollInfo.nPage == 0) MaxScroll.X = 0;
return MaxScroll;
}
/// <summary>
/// 移动控件滚动条位置
/// </summary>
/// <param name="Bar"></param>
/// <param name="Point"></param>
/// <param name="MyControl"></param>
private static void MoveBar(int Bar, int Point, Control MyControl)
{
if (Bar == 0)
{
SetScrollPos(MyControl.Handle, 0, Point, true);
SendMessage(MyControl.Handle, (int)0x0114, (int)ScrollBarRequests.SB_THUMBPOSITION, 0);
}
else
{
SetScrollPos(MyControl.Handle, 1, Point, true);
SendMessage(MyControl.Handle, (int)0x0115, (int)ScrollBarRequests.SB_THUMBPOSITION, 0);
}
}
}
}

simon_c 2009-02-16
  • 打赏
  • 举报
回复
顶自己一下

110,476

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧