C#PANEL跨越滚动条截图 以前的效果不是很理想.改了下

使用

 

ZgkeLib.ControlImage.GetPanel(panel1).Save(@"C:/1.bmp");

 

 

全部代码

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. namespace ZgkeLib
  8. {
  9.     /// <summary>
  10.     /// zgke@sina.com
  11.     /// qq:116149
  12.     /// Panel截图
  13.     /// </summary>
  14.     public class ControlImage
  15.     {
  16.         #region API
  17.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  18.         public struct SCROLLINFO
  19.         {
  20.             public uint cbSize;
  21.             public uint fMask;
  22.             public int nMin;
  23.             public int nMax;
  24.             public uint nPage;
  25.             public int nPos;
  26.             public int nTrackPos;
  27.         }
  28.         public enum ScrollBarInfoFlags
  29.         {
  30.             SIF_RANGE = 0x0001,
  31.             SIF_PAGE = 0x0002,
  32.             SIF_POS = 0x0004,
  33.             SIF_DISABLENOSCROLL = 0x0008,
  34.             SIF_TRACKPOS = 0x0010,
  35.             SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
  36.         }
  37.         public enum ScrollBarRequests
  38.         {
  39.             SB_LINEUP = 0,
  40.             SB_LINELEFT = 0,
  41.             SB_LINEDOWN = 1,
  42.             SB_LINERIGHT = 1,
  43.             SB_PAGEUP = 2,
  44.             SB_PAGELEFT = 2,
  45.             SB_PAGEDOWN = 3,
  46.             SB_PAGERIGHT = 3,
  47.             SB_THUMBPOSITION = 4,
  48.             SB_THUMBTRACK = 5,
  49.             SB_TOP = 6,
  50.             SB_LEFT = 6,
  51.             SB_BOTTOM = 7,
  52.             SB_RIGHT = 7,
  53.             SB_ENDSCROLL = 8
  54.         }
  55.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  56.         public static extern int GetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si);
  57.         [DllImport("user32")]
  58.         public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool Rush);
  59.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  60.         public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
  61.         #endregion
  62.         public static Bitmap GetPanel(Panel p_Panel)
  63.         {
  64.             bool _PanelAotu = p_Panel.AutoScroll;
  65.             Size _PanelSize =p_Panel.Size;
  66.             p_Panel.Visible = false;
  67.             p_Panel.AutoScroll = true;          
  68.             MoveBar(0, 0, p_Panel);
  69.             MoveBar(1, 0, p_Panel);
  70.             Point _Point = GetScrollPoint(p_Panel);
  71.             p_Panel.Width += _Point.X+5;
  72.             p_Panel.Height += _Point.Y+5;
  73.             Bitmap _BitMap = new Bitmap(p_Panel.Width, p_Panel.Height);
  74.             p_Panel.DrawToBitmap(_BitMap, new Rectangle(0, 0, _BitMap.Width, _BitMap.Height));
  75.             p_Panel.AutoScroll = _PanelAotu;
  76.             p_Panel.Size = _PanelSize;
  77.             p_Panel.Visible = true;
  78.         
  79.             return _BitMap;
  80.         }
  81.    
  82.         /// <summary>
  83.         /// 获取滚动条数据
  84.         /// </summary>
  85.         /// <param name="MyControl"></param>
  86.         /// <param name="ScrollSize"></param>
  87.         /// <returns></returns>
  88.         private static Point GetScrollPoint(Control MyControl)
  89.         {
  90.             Point MaxScroll = new Point();
  91.             SCROLLINFO ScrollInfo = new SCROLLINFO();
  92.             ScrollInfo.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(ScrollInfo);
  93.             ScrollInfo.fMask = (uint)ScrollBarInfoFlags.SIF_ALL;
  94.             GetScrollInfo(MyControl.Handle, 1, ref ScrollInfo);
  95.             MaxScroll.Y = ScrollInfo.nMax - (int)ScrollInfo.nPage;
  96.             if ((int)ScrollInfo.nPage == 0) MaxScroll.Y = 0;
  97.             GetScrollInfo(MyControl.Handle, 0, ref ScrollInfo);
  98.             MaxScroll.X = ScrollInfo.nMax - (int)ScrollInfo.nPage;
  99.             if ((int)ScrollInfo.nPage == 0) MaxScroll.X = 0;
  100.             return MaxScroll;
  101.         }
  102.         /// <summary>
  103.         /// 移动控件滚动条位置
  104.         /// </summary>
  105.         /// <param name="Bar"></param>
  106.         /// <param name="Point"></param>
  107.         /// <param name="MyControl"></param>
  108.         private static void MoveBar(int Bar, int Point, Control MyControl)
  109.         {
  110.             if (Bar == 0)
  111.             {
  112.                 SetScrollPos(MyControl.Handle, 0, Point, true);
  113.                 SendMessage(MyControl.Handle, (int)0x0114, (int)ScrollBarRequests.SB_THUMBPOSITION, 0);
  114.             }
  115.             else
  116.             {
  117.                 SetScrollPos(MyControl.Handle, 1, Point, true);
  118.                 SendMessage(MyControl.Handle, (int)0x0115, (int)ScrollBarRequests.SB_THUMBPOSITION, 0);
  119.             }
  120.         }
  121.     }
  122. }
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值