跨滚动条截图

最近项目需要一个图片生成功能,也简单!可偏偏是要生成有滚动条的一个控件.

思考后两种解决方案:  1 数据源直接读取生成个图片效果

2 对控件使用截图

1方案的代码比较复杂且实现的效果不佳,使用2方案!

因为要抓取的是c#的一个控件.所以首先查找msdn和csdn.无解!~~!

没办法搜索引擎开足马力,终于发现一个调用api的小方法,可以轻松实现.所以写点东西以便于下次温故


废话不多说了,直接代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GZMS
{
/// <summary>
/// 滚动条截图
/// </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
/// <summary>
/// TreeView截图
/// </summary>
/// <param name="_View"></param>
/// <returns></returns>
public static Bitmap GetTreeView(TreeView _View)
{
bool _TreeViewAotu = _View.Scrollable;

Size _TreeViewSize = _View.Size;
_View.Visible = false;
_View.Scrollable = true;

Point _Point = GetScrollPoint(_View);
for (int i = 0; i <=_Point.Y; i++)
{
MoveBar(i, 0, _View);
_View.Height += _Point.Y + 10;
}
_View.Width += _Point.X + 5;

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

_View.Scrollable = _TreeViewAotu;
_View.Size = _TreeViewSize;
_View.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);
}
}
}

 

}

 

仅供参考或收藏.O(∩_∩)O~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值