﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-ChengKing-文章分类-C#之Control Study</title><link>http://www.cnitblog.com/ChengKing/category/1233.html</link><description>ChengKing</description><language>zh-cn</language><lastBuildDate>Wed, 05 Oct 2011 04:19:39 GMT</lastBuildDate><pubDate>Wed, 05 Oct 2011 04:19:39 GMT</pubDate><ttl>60</ttl><item><title>Control Study -&gt; 主表和子表数据同时在绑定控件显示 </title><link>http://www.cnitblog.com/ChengKing/articles/5011.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Tue, 29 Nov 2005 15:25:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/5011.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/5011.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/5011.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/5011.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/5011.html</trackback:ping><description><![CDATA[<P>(一).显示效果图<BR></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;<A href="http://blog.csdn.net/ChengKing/archive/2005/11/27/538128.aspx">http://blog.csdn.net/ChengKing/archive/2005/11/27/538128.aspx</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>(二)代码</P>
<P>&nbsp;&nbsp; 1.前台界面代码:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="数据绑定控件同时显示主表和子表数据.WebForm1" %&gt;<BR>&lt;%@ Import Namespace="System.Data" %&gt;<BR>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &gt;<BR>&lt;HTML&gt;<BR>&nbsp;&lt;HEAD&gt;<BR>&nbsp;&nbsp;&lt;title&gt;WebForm1&lt;/title&gt;<BR>&nbsp;&nbsp;&lt;meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"&gt;<BR>&nbsp;&nbsp;&lt;meta name="CODE_LANGUAGE" Content="C#"&gt;<BR>&nbsp;&nbsp;&lt;meta name="vs_defaultClientScript" content="JavaScript"&gt;<BR>&nbsp;&nbsp;&lt;meta name="vs_targetSchema" content="<A href="http://schemas.microsoft.com/intellisense/ie5"><FONT color=#002c99>http://schemas.microsoft.com/intellisense/ie5</FONT></A>"&gt;<BR>&nbsp;&lt;/HEAD&gt;<BR>&nbsp;&lt;body MS_POSITIONING="GridLayout"&gt;<BR>&nbsp;&nbsp;&lt;form id="Form1" method="post" runat="server"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;!-- 绑定主表数据 --&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:repeater id="myRepeater" runat="server"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;HeaderTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table border="0" bgcolor="lightblue"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/HeaderTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td style="background-color: lightgray"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;b&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%# DataBinder.Eval(Container.DataItem,"TypeID") %&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%# DataBinder.Eval(Container.DataItem,"TypeName") %&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/b&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- 绑定子表数据 --&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:repeater id="childRepeater" datasource='&lt;%# ((DataRowView)Container.DataItem)<BR>&nbsp;&nbsp;&nbsp;.Row.GetChildRows("TypeRelation") %&gt;' runat="server"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;itemtemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table border="0" bgcolor="#ffcc33"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp;&amp;nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%# DataBinder.Eval(Container.DataItem,"[\"TypeID\"]") %&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%# DataBinder.Eval(Container.DataItem, "[\"TypeDetail\"]")%&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/itemtemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/asp:repeater&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;FooterTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/FooterTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:repeater&gt;</P>
<P>&nbsp;&nbsp;&lt;/form&gt;<BR>&nbsp;&lt;/body&gt;<BR>&lt;/HTML&gt;</P></BLOCKQUOTE>
<P dir=ltr>&nbsp; 2. 后代代码</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P>&nbsp;using System;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Data;<BR>using System.Drawing;<BR>using System.Web;<BR>using System.Web.SessionState;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;</P>
<P>namespace 数据绑定控件同时显示主表和子表数据<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// 数据绑定控件同时显示主表和子表数据<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class WebForm1 : System.Web.UI.Page<BR>&nbsp;{<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Repeater myRepeater;<BR>&nbsp;&nbsp;private void Page_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;DataSet ds = new DataSet();<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;//1.构造主表<BR>&nbsp;&nbsp;&nbsp;DataTable dtTypeParent=new DataTable();<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.Columns.Add(new DataColumn("TypeID",typeof(int)));<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.Columns.Add(new DataColumn("TypeName",typeof(string)));<BR>&nbsp;&nbsp;&nbsp;//给主表添加两条记录<BR>&nbsp;&nbsp;&nbsp;DataRow drParent1 = dtTypeParent.NewRow();<BR>&nbsp;&nbsp;&nbsp;drParent1["TypeID"] = 1;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drParent1["TypeName"] = "水果";&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.Rows.Add(drParent1);<BR>&nbsp;&nbsp;&nbsp;DataRow drParent2 = dtTypeParent.NewRow();<BR>&nbsp;&nbsp;&nbsp;drParent2["TypeID"] = 2;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drParent2["TypeName"] = "玩具";<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.Rows.Add(drParent2);<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.TableName="TypeParent";&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;dtTypeParent.PrimaryKey = new DataColumn[] {dtTypeParent.Columns["TypeID"]};<BR>&nbsp;&nbsp;&nbsp;ds.Tables.Add(dtTypeParent);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;//2.构造子表<BR>&nbsp;&nbsp;&nbsp;DataTable dtTypeChild=new DataTable();<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Columns.Add(new DataColumn("TypeID",typeof(int)));<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Columns.Add(new DataColumn("TypeDetail",typeof(string)));<BR>&nbsp;&nbsp;&nbsp;//给子表添加五条记录<BR>&nbsp;&nbsp;&nbsp;DataRow drChild1 = dtTypeChild.NewRow();<BR>&nbsp;&nbsp;&nbsp;drChild1["TypeID"] = 1;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drChild1["TypeDetail"] = "苹果";&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Rows.Add(drChild1);<BR>&nbsp;&nbsp;&nbsp;DataRow drChild2 = dtTypeChild.NewRow();<BR>&nbsp;&nbsp;&nbsp;drChild2["TypeID"] = 1;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drChild2["TypeDetail"] = "桔子";&nbsp;<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Rows.Add(drChild2);<BR>&nbsp;&nbsp;&nbsp;DataRow drChild3 = dtTypeChild.NewRow();<BR>&nbsp;&nbsp;&nbsp;drChild3["TypeID"] = 1;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drChild3["TypeDetail"] = "香蕉";<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Rows.Add(drChild3);<BR>&nbsp;&nbsp;&nbsp;DataRow drChild4 = dtTypeChild.NewRow();<BR>&nbsp;&nbsp;&nbsp;drChild4["TypeID"] = 2;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drChild4["TypeDetail"] = "机器人";<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Rows.Add(drChild4);<BR>&nbsp;&nbsp;&nbsp;DataRow drChild5 = dtTypeChild.NewRow();<BR>&nbsp;&nbsp;&nbsp;drChild5["TypeID"] = 2;&nbsp; <BR>&nbsp;&nbsp;&nbsp;drChild5["TypeDetail"] = "小汽车";<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.Rows.Add(drChild5);<BR>&nbsp;&nbsp;&nbsp;dtTypeChild.TableName="TypeChild";&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;ds.Tables.Add(dtTypeChild);</P>
<P>&nbsp;&nbsp;&nbsp;ds.Relations.Add("TypeRelation",<BR>&nbsp;&nbsp;&nbsp;&nbsp;ds.Tables["TypeParent"].Columns["TypeID"],<BR>&nbsp;&nbsp;&nbsp;&nbsp;ds.Tables["TypeChild"].Columns["TypeID"]);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;myRepeater.DataSource = ds.Tables["TypeParent"];<BR>&nbsp;&nbsp;&nbsp;Page.DataBind();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P><BR>&nbsp;&nbsp;#region Web 窗体设计器生成的代码<BR>&nbsp;&nbsp;override protected void OnInit(EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();<BR>&nbsp;&nbsp;&nbsp;base.OnInit(e);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Page_Load);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion<BR>&nbsp;}<BR>}<BR></P></BLOCKQUOTE>
<P dir=ltr>(三).示例下载</P>
<P dir=ltr>&nbsp;&nbsp;&nbsp; <A href="http://www.cnblogs.com/Files/ChengKing/DataRalationDisplay.rar"><FONT color=#002c99>http://www.cnblogs.com/Files/ChengKing/DataRalationDisplay.rar</FONT></A></P>
<P>&nbsp;&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/5011.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-29 23:25 <a href="http://www.cnitblog.com/ChengKing/articles/5011.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 自定义DataGrid翻页按钮</title><link>http://www.cnitblog.com/ChengKing/articles/5010.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Tue, 29 Nov 2005 15:23:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/5010.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/5010.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/5010.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/5010.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/5010.html</trackback:ping><description><![CDATA[<P>(一).显示截图效果</P>
<P>&nbsp;&nbsp; <A href="http://blog.csdn.net/ChengKing/archive/2005/11/20/533551.aspx">http://blog.csdn.net/ChengKing/archive/2005/11/20/533551.aspx</A></P>
<P>(二).代码</P>
<P>&nbsp;&nbsp; 1. *.aspx文件代码</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="New_DataGrid自定义分页按钮.WebForm1" %&gt;<BR>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &gt;<BR>&lt;HTML&gt;<BR>&nbsp;&lt;HEAD&gt;<BR>&nbsp;&nbsp;&lt;title&gt;WebForm1&lt;/title&gt;<BR>&nbsp;&nbsp;&lt;meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"&gt;<BR>&nbsp;&nbsp;&lt;meta name="CODE_LANGUAGE" Content="C#"&gt;<BR>&nbsp;&nbsp;&lt;meta name="vs_defaultClientScript" content="JavaScript"&gt;<BR>&nbsp;&nbsp;&lt;meta name="vs_targetSchema" content="<A href="http://schemas.microsoft.com/intellisense/ie5"><FONT color=#002c99>http://schemas.microsoft.com/intellisense/ie5</FONT></A>"&gt;<BR>&nbsp;&lt;/HEAD&gt;<BR>&nbsp;&lt;body MS_POSITIONING="GridLayout"&gt;<BR>&nbsp;&nbsp;&lt;form id="Form1" method="post" runat="server"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;FONT face="宋体"&gt;&lt;/FONT&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 160px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Width="768px" AllowPaging="True"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;HeaderStyle BackColor="#999900"&gt;&lt;/HeaderStyle&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;PagerStyle NextPageText="下一页" PrevPageText="上一页" Mode="NumericPages"&gt;&lt;/PagerStyle&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:datagrid&gt;<BR>&nbsp;&nbsp;&lt;/form&gt;<BR>&nbsp;&lt;/body&gt;<BR>&lt;/HTML&gt;</P>
<P>&nbsp;2. *.cs代码文件</P>
<P>&nbsp;&nbsp;&nbsp; using System;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Data;<BR>using System.Drawing;<BR>using System.Web;<BR>using System.Web.SessionState;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;</P>
<P>namespace New_DataGrid自定义分页按钮<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// DataGrid自定义分页按钮<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class WebForm1 : System.Web.UI.Page<BR>&nbsp;{<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.DataGrid DataGrid1;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;private void Page_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;if(!Page.IsPostBack)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;GetData();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 生成数据<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.BindData();&nbsp; // 绑定数据到DataGrid&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 生成数据<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void GetData()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;DataTable table = new DataTable();<BR>&nbsp;&nbsp;&nbsp;DataColumnCollection columns = table.Columns;</P>
<P>&nbsp;&nbsp;&nbsp;//定义结构<BR>&nbsp;&nbsp;&nbsp;columns.Add("学号",typeof(System.Int32));<BR>&nbsp;&nbsp;&nbsp;columns.Add("分数",typeof(System.Int32));&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;Random myRandom = new Random();<BR>&nbsp;&nbsp;&nbsp;int intScore = 0;<BR>&nbsp;&nbsp;&nbsp;for(int i = 1;i &lt; 101; i++)<BR>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;intScore = myRandom.Next(100);<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataRow drNew = table.NewRow();<BR>&nbsp;&nbsp;&nbsp;&nbsp;drNew[0] = i.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;drNew[1] = intScore.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;table.Rows.Add(drNew);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;this.ViewState.Add("table",table);&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 绑定DataGrid<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void BindData()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;DataTable table = (DataTable)this.ViewState["table"];<BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.DataSource = table;<BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.DataBind();<BR>&nbsp;&nbsp;}&nbsp;&nbsp;<BR>&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;#region Web 窗体设计器生成的代码<BR>&nbsp;&nbsp;override protected void OnInit(EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();<BR>&nbsp;&nbsp;&nbsp;base.OnInit(e);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Page_Load);<BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.ItemCreated +=new DataGridItemEventHandler(DataGrid1_ItemCreated);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = e.NewPageIndex;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = 0;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;this.BindData();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;System.Web.UI.WebControls.ListItemType elemType = e.Item.ItemType;<BR>&nbsp;&nbsp;&nbsp;if (elemType == System.Web.UI.WebControls.ListItemType.Pager) <BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;TableCell pager = (TableCell) e.Item.Controls[0];<BR>&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i&lt;pager.Controls.Count; i+=2) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object o = pager.Controls[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (o is LinkButton) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinkButton h = (LinkButton) o;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.Text = " " + h.Text + " "; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Label l = (Label) o;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l.Text = String.Format("&lt;font color='red'&gt;[第{0}页]&lt;/font&gt;", l.Text); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>&nbsp;&nbsp;<BR>}<BR></P>
<P>(三).源代码下载(创建虚拟目录后就可以直接运行)</P>
<P><A href="http://www.cnblogs.com/Files/ChengKing/自定义分页example.rar"><FONT color=#002c99>&nbsp;&nbsp;&nbsp; http://www.cnblogs.com/Files/ChengKing/自定义分页example.rar</FONT></A></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; </P><img src ="http://www.cnitblog.com/ChengKing/aggbug/5010.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-29 23:23 <a href="http://www.cnitblog.com/ChengKing/articles/5010.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study-&gt;自定义DataGrid翻页控件</title><link>http://www.cnitblog.com/ChengKing/articles/5009.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Tue, 29 Nov 2005 15:22:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/5009.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/5009.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/5009.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/5009.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/5009.html</trackback:ping><description><![CDATA[<P>(一)说明</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般情况下,使用DataGrid自带的翻页按钮就能够实现所有翻页操作了，但有时候想定制自己的翻页样式</P>
<P>&nbsp;&nbsp;&nbsp; 或美化其显示效果，这时可以自已写个翻页用户控件.</P>
<P>(二).显示效果载图</P>
<P>&nbsp;&nbsp;&nbsp; <A href="http://blog.csdn.net/ChengKing/archive/2005/11/20/533539.aspx">http://blog.csdn.net/ChengKing/archive/2005/11/20/533539.aspx</A></P>
<P>&nbsp;&nbsp;(三)代码</P>
<P>&nbsp;&nbsp; 1.*.aspx代码文件:</P>
<P>&nbsp;&nbsp;&nbsp; &lt;%@ Page language="c#" Codebehind="DGCustomerPagination.aspx.cs" AutoEventWireup="false" Inherits="New_DataGrid自定义分页事件.WebForm1" %&gt;<BR>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &gt;<BR>&lt;HTML&gt;<BR>&nbsp; &lt;HEAD&gt;<BR>&nbsp;&nbsp;&lt;title&gt;WebForm1&lt;/title&gt;<BR>&nbsp;&nbsp;&lt;meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"&gt;<BR>&nbsp;&nbsp;&lt;meta content="C#" name="CODE_LANGUAGE"&gt;<BR>&nbsp;&nbsp;&lt;meta content="JavaScript" name="vs_defaultClientScript"&gt;<BR>&nbsp;&nbsp;&lt;meta content="<A href="http://schemas.microsoft.com/intellisense/ie5"><FONT color=#002c99>http://schemas.microsoft.com/intellisense/ie5</FONT></A>" name="vs_targetSchema"&gt;<BR>&nbsp; &lt;/HEAD&gt;<BR>&nbsp;&lt;body MS_POSITIONING="GridLayout"&gt;<BR>&nbsp;&nbsp;&lt;form id="Form1" method="post" runat="server"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 160px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Width="768px" AllowPaging="True"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;HeaderStyle BackColor="#999900"&gt;&lt;/HeaderStyle&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;PagerStyle NextPageText="下一页" PrevPageText="上一页" Mode="NumericPages"&gt;&lt;/PagerStyle&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:datagrid&gt;&lt;asp:linkbutton id="lbLastPage" style="Z-INDEX: 105; LEFT: 808px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;onclick="ChangePageClick" runat="server" CommandArgument="last"&gt;最后页&lt;/asp:linkbutton&gt;&lt;asp:linkbutton id="lbNextPage" style="Z-INDEX: 104; LEFT: 744px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;onclick="ChangePageClick" runat="server" CommandArgument="next"&gt;下一页&lt;/asp:linkbutton&gt;&lt;asp:linkbutton id="lbPrevPage" style="Z-INDEX: 103; LEFT: 680px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;onclick="ChangePageClick" runat="server" CommandArgument="prev"&gt;上一页&lt;/asp:linkbutton&gt;&lt;asp:linkbutton id="lbFirstPage" style="Z-INDEX: 102; LEFT: 616px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;onclick="ChangePageClick" runat="server" CommandArgument="first"&gt;最前页&lt;/asp:linkbutton&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:Label id="lblDisplayPosition" style="Z-INDEX: 106; LEFT: 240px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Width="64px"&gt;&lt;/asp:Label&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:TextBox id="txtSelectPage" style="Z-INDEX: 107; LEFT: 360px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Width="104px"&gt;&lt;/asp:TextBox&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:Button id="btnSelectPage" style="Z-INDEX: 108; LEFT: 472px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Text="确定"&gt;&lt;/asp:Button&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:Label id="lblError" style="Z-INDEX: 109; LEFT: 520px; POSITION: absolute; TOP: 120px"<BR>&nbsp;&nbsp;&nbsp;&nbsp;runat="server" Width="88px"&gt;Label&lt;/asp:Label&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:Label id="lbTotal" style="Z-INDEX: 110; LEFT: 104px; POSITION: absolute; TOP: 120px" runat="server"<BR>&nbsp;&nbsp;&nbsp;&nbsp;Width="136px"&gt;&lt;/asp:Label&gt;&lt;/form&gt;<BR>&nbsp;&lt;/body&gt;<BR>&lt;/HTML&gt;</P>
<P></P>
<P>&nbsp; 2.*.aspx.cs代码文件</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using System;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Data;<BR>using System.Drawing;<BR>using System.Web;<BR>using System.Web.SessionState;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;<BR>using System.Data.SqlClient;</P>
<P>namespace New_DataGrid自定义分页事件<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// DataGrid自定义分页<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class WebForm1 : System.Web.UI.Page<BR>&nbsp;{<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.DataGrid DataGrid1;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.LinkButton lbLastPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.LinkButton lbNextPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.LinkButton lbPrevPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.LinkButton lbFirstPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Label lblDisplayPosition;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Button btnSelectPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.TextBox txtSelectPage;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Label lblError;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Button Button1;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;protected System.Web.UI.WebControls.Label lbTotal;&nbsp; //显示记录总数<BR>&nbsp;&nbsp;private static int intRecord = 0;&nbsp;&nbsp; //显示总记录数<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;private void Page_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.lblError.Text = "";&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;if(!Page.IsPostBack)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;GetData();&nbsp; // 生成数据<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.BindData();&nbsp; //绑定数据到DataGrid<BR>&nbsp;&nbsp;&nbsp;&nbsp;DisPlayStats();&nbsp;&nbsp; //显示这样的表示: 2/100&nbsp; 意思是:共100页，当前为第二页<BR>&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 生成数据<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void GetData()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;DataTable table = new DataTable();<BR>&nbsp;&nbsp;&nbsp;DataColumnCollection columns = table.Columns;</P>
<P>&nbsp;&nbsp;&nbsp;//定义结构<BR>&nbsp;&nbsp;&nbsp;columns.Add("学号",typeof(System.Int32));<BR>&nbsp;&nbsp;&nbsp;columns.Add("分数",typeof(System.Int32));&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;Random myRandom = new Random();<BR>&nbsp;&nbsp;&nbsp;int intScore = 0;<BR>&nbsp;&nbsp;&nbsp;for(int i = 1;i &lt; 101; i++)<BR>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;intScore = myRandom.Next(100);<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataRow drNew = table.NewRow();<BR>&nbsp;&nbsp;&nbsp;&nbsp;drNew[0] = i.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;drNew[1] = intScore.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;table.Rows.Add(drNew);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;this.ViewState.Add("table",table);</P>
<P>&nbsp;&nbsp;&nbsp;intRecord = table.Rows.Count;<BR>&nbsp;&nbsp;&nbsp;this.lbTotal.Text = "总共:"+intRecord.ToString()+"条记录";<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 绑定DataGrid<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void BindData()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;DataTable table = (DataTable)this.ViewState["table"];<BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.DataSource = table;<BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.DataBind();</P>
<P>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 自定义控件按钮事件<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;/// &lt;param name="sender"&gt;&lt;/param&gt;<BR>&nbsp;&nbsp;/// &lt;param name="e"&gt;&lt;/param&gt;<BR>&nbsp;&nbsp;public void ChangePageClick(object sender, EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;string CommandName = ((LinkButton)sender).CommandArgument.ToString();<BR>&nbsp;&nbsp;&nbsp;switch(CommandName)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;case "next":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (DataGrid1.CurrentPageIndex &lt; DataGrid1.PageCount - 1 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataGrid1.CurrentPageIndex += 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;case "prev":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (DataGrid1.CurrentPageIndex &gt; 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataGrid1.CurrentPageIndex -= 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;case "last":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;default:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataGrid1.CurrentPageIndex = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;DisPlayStats();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void DisPlayStats()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.lblDisplayPosition.Text = (DataGrid1.CurrentPageIndex + 1).ToString() + "/";<BR>&nbsp;&nbsp;&nbsp;this.lblDisplayPosition.Text += DataGrid1.PageCount.ToString();<BR>&nbsp;&nbsp;&nbsp;this.txtSelectPage.Text = (DataGrid1.CurrentPageIndex + 1).ToString();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Web 窗体设计器生成的代码<BR>&nbsp;&nbsp;override protected void OnInit(EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();<BR>&nbsp;&nbsp;&nbsp;base.OnInit(e);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);<BR>&nbsp;&nbsp;&nbsp;this.btnSelectPage.Click += new System.EventHandler(this.btnSelectPage_Click);<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Page_Load);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = e.NewPageIndex;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = 0;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;this.BindData();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void btnSelectPage_Click(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;int intPage = 0;<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;intPage = int.Parse(this.txtSelectPage.Text);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.lblError.Text = "只能输入数字!";&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return;<BR>&nbsp;&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = intPage-1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.BindData();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.DataGrid1.CurrentPageIndex = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.BindData();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.lblError.Text = "超出页码范围!";&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;DisPlayStats();<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}<BR></P>
<P>(四).示例源代码下载(可以直接运行)</P>
<P>&nbsp;&nbsp;&nbsp;<A href="http://www.cnblogs.com/Files/ChengKing/自定义分页example.rar"><FONT color=#002c99>&nbsp;</FONT><FONT color=#000080>http://www.cnblogs.com/Files/ChengKing/<STRONG><FONT color=#002c99>自定义分页example.rar</FONT></STRONG></FONT></A></P><img src ="http://www.cnitblog.com/ChengKing/aggbug/5009.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-29 23:22 <a href="http://www.cnitblog.com/ChengKing/articles/5009.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用IFormatter实现&gt;存储容器(功能:实现游戏保存和读取). </title><link>http://www.cnitblog.com/ChengKing/articles/4008.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:44:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4008.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4008.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4008.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4008.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4008.html</trackback:ping><description><![CDATA[<P>(一)引言</P>
<P>&nbsp;&nbsp;&nbsp; 用using System.Runtime.Serialization命名空间中的:IFormatter实现存储容器.<BR>&nbsp;&nbsp;&nbsp; 用堆栈的实现原理.&nbsp; 可以存储几乎.Net所有的对象. <BR>&nbsp;&nbsp; 本实现实现用它存储Form窗体中的所有对象,包括Label/TextBox/任意类等状态值.</P>
<P>&nbsp; 保存到文件中,并从文件中读取数据.</P>
<P>(二)代码</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; 说明:&nbsp; 下面代码实现的是存储象棋游戏棋盘上的所有对象,比如: 用于保存某一时刻棋盘的位置,以及各种控件的状态值等.&nbsp; 读者可以用此方法来保存一些应用程序中的状态值等.</P>
<P>为了便于理解,把整个文件都显示出来了.主要包括两个方法: &nbsp;保存(序列化)和读取(反序列)</P>
<P>&nbsp;&nbsp;&nbsp; using System;<BR>using System.IO;<BR>using System.Drawing;<BR>using System.Runtime.Serialization;<BR>using System.Runtime.Serialization.Formatters.Binary;</P>
<P>namespace 智能象棋游戏<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// 功能:实现 游戏&nbsp;保存和读取</P>
<P>&nbsp;/// 特点:不但能够恢复断点,而且还能够在相应的断点执行退旗等操作<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Class6<BR>&nbsp;{&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; public string openFileName="..\\..\\save\\SAVA.bin";&nbsp;&nbsp; //存储文件名<BR>&nbsp;&nbsp;&nbsp;&nbsp; public string saveFileName="..\\..\\save\\SAVA.bin";&nbsp;&nbsp; //存储文件名&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;public Class6()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;public void Save(Form2 form2)<BR>&nbsp;&nbsp;{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //******序列化Form2中的数组和变量和类*******//<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;IFormatter formatter=new BinaryFormatter();&nbsp;&nbsp;&nbsp;//定义类,主要用此类中的两个方法来实现功能<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream stream=new FileStream&nbsp;&nbsp;&nbsp;&nbsp; (this.saveFileName,FileMode.Create,FileAccess.Write,FileShare.None);&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //序列化Form2 中pictureBoxStatus数组<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.pictureBoxsStatus);</P>
<P>&nbsp;&nbsp;&nbsp;//存储象棋的当前残局,各个棋子的位置等,以及各方的分数,下棋用时等<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.battle);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; formatter.Serialize(stream,form2.soldiers);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.tempPoint);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.whetherSelected);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.letDown);<BR>&nbsp; &nbsp; &nbsp;&nbsp;formatter.Serialize(stream,form2.signRight);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.start);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.wrongString);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.startTime);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.displaystartTime);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.pause);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.whichFangQianZou);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.pauseCount);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.label3_ShanShuo);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.add_Or_Not);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.shanShuoVarialbe);<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.whoGoString);</P>
<P>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//序列化Form2中类Class2 a中的变量和数组;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; formatter.Serialize(stream,form2.a.GenEatGen);&nbsp;formatter.Serialize (stream,form2.a.have);&nbsp;formatter.Serialize(stream,form2.a.who);&nbsp;formatter.Serialize(stream,form2.a.str);&nbsp;formatter.Serialize(stream,form2.a.allPoint);&nbsp;formatter.Serialize(stream,form2.a.partPoint);&nbsp;formatter.Serialize(stream,form2.a.whichPicture);&nbsp;formatter.Serialize(stream,form2.a.rednum);&nbsp;formatter.Serialize(stream,form2.a.bluenum);&nbsp;formatter.Serialize(stream,form2.a.width);&nbsp;formatter.Serialize(stream,form2.a.height);&nbsp;formatter.Serialize(stream,form2.a.first_X);&nbsp;formatter.Serialize(stream,form2.a.first_Y);&nbsp;formatter.Serialize(stream,form2.a.blueNumTotal);&nbsp;formatter.Serialize(stream,form2.a.redNumTotal);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;//序列化Form2中类Class5 keyBoard中的变量 &nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.keyBoard.KeyBoardOperating);<BR>&nbsp;&nbsp;&nbsp;formatter.Serialize(stream,form2.keyBoard.MouseOperating);&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;stream.Close();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void Read(ref Form2 form2)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp; IFormatter formatter=new BinaryFormatter();<BR>&nbsp;&nbsp;&nbsp;&nbsp; Stream stream=new FileStream(this.openFileName,FileMode.Open,FileAccess.Read,FileShare.Read);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; //Form2 a=new Form2();<BR>&nbsp;&nbsp;&nbsp;&nbsp; Form2 a=form2;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; //反序列化Form2 中pictureBoxStatus数组<BR>&nbsp;&nbsp;&nbsp; &nbsp;a.pictureBoxsStatus=(bool[])formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.battle=(string)formatter.Deserialize(stream);&nbsp;<BR>&nbsp;&nbsp;&nbsp; &nbsp;a.soldiers=(string[])formatter.Deserialize(stream);<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.tempPoint=(Point[])formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp; &nbsp;a.whetherSelected=(bool)formatter.Deserialize(stream);<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.letDown=(bool)formatter.Deserialize(stream);<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.signRight=(int)formatter.Deserialize(stream);<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.start=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.wrongString=(string)formatter.Deserialize(stream);<BR>&nbsp; &nbsp;&nbsp;&nbsp;a.startTime=(DateTime)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp; &nbsp;a.displaystartTime=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.pause=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.whichFangQianZou=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.pauseCount=(int)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.label3_ShanShuo=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.add_Or_Not=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.shanShuoVarialbe=(int)formatter.Deserialize(stream);&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.whoGoString=(string)formatter.Deserialize(stream);&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; //反序列化Form2中类Class5 keyBoard中的变量 <BR>&nbsp;&nbsp;&nbsp;&nbsp; a.keyBoard.KeyBoardOperating=(bool)formatter.Deserialize(stream);<BR>&nbsp;&nbsp;&nbsp;&nbsp; a.keyBoard.MouseOperating=(bool)formatter.Deserialize(stream);&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<BR>&nbsp; &nbsp;&nbsp;&nbsp;stream.Close();<BR>&nbsp; &nbsp; }&nbsp;&nbsp;<BR>&nbsp; }<BR>}<BR></P>
<P></P>
<P>需要注意的一点时，在往容器里存储和从容器里读取时，要按照椎栈的原理进行存储和读取.</P>
<P>遵守“先进后出，后进先出原则”。 </P>
<P>即先放到容器里的对象，在读取时到最后才能读取出来.最后存储的对象，在读取时第一个被读取出来.<BR></P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4008.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:44 <a href="http://www.cnitblog.com/ChengKing/articles/4008.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 用Process调用其它应用程序</title><link>http://www.cnitblog.com/ChengKing/articles/4002.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:38:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4002.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4002.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4002.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4002.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4002.html</trackback:ping><description><![CDATA[<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;<BR>using System.Diagnostics;</P>
<P>namespace WindowsApplication3<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;private System.ComponentModel.IContainer components;</P>
<P>&nbsp;&nbsp;private System.Diagnostics.Process process;<BR>&nbsp;&nbsp;private System.Windows.Forms.Button button1; <BR>&nbsp;&nbsp;//Form2 a;<BR>&nbsp;&nbsp;//Graphics g=pictureBox1.CreateGraphics();<BR>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.button1 = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// button1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.button1.Location = new System.Drawing.Point(136, 144);<BR>&nbsp;&nbsp;&nbsp;this.button1.Name = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.button1.Text = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.Click += new System.EventHandler(this.button1_Click_1);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(344, 302);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.button1);<BR>&nbsp;&nbsp;&nbsp;this.HelpButton = true;<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);<BR>&nbsp;&nbsp;&nbsp;this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());<BR>&nbsp;&nbsp;}&nbsp;</P>
<P>&nbsp;&nbsp;private void Form1_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void button1_Click_1(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;process=new Process();&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;process.StartInfo.FileName="skate.exe";<BR>&nbsp;&nbsp;&nbsp;<A>//process.StartInfo.FileName=@"F:\c#\</A>拼图游戏2\bin\Release\拼图游戏2.exe";<BR>&nbsp;&nbsp;&nbsp;this.process.Start();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)<BR>&nbsp;&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}<BR></P>
<P>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4002.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:38 <a href="http://www.cnitblog.com/ChengKing/articles/4002.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control study -&gt; Drag拖放事件示例 </title><link>http://www.cnitblog.com/ChengKing/articles/4003.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:38:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4003.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4003.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4003.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4003.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4003.html</trackback:ping><description><![CDATA[<P>(一).功能描述</P>
<P>&nbsp;&nbsp; 涉及到DragEnter DragDrop DragLeave DragOver QueryContinueDrag事件用法的一个例子</P>
<P>(二).代码</P>
<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;</P>
<P>namespace 拖放数据操作_按键事件_<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;private System.Windows.Forms.Button button1;<BR>&nbsp;&nbsp;private System.Windows.Forms.TextBox textBox1;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.button1 = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.textBox1 = new System.Windows.Forms.TextBox();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// button1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.button1.Location = new System.Drawing.Point(112, 72);<BR>&nbsp;&nbsp;&nbsp;this.button1.Name = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.button1.Text = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.button1_QueryContinueDrag);<BR>&nbsp;&nbsp;&nbsp;this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// textBox1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.textBox1.AccessibleDescription = "aaaaa";<BR>&nbsp;&nbsp;&nbsp;this.textBox1.AccessibleName = "bbbbbb";<BR>&nbsp;&nbsp;&nbsp;this.textBox1.AllowDrop = true;<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Location = new System.Drawing.Point(64, 160);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Multiline = true;<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Name = "textBox1";<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Size = new System.Drawing.Size(168, 48);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Text = "";<BR>&nbsp;&nbsp;&nbsp;this.textBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.textBox1_DragOver);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.DragLeave += new System.EventHandler(this.textBox1_DragLeave);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(292, 266);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.textBox1);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.button1);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void button1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(e.EscapePressed)<BR>&nbsp;&nbsp;&nbsp;&nbsp;e.Action=DragAction.Cancel;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;button1.DoDragDrop(button1.Text,DragDropEffects.Copy|DragDropEffects.Move);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(e.Data.GetDataPresent(DataFormats.Text))<BR>&nbsp;&nbsp;&nbsp;&nbsp;e.Effect=DragDropEffects.Copy;<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;e.Effect=DragDropEffects.None;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//使用KeyState属性<BR>&nbsp;&nbsp;&nbsp;if((e.KeyState&amp;(1&lt;&lt;3))!=0)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((e.AllowedEffect&amp;DragDropEffects.Copy)!=0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.Effect=DragDropEffects.Copy;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textBox1.Text=e.Data.GetData(DataFormats.Text).ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;e.Effect=DragDropEffects.Move;<BR>&nbsp;&nbsp;&nbsp;&nbsp;textBox1.Text=e.Data.GetData(DataFormats.Text).ToString();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void textBox1_DragLeave(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;textBox1.Text="执行 DragLeave";<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void textBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;textBox1.Text="执行 DragOver";<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}<BR>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4003.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:38 <a href="http://www.cnitblog.com/ChengKing/articles/4003.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 实现:用鼠标拖动图片</title><link>http://www.cnitblog.com/ChengKing/articles/4001.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:37:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4001.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4001.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4001.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4001.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4001.html</trackback:ping><description><![CDATA[<P>(一).说明</P>
<P>&nbsp;&nbsp;&nbsp; 功能:用鼠标实现拖动图片.<BR>&nbsp;&nbsp;&nbsp; 用法:将鼠标指向一图片,按住鼠标左键拖动. 图片会随鼠标一起移动.</P>
<P>(二).代码</P>
<P>&nbsp;&nbsp;&nbsp; (说明: 此功能我用的用途是将象棋游戏之棋子拖动效果)</P>
<P>&nbsp;&nbsp;&nbsp; 读者可以将此功能用作其它用途,也可以用作设计其它的游戏.</P>
<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;<BR>using System.Threading;</P>
<P>namespace 拖动图片<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;public System.Windows.Forms.PictureBox[] pictureBox=new PictureBox[2];<BR>&nbsp;&nbsp;//private System.Windows.Forms.PictureBox pictureBox2;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();<BR>&nbsp;&nbsp;&nbsp;for(int i=0;i&lt;2;i++)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.pictureBox[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.pictureBox[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.pictureBox[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);<BR>&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows Form Designer generated code<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0] = new System.Windows.Forms.PictureBox();<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1] = new System.Windows.Forms.PictureBox();<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// pictureBox1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].BackColor = System.Drawing.Color.Transparent;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].Location = new System.Drawing.Point(200, 192);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].Name = "pictureBox1";<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].Size = new System.Drawing.Size(88, 72);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[0].TabStop = false;<BR>&nbsp;&nbsp;&nbsp;/*this.pictureBox[1].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);*/<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// pictureBox2<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;//this.Opacity=0.8;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].BackColor = System.Drawing.Color.DodgerBlue;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].Location = new System.Drawing.Point(200, 72);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].Name = "pictureBox2";<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].Size = new System.Drawing.Size(88, 64);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[1].TabStop = false;<BR>&nbsp;&nbsp;&nbsp;/*this.pictureBox[2].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[2].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox[2].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);<BR>&nbsp;&nbsp;&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(432, 348);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.pictureBox[0]);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.pictureBox[1]);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool whetherSelected=false;<BR>&nbsp;&nbsp;Point p=new Point();</P>
<P>&nbsp;&nbsp;#region 事件代码<BR>&nbsp;&nbsp;private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; whetherSelected=true;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;p.X=Cursor.Position.X;<BR>&nbsp;&nbsp;&nbsp;p.Y=Cursor.Position.Y;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;whetherSelected=false;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(whetherSelected==true) <BR>&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//加入一个函数,得到是哪个pictureBox<BR>&nbsp;&nbsp;&nbsp;&nbsp;int index=-1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0;i&lt;2;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(this.pictureBox[i].Capture)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index=i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.pictureBox[index].Left=this.pictureBox[index].Left+(Cursor.Position.X-p.X);<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.pictureBox[index].Top=this.pictureBox[index].Top+(Cursor.Position.Y-p.Y);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pictureBox[1-index].SendToBack();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;p.X=Cursor.Position.X;<BR>&nbsp;&nbsp;&nbsp;&nbsp;p.Y=Cursor.Position.Y;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;<BR>&nbsp;}<BR>}</P>
<P></P>
<P><BR>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:37 <a href="http://www.cnitblog.com/ChengKing/articles/4001.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control study -&gt; 图像格式转换 如:将*.jpg -&gt; *.gif进行转换</title><link>http://www.cnitblog.com/ChengKing/articles/4000.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:36:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4000.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4000.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4000.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4000.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4000.html</trackback:ping><description><![CDATA[<P>(一).功能</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一个演示图像格式转换的示例程序.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 支持格式:&nbsp; bmp jpeg gif tiff png wmf 等之间的相互转换</P>
<P>(二).代码(整个后台代码)</P>
<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;<BR>using System.IO;<BR>using System.Drawing.Imaging;<BR>namespace 图像格式转换<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class FormMain : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;private System.Windows.Forms.Panel panel1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Splitter splitter1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Panel panel2;<BR>&nbsp;&nbsp;private System.Windows.Forms.Button buttonOpen;<BR>&nbsp;&nbsp;private System.Windows.Forms.Button buttonConvert;<BR>&nbsp;&nbsp;private System.Windows.Forms.ComboBox comboBox;<BR>&nbsp;&nbsp;private System.Windows.Forms.Label label1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Label label2;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;<BR>&nbsp;&nbsp;private System.Windows.Forms.PictureBox pictureBox;<BR>&nbsp;&nbsp;private System.Drawing.Bitmap bitmap;</P>
<P>&nbsp;&nbsp;public FormMain()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;bitmap=null;<BR>&nbsp;&nbsp;&nbsp;pictureBox.Width=panel2.Width;<BR>&nbsp;&nbsp;&nbsp;pictureBox.Height=panel2.Height;<BR>&nbsp;&nbsp;&nbsp;pictureBox.Top=panel1.Top;<BR>&nbsp;&nbsp;&nbsp;pictureBox.Left=panel1.Left;<BR>&nbsp;&nbsp;&nbsp;panel2.Resize+=new System.EventHandler(panel2_Resize);<BR>&nbsp;&nbsp;&nbsp;//buttonOpen.Click+=new System.EventHandler(buttonOpen_Click);<BR>&nbsp;&nbsp;&nbsp;comboBox.Items.AddRange(new object[]{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.jpg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.bmp",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.gif",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.png",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.tif",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "*.wmf"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows Form Designer generated code<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.panel1 = new System.Windows.Forms.Panel();<BR>&nbsp;&nbsp;&nbsp;this.buttonConvert = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.comboBox = new System.Windows.Forms.ComboBox();<BR>&nbsp;&nbsp;&nbsp;this.label1 = new System.Windows.Forms.Label();<BR>&nbsp;&nbsp;&nbsp;this.label2 = new System.Windows.Forms.Label();<BR>&nbsp;&nbsp;&nbsp;this.splitter1 = new System.Windows.Forms.Splitter();<BR>&nbsp;&nbsp;&nbsp;this.panel2 = new System.Windows.Forms.Panel();<BR>&nbsp;&nbsp;&nbsp;this.pictureBox = new System.Windows.Forms.PictureBox();<BR>&nbsp;&nbsp;&nbsp;this.panel1.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;this.panel2.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// panel1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;<BR>&nbsp;&nbsp;&nbsp;this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.buttonConvert,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.buttonOpen,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.comboBox,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label2});<BR>&nbsp;&nbsp;&nbsp;this.panel1.Dock = System.Windows.Forms.DockStyle.Left;<BR>&nbsp;&nbsp;&nbsp;this.panel1.Name = "panel1";<BR>&nbsp;&nbsp;&nbsp;this.panel1.Size = new System.Drawing.Size(128, 365);<BR>&nbsp;&nbsp;&nbsp;this.panel1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// buttonConvert<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.buttonConvert.Location = new System.Drawing.Point(40, 264);<BR>&nbsp;&nbsp;&nbsp;this.buttonConvert.Name = "buttonConvert";<BR>&nbsp;&nbsp;&nbsp;this.buttonConvert.Size = new System.Drawing.Size(56, 24);<BR>&nbsp;&nbsp;&nbsp;this.buttonConvert.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.buttonConvert.Text = "转换";<BR>&nbsp;&nbsp;&nbsp;buttonConvert.Click+=new System.EventHandler(buttonConvert_Click);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// buttonOpen<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.Location = new System.Drawing.Point(32, 56);<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.Name = "buttonOpen";<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.Size = new System.Drawing.Size(64, 24);<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.Text = "打开文件";<BR>&nbsp;&nbsp;&nbsp;this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// comboBox<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.comboBox.Location = new System.Drawing.Point(64, 224);<BR>&nbsp;&nbsp;&nbsp;this.comboBox.Name = "comboBox";<BR>&nbsp;&nbsp;&nbsp;this.comboBox.Size = new System.Drawing.Size(48, 20);<BR>&nbsp;&nbsp;&nbsp;this.comboBox.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.comboBox.Text = "comboBox1";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// label1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.label1.Location = new System.Drawing.Point(24, 104);<BR>&nbsp;&nbsp;&nbsp;this.label1.Name = "label1";<BR>&nbsp;&nbsp;&nbsp;this.label1.Size = new System.Drawing.Size(80, 40);<BR>&nbsp;&nbsp;&nbsp;this.label1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.label1.Text = "label1";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// label2<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.label2.Location = new System.Drawing.Point(8, 224);<BR>&nbsp;&nbsp;&nbsp;this.label2.Name = "label2";<BR>&nbsp;&nbsp;&nbsp;this.label2.Size = new System.Drawing.Size(48, 16);<BR>&nbsp;&nbsp;&nbsp;this.label2.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.label2.Text = "转换为";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// splitter1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.splitter1.Location = new System.Drawing.Point(128, 0);<BR>&nbsp;&nbsp;&nbsp;this.splitter1.Name = "splitter1";<BR>&nbsp;&nbsp;&nbsp;this.splitter1.Size = new System.Drawing.Size(3, 365);<BR>&nbsp;&nbsp;&nbsp;this.splitter1.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.splitter1.TabStop = false;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// panel2<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;<BR>&nbsp;&nbsp;&nbsp;this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pictureBox});<BR>&nbsp;&nbsp;&nbsp;this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;<BR>&nbsp;&nbsp;&nbsp;this.panel2.Location = new System.Drawing.Point(131, 0);<BR>&nbsp;&nbsp;&nbsp;this.panel2.Name = "panel2";<BR>&nbsp;&nbsp;&nbsp;this.panel2.Size = new System.Drawing.Size(373, 365);<BR>&nbsp;&nbsp;&nbsp;this.panel2.TabIndex = 2;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// pictureBox<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox.Name = "pictureBox";<BR>&nbsp;&nbsp;&nbsp;this.pictureBox.Size = new System.Drawing.Size(369, 361);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox.TabStop = false;<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// FormMain<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(504, 365);<BR>&nbsp;&nbsp;&nbsp;this.Controls.AddRange(new System.Windows.Forms.Control[] {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.panel2,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.splitter1,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.panel1});<BR>&nbsp;&nbsp;&nbsp;this.Name = "FormMain";<BR>&nbsp;&nbsp;&nbsp;this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;<BR>&nbsp;&nbsp;&nbsp;this.Text = "FormMain";<BR>&nbsp;&nbsp;&nbsp;this.panel1.ResumeLayout(false);<BR>&nbsp;&nbsp;&nbsp;this.panel2.ResumeLayout(false);<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new FormMain());<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void buttonOpen_Click(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;OpenFileDialog openFileDialog=new OpenFileDialog();<BR>&nbsp;&nbsp;&nbsp;openFileDialog.Filter="*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";<BR>&nbsp;&nbsp;&nbsp;openFileDialog.Title="打开图像文件";<BR>&nbsp;&nbsp;&nbsp;openFileDialog.Multiselect=false;<BR>&nbsp;&nbsp;&nbsp;if(openFileDialog.ShowDialog()==DialogResult.OK)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(bitmap!=null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;string fileName=openFileDialog.FileName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;bitmap=new Bitmap(fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(bitmap.Width&gt;bitmap.Width)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Width=panel2.Width;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Height=(int)((double)bitmap.Height*panel2.Width/bitmap.Width);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Height=panel2.Height;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pictureBox.Width=(int)((double)bitmap.Width*panel2.Height/bitmap.Height);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Image=bitmap;<BR>&nbsp;&nbsp;&nbsp;&nbsp;FileInfo f=new FileInfo(fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.Text="图象转换:"+f.Name;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.label1.Text=f.Name;<BR>&nbsp;&nbsp;&nbsp;&nbsp;buttonConvert.Enabled=true;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;private void panel2_Resize(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;pictureBox.Top=panel1.Top;<BR>&nbsp;&nbsp;&nbsp;pictureBox.Left=panel1.Left;<BR>&nbsp;&nbsp;&nbsp;if(bitmap!=null)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(bitmap.Width&gt;bitmap.Height)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Width=panel2.Width;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Height=(int)((double) bitmap.Height*panel2.Width/bitmap.Width);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Height=panel2.Height;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Width=(int)((double)bitmap.Width*panel2.Height/bitmap.Height);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Width=panel2.Width;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pictureBox.Height=panel2.Height;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;pictureBox.Refresh();<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;private void buttonConvert_Click(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(comboBox.SelectedItem==null)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SaveFileDialog saveFileDialog=new SaveFileDialog();<BR>&nbsp;&nbsp;&nbsp;&nbsp;saveFileDialog.Title="转化为:";<BR>&nbsp;&nbsp;&nbsp;&nbsp;saveFileDialog.OverwritePrompt=true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; saveFileDialog.CheckPathExists=true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;saveFileDialog.Filter=comboBox.Text+"|"+comboBox.Text;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(saveFileDialog.ShowDialog()==DialogResult.OK)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string fileName=saveFileDialog.FileName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(comboBox.Text)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case "*.bmp":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Bmp);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "*.jpg":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Jpeg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "*.gif":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Gif);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "*.tif":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Tiff);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "*.png":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Png);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "*.wmf":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Save(fileName,ImageFormat.Wmf);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInfo f=new FileInfo(fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Text="图像转换:"+f.Name;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label1.Text=f.Name;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}</P>
<P></P>
<P><BR>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:36 <a href="http://www.cnitblog.com/ChengKing/articles/4000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 模拟鼠标 </title><link>http://www.cnitblog.com/ChengKing/articles/3999.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:31:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3999.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3999.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3999.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3999.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3999.html</trackback:ping><description><![CDATA[<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;</P>
<P>namespace 模拟鼠标<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1 = new System.Windows.Forms.PictureBox();<BR>&nbsp;&nbsp;&nbsp;this.label1 = new System.Windows.Forms.Label();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// pictureBox1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Location = new System.Drawing.Point(88, 120);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Name = "pictureBox1";<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.TabStop = false;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.pictureBox1_KeyDown);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// label1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.label1.BackColor = System.Drawing.Color.DarkOliveGreen;<BR>&nbsp;&nbsp;&nbsp;this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;<BR>&nbsp;&nbsp;&nbsp;this.label1.Location = new System.Drawing.Point(48, 208);<BR>&nbsp;&nbsp;&nbsp;this.label1.Name = "label1";<BR>&nbsp;&nbsp;&nbsp;this.label1.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.label1.Text = "label1";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(292, 266);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.label1);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.pictureBox1);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Form1_Load);<BR>&nbsp;&nbsp;&nbsp;this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);<BR>&nbsp;&nbsp;&nbsp;this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;[System.Runtime.InteropServices.DllImport("user32")]<BR>&nbsp;&nbsp;&nbsp;&nbsp; private static extern int mouse_event(int dwFlags,int dx,int dy,int cButtons,int dwExtraInfo);<BR>&nbsp;&nbsp;const int MOUSEEVENTF_MOVE=0x0001;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_LEFTDOWN=0X0002;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_LEFTUP=0X0004;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_RIGHTDOWN=0X0008;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_RIGHTUP=0X0010;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_MIDDLEDOWN=0X0020;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_MIDDLEUP=0X0040;<BR>&nbsp;&nbsp;private System.Windows.Forms.PictureBox pictureBox1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Label label1;<BR>&nbsp;&nbsp;const int MOUSEEVENTF_ABSOLUTE=0X8000;</P>
<P>&nbsp;&nbsp;private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//mouse_event(MOUSEEVENTF_MOVE,-10,-10,0,0);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Down)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,20,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Up)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,-20,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Left)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,-20,0,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Right)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,20,0,0,0);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;public void pictureBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;/*if(e.KeyCode==Keys.Down)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,20,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Up)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,-20,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Left)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,-20,0,0,0);<BR>&nbsp;&nbsp;&nbsp;if(e.KeyCode==Keys.Right)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,20,0,0,0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;*/<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp;private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(e.Button==MouseButtons.Right)<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,20,0,0);<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;mouse_event(MOUSEEVENTF_MOVE,0,-20,0,0);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P><BR>&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;}<BR>}<BR></P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3999.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:31 <a href="http://www.cnitblog.com/ChengKing/articles/3999.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 放大图像区域</title><link>http://www.cnitblog.com/ChengKing/articles/3997.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:30:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3997.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3997.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3997.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3997.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3997.html</trackback:ping><description><![CDATA[<P>(一).说明</P>
<P>&nbsp; 将鼠标指向一幅图片的一块区域,此区域会放大显示,变清晰.<BR>用类: Graphics 实现.</P>
<P>(二).代码&nbsp;&nbsp;</P>
<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;<BR>using System.Drawing.Drawing2D;</P>
<P>namespace 放大图像区域<BR>{<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;private System.Windows.Forms.PictureBox pictureBox1;<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;<BR>&nbsp;&nbsp;private System.Windows.Forms.Button button1;<BR>&nbsp;&nbsp;//private Cursor myCursor;<BR>&nbsp;&nbsp;Cursor myCursor=new Cursor("..\\..\\MAGNIFY.cur"); //自定义鼠标<BR>&nbsp;&nbsp;Graphics g;<BR>&nbsp;&nbsp;Image myImage;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1 = new System.Windows.Forms.PictureBox();<BR>&nbsp;&nbsp;&nbsp;this.button1 = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// pictureBox1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlText;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Location = new System.Drawing.Point(24, 16);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Name = "pictureBox1";<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.Size = new System.Drawing.Size(440, 384);<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.TabStop = false;<BR>&nbsp;&nbsp;&nbsp;this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// button1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.button1.Location = new System.Drawing.Point(384, 96);<BR>&nbsp;&nbsp;&nbsp;this.button1.Name = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.button1.Text = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.Click += new System.EventHandler(this.button1_Click);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(472, 406);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.button1);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.pictureBox1);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Form1_Load);<BR>&nbsp;&nbsp;&nbsp;this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Cursor.Current=myCursor;<BR>&nbsp;&nbsp;&nbsp;Rectangle sourceRectangle=new Rectangle(e.X-10,e.Y-10,20,20);&nbsp;&nbsp;//要放大的区域<BR>&nbsp;&nbsp;&nbsp;//Rectangle destRectangle=new Rectangle(e.X-20,e.Y-20,40,40);<BR>&nbsp;&nbsp;&nbsp;Rectangle destRectangle=new Rectangle(pictureBox1.Width-150,pictureBox1.Height-150,pictureBox1.Width,pictureBox1.Height);&nbsp;&nbsp; //放大的比例<BR>&nbsp;&nbsp;&nbsp;g.DrawLine(myImage,destRectangle,sourceRectangle,GraphicsUnit.Pixel);<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Cursor.Current=Cursors.Default;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;g=this.pictureBox1.CreateGraphics();<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;myImage=this.pictureBox1.Image;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void button1_Click(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;//Graphics g=this.pictureBox1.CreateGraphics();<BR>&nbsp;&nbsp;&nbsp;Graphics g=pictureBox1.CreateGraphics();<BR>&nbsp;&nbsp;&nbsp;g.DrawLine(new Pen(Color.Red,5),20,20,50,50);<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}</P>
<P></P>
<P><BR>&nbsp;</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3997.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:30 <a href="http://www.cnitblog.com/ChengKing/articles/3997.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Control Study -&gt; 用获取路径方法得到圆形窗体(如将: PictureBox变成圆形)</title><link>http://www.cnitblog.com/ChengKing/articles/3998.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:30:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/3998.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/3998.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/3998.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/3998.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/3998.html</trackback:ping><description><![CDATA[<P>(一).功能</P>
<P>     有时候由于显示效果,需要将某个控件变一下形状.<BR>     本文举例将PictureBox[]数组变成圆形.</P>
<P>(二).代码</P>
<P>    (这里说明一下,我个人将它变形是因为我用PictureBox表示象棋棋子,</P>
<P>     由于PictureBox默认是方 的,  所以得把它变成圆的)</P>
<P>     大家可以将这个功能作为其它用途使用.</P>
<P>using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;</P>
<P><BR>namespace 智能象棋游戏<BR>{<BR> /// <summary><BR> /// 功能:将所有picturebox控件变为圆形<BR> /// 特点:使旗子变圆形</P>
<P> /// </summary></P>
<P> public class Class7<BR> {<BR>  [System.Runtime.InteropServices.DllImport("gdi32")]<BR>  private static extern IntPtr BeginPath(IntPtr hdc);<BR>  [System.Runtime.InteropServices.DllImport("gdi32")]<BR>  private static extern int SetBkMode(IntPtr hdc,int nBkMode);  <BR>  const int TRANSPARENT=1;<BR>  [System.Runtime.InteropServices.DllImport("gdi32")]     <BR>  private static extern IntPtr EndPath(IntPtr hdc);<BR>  [System.Runtime.InteropServices.DllImport("gdi32")]<BR>  private static extern IntPtr PathToRegion(IntPtr hdc);<BR>  [System.Runtime.InteropServices.DllImport("gdi32")]<BR>  private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2);<BR>  [System.Runtime.InteropServices.DllImport("user32")]<BR>  private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);<BR>  [System.Runtime.InteropServices.DllImport("user32")]<BR>  private static extern IntPtr GetDC(IntPtr hwnd);<BR>  public Class7()<BR>  {   <BR>  }<BR>  public void MakeToPictureBoxsToCircle(PictureBox[] pb)<BR>  {<BR>   IntPtr dc;<BR>   IntPtr region;<BR>   for(int i=0;i<pb.Length;i++)<BR>   {<BR>    dc=GetDC(pb[i].Handle);<BR>    BeginPath(dc);<BR>    SetBkMode(dc,TRANSPARENT);<BR>    Ellipse(dc,0,0,pb[i].Width-3,pb[i].Height-2);<BR>    EndPath(dc);<BR>    region=PathToRegion(dc);<BR>    SetWindowRgn(pb[i].Handle,region,false);<BR>   }   <BR>  }<BR> }<BR>}</P>
<P></P>
<P><BR></P><img src ="http://www.cnitblog.com/ChengKing/aggbug/3998.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:30 <a href="http://www.cnitblog.com/ChengKing/articles/3998.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>