Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 6k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace ch3_7
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.MainMenu mainMenu1;
  15. private System.Windows.Forms.MenuItem menuItem1;
  16. private System.Windows.Forms.MenuItem menuItem2;
  17. private System.Windows.Forms.MenuItem menuItem3;
  18. private System.Windows.Forms.Panel panel1;
  19. private System.Windows.Forms.PictureBox pictureBox1;
  20. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  21. /// <summary>
  22. /// 必需的设计器变量。
  23. /// </summary>
  24. private System.ComponentModel.Container components = null;
  25. public Form1()
  26. {
  27. //
  28. // Windows 窗体设计器支持所必需的
  29. //
  30. InitializeComponent();
  31. //
  32. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  33. //
  34. }
  35. /// <summary>
  36. /// 清理所有正在使用的资源。
  37. /// </summary>
  38. protected override void Dispose( bool disposing )
  39. {
  40. if( disposing )
  41. {
  42. if (components != null) 
  43. {
  44. components.Dispose();
  45. }
  46. }
  47. base.Dispose( disposing );
  48. }
  49. #region Windows Form Designer generated code
  50. /// <summary>
  51. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  52. /// 此方法的内容。
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  57. this.menuItem1 = new System.Windows.Forms.MenuItem();
  58. this.menuItem2 = new System.Windows.Forms.MenuItem();
  59. this.menuItem3 = new System.Windows.Forms.MenuItem();
  60. this.panel1 = new System.Windows.Forms.Panel();
  61. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  62. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  63. this.panel1.SuspendLayout();
  64. this.SuspendLayout();
  65. // 
  66. // mainMenu1
  67. // 
  68. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  69.   this.menuItem1});
  70. // 
  71. // menuItem1
  72. // 
  73. this.menuItem1.Index = 0;
  74. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  75.   this.menuItem2,
  76.   this.menuItem3});
  77. this.menuItem1.Text = "图像";
  78. // 
  79. // menuItem2
  80. // 
  81. this.menuItem2.Index = 0;
  82. this.menuItem2.Text = "捕捉图像";
  83. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  84. // 
  85. // menuItem3
  86. // 
  87. this.menuItem3.Index = 1;
  88. this.menuItem3.Text = "保存图像";
  89. this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
  90. // 
  91. // panel1
  92. // 
  93. this.panel1.AutoScroll = true;
  94. this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  95. this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
  96.  this.pictureBox1});
  97. this.panel1.Location = new System.Drawing.Point(16, 8);
  98. this.panel1.Name = "panel1";
  99. this.panel1.Size = new System.Drawing.Size(488, 384);
  100. this.panel1.TabIndex = 0;
  101. // 
  102. // pictureBox1
  103. // 
  104. this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  105. this.pictureBox1.Name = "pictureBox1";
  106. this.pictureBox1.Size = new System.Drawing.Size(484, 380);
  107. this.pictureBox1.TabIndex = 0;
  108. this.pictureBox1.TabStop = false;
  109. // 
  110. // saveFileDialog1
  111. // 
  112. this.saveFileDialog1.FileName = "doc1";
  113. // 
  114. // Form1
  115. // 
  116. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  117. this.AutoScroll = true;
  118. this.ClientSize = new System.Drawing.Size(520, 437);
  119. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  120.   this.panel1});
  121. this.Menu = this.mainMenu1;
  122. this.Name = "Form1";
  123. this.Text = "抓图程序";
  124. this.panel1.ResumeLayout(false);
  125. this.ResumeLayout(false);
  126. }
  127. #endregion
  128. /// <summary>
  129. /// 应用程序的主入口点。
  130. /// </summary>
  131. [STAThread]
  132. static void Main() 
  133. {
  134. Application.Run(new Form1());
  135. }
  136. private void menuItem2_Click(object sender, System.EventArgs e)
  137. {
  138. try
  139. {
  140. IDataObject d = Clipboard.GetDataObject ( ) ;
  141. //判断剪切板中数据是不是位图
  142. if ( d.GetDataPresent ( DataFormats.Bitmap ) ) 
  143. {
  144. //获得位图对象
  145. Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ;
  146. //设置PictureBox的大小
  147. this.pictureBox1.Width=b.Width;
  148. this.pictureBox1.Height=b.Height;
  149. //设置Panel1的大小
  150. this.panel1.Width=b.Width;
  151. this.panel1.Height=b.Height;
  152. //显示图片 
  153. this.pictureBox1.Image=b;
  154. }
  155. else
  156. {
  157. //如果剪贴板上没有图像文件,则发出提醒
  158. MessageBox.Show("没有可显示的位图文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  159. }
  160. }
  161. catch(Exception error)
  162. {
  163.            //读取剪贴板出错处理
  164.        MessageBox.Show("错误信息是: "+error.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  165. }
  166. }
  167. private void menuItem3_Click(object sender, System.EventArgs e)
  168. {
  169. try
  170. {
  171. IDataObject d = Clipboard.GetDataObject ( ) ;
  172. //判断剪切板中数据是不是位图
  173. if ( d.GetDataPresent ( DataFormats.Bitmap ) ) 
  174. {
  175. Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ;
  176. //设置过滤器
  177. this.saveFileDialog1.Filter="Bitmap file(*.bmp)|*.bmp|All files(*.*)|*.*";
  178. if (this.saveFileDialog1.ShowDialog()==DialogResult.OK)
  179. {
  180. //获取保存文件名
  181. string fileName=this.saveFileDialog1.FileName;
  182. //保存文件
  183. b.Save(fileName);
  184. }
  185. }
  186. else
  187. {
  188. //如果剪贴板上没有图像文件,则发出提醒
  189. MessageBox.Show("没有可保存的位图文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  190. }
  191. }
  192. catch(Exception error)
  193. {
  194. //读取剪贴板出错处理
  195. MessageBox.Show("错误信息是: "+error.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  196. }
  197. }
  198. }
  199. }