新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → WINFROM下带有自动滚动条截图 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7434 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: WINFROM下带有自动滚动条截图 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 WINFROM下带有自动滚动条截图


    WINFROM下带有自动滚动条截图

    根据滚动条的长度进行截图,发图复制到一起

    使用PANEL 如果是窗体请把窗体放到PANEL里 要不会多出标题来

    下面是全部代码

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

    namespace ZgkeLib
    {
        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>
            /// 获取图形
            /// </summary>
            /// <param name="MyControl"></param>
            /// <returns></returns>
            public static Bitmap GetImage(Control MyControl)
            {
                Point ScrollMaxInfo=GetScrollPoint(MyControl);
                Size ControlMaxSize = GetControlSize(MyControl, ScrollMaxInfo);

                Bitmap ControlImage = new Bitmap(ControlMaxSize.Width, ControlMaxSize.Height);
                Graphics MainGraphics = Graphics.FromImage(ControlImage);
                Bitmap TempImage = new Bitmap(MyControl.Width-16, MyControl.Height-16);

                int StepWidth = GetScrollNumb(MyControl.Width-16, ScrollMaxInfo.X);
                int StepHeight = GetScrollNumb(MyControl.Height-16, ScrollMaxInfo.Y);

                int LogWidth = 0;
                int LogHeigh = 0;
                MoveBar(0, 0, MyControl);
               
                for (int i = 0; i != StepWidth; i++)
                {
                    MoveBar(1, 0, MyControl);
                    LogHeigh = 0;
                    for (int z = 0; z != StepHeight; z++)
                    {
                        MyControl.DrawToBitmap(TempImage, new Rectangle(0, 0, MyControl.Width-16, MyControl.Height-16));
                        MainGraphics.DrawImage(TempImage, LogWidth, LogHeigh);                   
                        MoveBar(1, MyControl.Height-16+LogHeigh, MyControl);
                        LogHeigh=GetScrollPoint(1, MyControl);
                    }
                    MoveBar(0, MyControl.Width-16 + LogWidth, MyControl);
                    LogWidth = GetScrollPoint(0, MyControl);
                }
                MainGraphics.Dispose();
                TempImage.Dispose();
                return ControlImage;
            }
            /// <summary>
            /// 获取目前滚动条位置
            /// </summary>
            /// <param name="Bar"></param>
            /// <param name="MyControl"></param>
            /// <returns></returns>
            public static int GetScrollPoint(int Bar, Control MyControl)
            {         
                SCROLLINFO ScrollInfo = new SCROLLINFO();
                ScrollInfo.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(ScrollInfo);
                ScrollInfo.fMask = (uint)ScrollBarInfoFlags.SIF_ALL;

                GetScrollInfo(MyControl.Handle, Bar, ref ScrollInfo);

                return ScrollInfo.nPos;
            }
            /// <summary>
            /// 获取循环几次获得图形
            /// </summary>
            /// <returns></returns>
            private static int GetScrollNumb(int MyControlNumb, int ScrollMaxInfoNumb)
            {
                double Step = (double)ScrollMaxInfoNumb / (double)MyControlNumb;
                Step++;

                if ((int)Step == Step)
                {
                    return (int)Step;
                }
                else
                {
                    return (int)Step+1;
                }
            }
            /// <summary>
            /// 获得控件不需要滚动条的宽和高(最终图形大小)
            /// </summary>
            /// <param name="MyControl"></param>
            /// <param name="ScrollMaxInfo"></param>
            /// <returns></returns>
            private static Size GetControlSize(Control MyControl, Point ScrollMaxInfo)
            {
                return new Size(MyControl.Size.Width + ScrollMaxInfo.X-16, MyControl.Size.Height+ScrollMaxInfo.Y-16);
            }
            /// <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);
                }
            }
        }
    }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2012/7/24 10:21:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/27 1:47:58

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    71.289ms