• C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能


    本实例全部文章目录

    添加设置窗口

    在解决方案资源管理器窗口中,右键单击项目名称,在弹出的菜单中选择:添加》Windows窗体:


    输入窗体名称“frmSetup”:


    设置窗体的Text属性为“设置”,设置窗体的Size为“472, 276”,StartPosition属性为“CenterScreen”。

    添加设置标签页:

    左侧工具箱》窗器:双击“TabControl”


    设置的Dock属性为“Top”,Size属性为“456, 200”;

    添加标签页:


    添加三个标签页,Text分别设置为“基本设置,自动上传,自动保存”


    添加确定和取消按钮;

    基本设置标签页:


    从工具箱中添加两个GroupBox,分别为“热键、截图选项”;

    添加两个RadioButton,用于热键选择;

    添加四个CheckBox用于截图选项;

    添加两个TextBox用于设置放大镜的尺寸;

    添加两个PictureBox用于显示X和锁的图片;

    添加图片资源:

    双击Properties中的“Resources.resx”


    切换到图像视图:


    将图片复制,粘贴到这里,分别命名为“Lock,X”;

    设置PictureBox的Image属性为对应的资源:


    自动上传标签页:


    自动保存标签页:


    其中文件名称需要使用两个ComboBox,Items集合分别设置为:



    编写代码:

    双击设置窗体,切换到代码视图,添加私有变量:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 保存Form1的句柄  
    3. /// </summary>  
    4. private IntPtr frm1Handle = IntPtr.Zero;  

    修改构造函数:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 构造函数  
    3. /// </summary>  
    4. /// <param name="frm1_Handle"></param>  
    5. public frmSetup(IntPtr frm1_Handle)  
    6. {  
    7.     InitializeComponent();  
    8.     this.frm1Handle = frm1_Handle;  
    9. }  

    为托盘菜单中的设置添加事件处理

    打开主窗体Form1的设计视图,选中“contextMenuStrip1”


    修改设置菜单的Name为“tsmi_Set”,双击设置菜单,添加代码:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 托盘菜单设置事件处理程序  
    3. /// </summary>  
    4. /// <param name="sender"></param>  
    5. /// <param name="e"></param>  
    6. private void tsmi_Set_Click(object sender, EventArgs e)  
    7. {  
    8.     frmSetup frm = new frmSetup(this.Handle);  
    9.     frm.ShowDialog();  
    10. }  
    编译,调试一下,通过托盘图标的右键菜单》设置 可以打开刚刚添加的设置窗口了。

    添加项目引用:

    主窗体添加相关配置项变量:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. #region 基本设置参数  
    2. /// <summary>  
    3. /// 截图时是否显示截图信息栏  
    4. /// </summary>  
    5. public bool InfoBoxVisible = true;  
    6. /// <summary>  
    7. /// 截图时是否显示编辑工具栏  
    8. /// </summary>  
    9. public bool ToolBoxVisible = true;  
    10. /// <summary>  
    11. /// 截图中是否包含鼠标指针形状  
    12. /// </summary>  
    13. public bool IsCutCursor = true;  
    14. /// <summary>  
    15. /// 截图时是否显示放大镜  
    16. /// </summary>  
    17. public bool ZoomBoxVisible = true;  
    18. /// <summary>  
    19. /// 放大镜的尺寸——宽度  
    20. /// </summary>  
    21. public int ZoomBoxWidth = 120;  
    22. /// <summary>  
    23. /// 放大镜的尺寸——高度  
    24. /// </summary>  
    25. public int ZoomBoxHeight = 100;  
    26. #endregion  
    27.  
    28. #region 图片上传参数  
    29. public string PicDescFieldName = "pictitle";  
    30. public string ImageFieldName = "upfile";  
    31. public string PicDesc = "cutImage";  
    32. public string UploadUrl = "http://";  
    33. public bool DoUpload = false;  
    34. #endregion  
    35.  
    36. #region 自动保存参数  
    37. /// <summary>  
    38. /// 是否自动保存到硬盘  
    39. /// </summary>  
    40. public bool AutoSaveToDisk = false;  
    41. /// <summary>  
    42. /// 自动保存目录  
    43. /// </summary>  
    44. public string AutoSaveDirectory = string.Empty;  
    45. /// <summary>  
    46. /// 是否启用日期格式“2013_02_22”的子目录  
    47. /// </summary>  
    48. public bool AutoSaveSubDir = false;  
    49. /// <summary>  
    50. /// 自动保存文件名前缀  
    51. /// </summary>  
    52. public string AutoSaveFileName1 = "屏幕截图";  
    53. /// <summary>  
    54. /// 自动文件名规则:日期时间,日期_序号,序号  
    55. /// </summary>  
    56. public string AutoSaveFileName2 = "日期时间";  
    57. /// <summary>  
    58. /// 自动保存文件格式:.png, .jpg, .jpeg, .gif, .bmp  
    59. /// </summary>  
    60. public string AutoSaveFileName3 = ".png";  
    61. /// <summary>  
    62. /// 自动保存文件名序号  
    63. /// </summary>  
    64. public int autoSaveFileIndex = 0;  
    65. #endregion 自动保存参数  

    添加“AppSettingKeys”类:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 提供配置文件中AppSettings节中对应的Key名称  
    3. /// </summary>  
    4. public static class AppSettingKeys  
    5. {  
    6.     //基本设置  
    7.     public static string HotKeyMode = "HotKeyMode";  
    8.     public static string InfoBoxVisible = "InfoBoxVisible";  
    9.     public static string ToolBoxVisible = "ToolBoxVisible";  
    10.     public static string ZoomBoxVisible = "ZoomBoxVisible";  
    11.     public static string ZoomBoxWidth = "ZoomBoxWidth";  
    12.     public static string ZoomBoxHeight = "ZoomBoxHeight";  
    13.     public static string IsCutCursor = "IsCutCursor";  
    14.     //图片上传  
    15.     public static string PicDescFieldName = "PicDescFieldName";  
    16.     public static string ImageFieldName = "ImageFieldName";  
    17.     public static string PicDesc = "PicDesc";  
    18.     public static string UploadUrl = "UploadUrl";  
    19.     public static string DoUpload = "DoUpload";  
    20.     //自动保存  
    21.     public static string AutoSaveToDisk = "AutoSaveToDisk";  
    22.     public static string AutoSaveSubDir = "AutoSaveSubDir";  
    23.     public static string AutoSaveDirectory = "AutoSaveDirectory";  
    24.     public static string AutoSaveFileName1 = "AutoSaveFileName1";  
    25.     public static string AutoSaveFileName2 = "AutoSaveFileName2";  
    26.     public static string AutoSaveFileName3 = "AutoSaveFileName3";  
    27.   
    28. }  

    Program.cs文件添加枚举类型:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 控制键的类型  
    3. /// </summary>  
    4. public enum KeyModifiers : uint  
    5. {  
    6.     None = 0,  
    7.     Alt = 1,  
    8.     Control = 2,  
    9.     Shift = 4,  
    10.     Windows = 8  
    11. }  

    警告:由于xxx是引用封送类的字段,访问上面的成员可能导致运行时异常

    设置窗口完整代码:

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Windows.Forms;  
    9.   
    10. namespace Screenshot  
    11. {  
    12.     public partial class frmSetup : Form  
    13.     {  
    14.         /// <summary>  
    15.         /// 保存Form1的句柄  
    16.         /// </summary>  
    17.         private IntPtr frm1Handle = IntPtr.Zero;  
    18.   
    19.         /// <summary>  
    20.         /// 构造函数  
    21.         /// </summary>  
    22.         /// <param name="frm1_Handle"></param>  
    23.         public frmSetup(IntPtr frm1_Handle)  
    24.         {  
    25.             InitializeComponent();  
    26.             this.frm1Handle = frm1_Handle;  
    27.         }  
    28.   
    29.         /// <summary>  
    30.         /// 确定按钮单击事件处理程序  
    31.         /// </summary>  
    32.         /// <param name="sender"></param>  
    33.         /// <param name="e"></param>  
    34.         private void button_ok_Click(object sender, EventArgs e)  
    35.         {  
    36.             if (checkBox_autoSave.Checked && textBox_saveDir.Text.Trim().Length == 0)  
    37.             {  
    38.                 MessageBox.Show("您选择了“自动保存屏幕截图到磁盘” 但还没有设置存储目录!");  
    39.                 return;  
    40.             }  
    41.             if (checkBox_autoSave.Checked && textBox_saveDir.Text.Trim().Length > 0)  
    42.             {  
    43.                 if (!System.Text.RegularExpressions.Regex.IsMatch(textBox_saveDir.Text.Trim(), "^[a-zA-Z]:\\[^/:\*\?"<>\|]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))  
    44.                 {  
    45.                     MessageBox.Show("您选择了“自动保存屏幕截图到磁盘” 但设置的存储目录不是有效的目录!");  
    46.                     return;  
    47.                 }  
    48.                 if (!System.IO.Directory.Exists(textBox_saveDir.Text.Trim()))  
    49.                 {  
    50.                     MessageBox.Show("您选择了“自动保存屏幕截图到磁盘” 但设置的存储目录不存在!");  
    51.                     return;  
    52.                 }  
    53.             }  
    54.             Form1 frm = (Form1)Form.FromHandle(frm1Handle);  
    55.             if (frm != null)  
    56.             {  
    57.                 //基本设置  
    58.                 if (radioButton1.Checked) // && frm.HotKeyMode != 0 无论是否改变都重新注册热键,解决有时热键失效的问题  
    59.                 {  
    60.                     Form1.UnregisterHotKey(frm1Handle, frm.hotKeyId);  
    61.                     Form1.RegisterHotKey(frm1Handle, frm.hotKeyId, (uint)KeyModifiers.Control | (uint)KeyModifiers.Alt, Keys.A);  
    62.                     frm.HotKeyMode = 0;  
    63.                 }  
    64.   
    65.                 if (radioButton2.Checked) // && frm.HotKeyMode != 1 无论是否改变都重新注册热键,解决有时热键失效的问题  
    66.                 {  
    67.                     Form1.UnregisterHotKey(frm1Handle, frm.hotKeyId);  
    68.                     Form1.RegisterHotKey(frm1Handle, frm.hotKeyId, (uint)KeyModifiers.Control | (uint)KeyModifiers.Shift, Keys.A);  
    69.                     frm.HotKeyMode = 1;  
    70.                 }  
    71.   
    72.                 frm.InfoBoxVisible = ckb_InfoBox.Checked;  
    73.                 frm.ToolBoxVisible = ckb_ToolBox.Checked;  
    74.                 frm.IsCutCursor = ckb_CutCursor.Checked;  
    75.                 frm.ZoomBoxVisible = ckb_ZoomBox.Checked;  
    76.   
    77.   
    78.                 frm.ZoomBoxWidth1 = Convert.ToInt32(tb_zoomBoxWidth.Text);  
    79.                 frm.ZoomBoxHeight1 = Convert.ToInt32(tb_zoomBoxHeight.Text);  
    80.   
    81.                 if (frm.ZoomBoxWidth1 < 120)  
    82.                 {  
    83.                     frm.ZoomBoxWidth1 = 120;  
    84.                     tb_zoomBoxWidth.Text = frm.ZoomBoxWidth1.ToString();  
    85.                 }  
    86.                 if (frm.ZoomBoxHeight1 < 100)  
    87.                 {  
    88.                     frm.ZoomBoxHeight1 = 100;  
    89.                     tb_zoomBoxHeight.Text = frm.ZoomBoxHeight1.ToString();  
    90.                 }  
    91.   
    92.                 //图片上传  
    93.                 frm.PicDescFieldName = textBox_fieldDesc.Text;  
    94.                 frm.ImageFieldName = textBox_fieldFile.Text;  
    95.                 frm.PicDesc = textBox_desc.Text;  
    96.                 frm.UploadUrl = textBox_uploadUrl.Text;  
    97.                 frm.DoUpload = checkBox_upload.Checked;  
    98.   
    99.                 //自动保存  
    100.                 frm.AutoSaveToDisk = checkBox_autoSave.Checked;  
    101.                 frm.AutoSaveSubDir = chb_subDir.Checked;  
    102.                 frm.AutoSaveDirectory = textBox_saveDir.Text;  
    103.   
    104.                 frm.AutoSaveFileName1 = textBox_fileName1.Text;  
    105.                 if (comboBox_fileName2.SelectedItem != null)  
    106.                 {  
    107.                     frm.AutoSaveFileName2 = comboBox_fileName2.Text;  
    108.                 }  
    109.                 else  
    110.                 {  
    111.                     frm.AutoSaveFileName2 = "日期时间";  
    112.                 }  
    113.                 if (comboBox_Extn.SelectedItem != null)  
    114.                 {  
    115.                     frm.AutoSaveFileName3 = comboBox_Extn.Text;  
    116.                 }  
    117.                 else  
    118.                 {  
    119.                     frm.AutoSaveFileName3 = ".png";  
    120.                 }  
    121.             }  
    122.   
    123.             SaveConfiguration();  
    124.   
    125.             this.Close();  
    126.         }  
    127.   
    128.         /// <summary>  
    129.         /// 保存配置信息到配置文件  
    130.         /// </summary>  
    131.         private void SaveConfiguration()  
    132.         {  
    133.             System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(null);  
    134.   
    135.             //基本设置  
    136.             SetConfigAppSetting(ref config, AppSettingKeys.HotKeyMode, radioButton1.Checked ? "1" : "0");  
    137.             SetConfigAppSetting(ref config, AppSettingKeys.InfoBoxVisible, ckb_InfoBox.Checked ? "1" : "0");  
    138.             SetConfigAppSetting(ref config, AppSettingKeys.ToolBoxVisible, ckb_ToolBox.Checked ? "1" : "0");  
    139.             SetConfigAppSetting(ref config, AppSettingKeys.IsCutCursor, ckb_CutCursor.Checked ? "1" : "0");  
    140.             SetConfigAppSetting(ref config, AppSettingKeys.ZoomBoxVisible, ckb_ZoomBox.Checked ? "1" : "0");  
    141.             SetConfigAppSetting(ref config, AppSettingKeys.ZoomBoxWidth, tb_zoomBoxWidth.Text);  
    142.             SetConfigAppSetting(ref config, AppSettingKeys.ZoomBoxHeight, tb_zoomBoxHeight.Text);  
    143.   
    144.             //图片上传  
    145.             SetConfigAppSetting(ref config, AppSettingKeys.PicDescFieldName, textBox_fieldDesc.Text.Trim());  
    146.             SetConfigAppSetting(ref config, AppSettingKeys.ImageFieldName, textBox_fieldFile.Text.Trim());  
    147.             SetConfigAppSetting(ref config, AppSettingKeys.PicDesc, textBox_desc.Text.Trim());  
    148.             SetConfigAppSetting(ref config, AppSettingKeys.UploadUrl, textBox_uploadUrl.Text.Trim());  
    149.             SetConfigAppSetting(ref config, AppSettingKeys.DoUpload, checkBox_upload.Checked ? "1" : "0");  
    150.   
    151.             //自动保存  
    152.             SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveToDisk, checkBox_autoSave.Checked ? "1" : "0");  
    153.             SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveSubDir, chb_subDir.Checked ? "1" : "0");  
    154.             SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveDirectory, textBox_saveDir.Text.Trim());  
    155.             SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveFileName1, textBox_fileName1.Text.Trim());  
    156.             if (comboBox_fileName2.SelectedItem != null)  
    157.             {  
    158.                 SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveFileName2, comboBox_fileName2.Text);  
    159.             }  
    160.             else  
    161.             {  
    162.                 SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveFileName2, "日期时间");  
    163.             }  
    164.             if (comboBox_Extn.SelectedItem != null)  
    165.             {  
    166.                 SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveFileName3, comboBox_Extn.Text);  
    167.             }  
    168.             else  
    169.             {  
    170.                 SetConfigAppSetting(ref config, AppSettingKeys.AutoSaveFileName3, ".png");  
    171.             }  
    172.   
    173.             config.Save(System.Configuration.ConfigurationSaveMode.Modified);  
    174.         }  
    175.   
    176.         /// <summary>  
    177.         /// 设置配置信息  
    178.         /// </summary>  
    179.         /// <param name="config"></param>  
    180.         /// <param name="key"></param>  
    181.         /// <param name="value"></param>  
    182.         /// <returns></returns>  
    183.         private bool SetConfigAppSetting(ref System.Configuration.Configuration config, string key, string value)  
    184.         {  
    185.             try  
    186.             {  
    187.                 if (config.AppSettings.Settings[key] != null)  
    188.                 {  
    189.                     config.AppSettings.Settings[key].Value = value;  
    190.                 }  
    191.                 else  
    192.                 {  
    193.                     config.AppSettings.Settings.Add(key, value);  
    194.                 }  
    195.                 return true;  
    196.             }  
    197.             catch (Exception ex)  
    198.             {  
    199.                 MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);  
    200.                 return false;  
    201.             }  
    202.         }  
    203.   
    204.         /// <summary>  
    205.         /// 获取配置信息  
    206.         /// </summary>  
    207.         /// <param name="config"></param>  
    208.         /// <param name="key"></param>  
    209.         /// <returns></returns>  
    210.         private string GetConfigAppSetting(ref System.Configuration.Configuration config, string key)  
    211.         {  
    212.             try  
    213.             {  
    214.                 if (config.AppSettings.Settings[key] != null)  
    215.                 {  
    216.                     return config.AppSettings.Settings[key].Value;  
    217.                 }  
    218.   
    219.             }  
    220.             catch (Exception ex)  
    221.             {  
    222.                 MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);  
    223.             }  
    224.             return string.Empty;  
    225.         }  
    226.   
    227.         /// <summary>  
    228.         /// 取消按钮单击事件处理程序  
    229.         /// </summary>  
    230.         /// <param name="sender"></param>  
    231.         /// <param name="e"></param>  
    232.         private void button_cancel_Click(object sender, EventArgs e)  
    233.         {  
    234.             this.Close();  
    235.         }  
    236.         /// <summary>  
    237.         /// 窗口加载事件处理程序  
    238.         /// </summary>  
    239.         /// <param name="sender"></param>  
    240.         /// <param name="e"></param>  
    241.         private void frmSetup_Load(object sender, EventArgs e)  
    242.         {  
    243.             chb_subDir.Text = "启用(按日期命名,格式:" + DateTime.Now.Date.ToString("yyyy_MM_dd") + ")";  
    244.   
    245.             Form1 frm = (Form1)Form.FromHandle(frm1Handle);  
    246.             if (frm != null)  
    247.             {  
    248.                 //基本设置  
    249.                 if (frm.HotKeyMode == 0)  
    250.                 {  
    251.                     radioButton1.Checked = true;  
    252.                     radioButton2.Checked = false;  
    253.                 }  
    254.                 else  
    255.                 {  
    256.                     radioButton1.Checked = false;  
    257.                     radioButton2.Checked = true;  
    258.                 }  
    259.   
    260.                 ckb_InfoBox.Checked = frm.InfoBoxVisible;  
    261.                 ckb_ToolBox.Checked = frm.ToolBoxVisible;  
    262.                 ckb_CutCursor.Checked = frm.IsCutCursor;  
    263.                 ckb_ZoomBox.Checked = frm.ZoomBoxVisible;  
    264.   
    265.                 //图片上传  
    266.                 textBox_fieldDesc.Text = frm.PicDescFieldName;  
    267.                 textBox_fieldFile.Text = frm.ImageFieldName;  
    268.                 textBox_desc.Text = frm.PicDesc;  
    269.                 textBox_uploadUrl.Text = frm.UploadUrl;  
    270.                 checkBox_upload.Checked = frm.DoUpload;  
    271.   
    272.                 //自动保存  
    273.                 checkBox_autoSave.Checked = frm.AutoSaveToDisk;  
    274.                 chb_subDir.Checked = frm.AutoSaveSubDir;  
    275.                 textBox_saveDir.Text = frm.AutoSaveDirectory;  
    276.                 textBox_fileName1.Text = frm.AutoSaveFileName1;  
    277.                 comboBox_fileName2.SelectedItem = frm.AutoSaveFileName2;  
    278.                 comboBox_Extn.SelectedItem = frm.AutoSaveFileName3;  
    279.   
    280.   
    281.             }  
    282.         }  
    283.         /// <summary>  
    284.         /// 浏览按钮事件处理程序  
    285.         /// </summary>  
    286.         /// <param name="sender"></param>  
    287.         /// <param name="e"></param>  
    288.         private void button_browse_Click(object sender, EventArgs e)  
    289.         {  
    290.             FolderBrowserDialog fbd = new FolderBrowserDialog();  
    291.             fbd.Description = "请选择屏幕截图的保存目录:";  
    292.             fbd.ShowNewFolderButton = true;  
    293.             fbd.RootFolder = Environment.SpecialFolder.MyComputer;  
    294.             fbd.SelectedPath = textBox_saveDir.Text;  
    295.             if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
    296.             {  
    297.                 textBox_saveDir.Text = fbd.SelectedPath;  
    298.             }  
    299.         }  
    300.         /// <summary>  
    301.         /// 更新自动保存文件名称示例  
    302.         /// </summary>  
    303.         private void UpdateFileNameExmple()  
    304.         {  
    305.             string AutoSaveFileName2 = string.Empty;  
    306.             if (comboBox_fileName2.SelectedItem != null)  
    307.             {  
    308.                 AutoSaveFileName2 = comboBox_fileName2.Text;  
    309.             }  
    310.             string AutoSaveFileName3 = ".png";  
    311.             if (comboBox_Extn.SelectedItem != null)  
    312.             {  
    313.                 AutoSaveFileName3 = comboBox_Extn.Text;  
    314.             }  
    315.   
    316.             switch (AutoSaveFileName2)  
    317.             {  
    318.                 case "日期_序号":  
    319.                     textBox_exmple.Text = textBox_fileName1.Text + DateTime.Now.ToString("yyyy-MM-dd_") + "0001" + AutoSaveFileName3;  
    320.                     break;  
    321.                 case "序号":  
    322.                     textBox_exmple.Text = textBox_fileName1.Text + "0001" + AutoSaveFileName3;  
    323.                     break;  
    324.                 default:  
    325.                     textBox_exmple.Text = textBox_fileName1.Text + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + AutoSaveFileName3;  
    326.                     break;  
    327.             }  
    328.         }  
    329.   
    330.         private void comboBox_fileName2_SelectedIndexChanged(object sender, EventArgs e)  
    331.         {  
    332.             UpdateFileNameExmple();  
    333.         }  
    334.   
    335.         private void comboBox_Extn_SelectedIndexChanged(object sender, EventArgs e)  
    336.         {  
    337.             UpdateFileNameExmple();  
    338.         }  
    339.   
    340.         private void textBox_fileName1_TextChanged(object sender, EventArgs e)  
    341.         {  
    342.             UpdateFileNameExmple();  
    343.         }  
    344.   
    345.         // Boolean flag used to determine when a character other than a number is entered.  
    346.         private bool nonNumberEntered = false;  
    347.   
    348.         private void tb_zoomBoxWidth_KeyDown(object sender, KeyEventArgs e)  
    349.         {  
    350.             // Initialize the flag to false.  
    351.             nonNumberEntered = false;  
    352.   
    353.             // Determine whether the keystroke is a number from the top of the keyboard.  
    354.             if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)  
    355.             {  
    356.                 // Determine whether the keystroke is a number from the keypad.  
    357.                 if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)  
    358.                 {  
    359.                     // Determine whether the keystroke is a backspace.  
    360.                     if (e.KeyCode != Keys.Back)  
    361.                     {  
    362.                         // A non-numerical keystroke was pressed.  
    363.                         // Set the flag to true and evaluate in KeyPress event.  
    364.                         nonNumberEntered = true;  
    365.                     }  
    366.                 }  
    367.             }  
    368.             //If shift key was pressed, it's not a number.  
    369.             if (Control.ModifierKeys == Keys.Shift)  
    370.             {  
    371.                 nonNumberEntered = true;  
    372.             }  
    373.         }  
    374.   
    375.         private void tb_zoomBoxHeight_KeyDown(object sender, KeyEventArgs e)  
    376.         {  
    377.             // Initialize the flag to false.  
    378.             nonNumberEntered = false;  
    379.   
    380.             // Determine whether the keystroke is a number from the top of the keyboard.  
    381.             if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)  
    382.             {  
    383.                 // Determine whether the keystroke is a number from the keypad.  
    384.                 if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)  
    385.                 {  
    386.                     // Determine whether the keystroke is a backspace.  
    387.                     if (e.KeyCode != Keys.Back)  
    388.                     {  
    389.                         // A non-numerical keystroke was pressed.  
    390.                         // Set the flag to true and evaluate in KeyPress event.  
    391.                         nonNumberEntered = true;  
    392.                     }  
    393.                 }  
    394.             }  
    395.             //If shift key was pressed, it's not a number.  
    396.             if (Control.ModifierKeys == Keys.Shift)  
    397.             {  
    398.                 nonNumberEntered = true;  
    399.             }  
    400.         }  
    401.   
    402.         private void tb_zoomBoxWidth_KeyPress(object sender, KeyPressEventArgs e)  
    403.         {  
    404.   
    405.             // Check for the flag being set in the KeyDown event.  
    406.             if (nonNumberEntered == true)  
    407.             {  
    408.                 // Stop the character from being entered into the control since it is non-numerical.  
    409.                 e.Handled = true;  
    410.             }  
    411.         }  
    412.   
    413.   
    414.         private void tb_zoomBoxHeight_KeyPress(object sender, KeyPressEventArgs e)  
    415.         {  
    416.             // Check for the flag being set in the KeyDown event.  
    417.             if (nonNumberEntered == true)  
    418.             {  
    419.                 // Stop the character from being entered into the control since it is non-numerical.  
    420.                 e.Handled = true;  
    421.             }  
    422.         }  
    423.   
    424.         /// <summary>  
    425.         /// 放大镜宽度改变事件处理  
    426.         /// </summary>  
    427.         /// <param name="sender"></param>  
    428.         /// <param name="e"></param>  
    429.         private void tb_zoomBoxWidth_TextChanged(object sender, EventArgs e)  
    430.         {  
    431.             int zoomWidth = Convert.ToInt32(tb_zoomBoxWidth.Text);  
    432.             if (zoomWidth < 120) { zoomWidth = 120; }  
    433.   
    434.             tb_zoomBoxHeight.Text = ((int)(zoomWidth * 100 / 120)).ToString();  
    435.         }  
    436.   
    437.         /// <summary>  
    438.         /// 放大镜高度改变事件处理  
    439.         /// </summary>  
    440.         /// <param name="sender"></param>  
    441.         /// <param name="e"></param>  
    442.         private void tb_zoomBoxHeight_TextChanged(object sender, EventArgs e)  
    443.         {  
    444.             int zoomHeight = Convert.ToInt32(tb_zoomBoxHeight.Text);  
    445.             if (zoomHeight < 100) { zoomHeight = 100; }  
    446.   
    447.             tb_zoomBoxWidth.Text = ((int)(zoomHeight * 120 / 100)).ToString();  
    448.         }  
    449.     }  
    450. }  

    源码下载:http://download.csdn.net/detail/testcs_dn/7263143

  • 相关阅读:
    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
    11
    实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题
    tx2系统备份与恢复
    如何在Ubuntu 18.04上安装和卸载TeamViewer
    bzoj 3732 Network (kruskal重构树)
    bzoj2152 聪聪可可 (树形dp)
    牛客 216D 消消乐 (二分图最小点覆盖)
    牛客 197E 01串
    Wannafly挑战赛23
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878973.html
Copyright © 2020-2023  润新知