作者: 乔克斯
查看: 2636|回复: 1
打印 上一主题 下一主题

[源码] 【控件截图】实现普通控件截图 与 Panel跨越滚动条截图

[复制链接]
跳转到指定楼层
楼主
乔克斯 发表于 2014-7-25 16:26:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 2636|回复: 1
实现普通控件截图 与 Panel跨越滚动条截图

使用方法
[C#] 纯文本查看 复制代码
GetControl(panel1).Save(@"C:/1.bmp");


全部代码

[C#] 纯文本查看 复制代码
/// <summary>
/// 传入控件取得控件的图像
/// </summary>
/// <param name="cl">控件</param>
/// <returns>Bitmap控件截图</returns>
public static Bitmap GetControl(Control cl) {
    Bitmap _BitMap = null;
    //判断如果是Panel,则调用Panel滚动截图的截图方法
    if (cl is Panel) {
        Panel c = (Panel)cl;
        bool _PanelAotu = c.AutoScroll;
        Size _PanelSize = c.Size;
        c.Visible = false;
        c.AutoScroll = true;
        MoveBar(0, 0, c);
        MoveBar(1, 0, c);
        Point _Point = GetScrollPoint(c);
        c.Width += _Point.X + 5;
        c.Height += _Point.Y + 5;
        _BitMap = new Bitmap(c.Width, c.Height);
        c.DrawToBitmap(_BitMap, new Rectangle(0, 0, _BitMap.Width, _BitMap.Height));
        c.AutoScroll = _PanelAotu;
        c.Size = _PanelSize;
        c.Visible = true;
    } else {
        //否则使用普通的控件截图方法
        _BitMap = new Bitmap(cl.Width, cl.Height);
        cl.DrawToBitmap(_BitMap, cl.ClientRectangle);
    }

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

#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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 转播转播
回复 论坛版权

使用道具 举报

沙发
hu13779472390 发表于 2016-1-29 18:19:07 | 只看该作者
感谢分享,学了些滚动条截图思想
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-11-16 15:48, Processed in 0.488830 second(s), 28 queries , Gzip On.

快速回复 返回顶部 返回列表