1.textbox显示鼠标相对于某个的坐标:

    1. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    2. {
    3. Point screenPoint =pictureBox1 .PointToClient( Control.MousePosition);//鼠标相对于屏幕左上角的坐标
    4. MousePosition.Text = screenPoint.ToString();/////此处MousePosition指的是textbox的名字
    5. }

    2.picturebox随着窗体的大小同比例的变大变小:(Form内写)

    1. #region 控件大小随着窗体大小等比例缩放
    2. private float x;//定义当前窗体的宽度
    3. private float y;//定义当前窗体的高度
    4. private void setTag(Control cons)
    5. {
    6. foreach (Control con in cons.Controls)
    7. {
    8. con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
    9. if (con.Controls.Count > 0)
    10. {
    11. setTag(con);
    12. }
    13. }
    14. }
    15. private void setControls(float newx, float newy, Control cons)
    16. {
    17. //遍历窗体中的控件,重新设置控件的值
    18. foreach (Control con in cons.Controls)
    19. {
    20. //获取控件的Tag属性值,并分割后存储字符串数组
    21. if (con.Tag != null)
    22. {
    23. string[] mytag = con.Tag.ToString().Split(new char[] {';'});
    24. //根据窗体缩放的比例确定控件的值
    25. con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
    26. con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
    27. con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
    28. con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
    29. Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
    30. con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
    31. if (con.Controls.Count > 0)
    32. {
    33. setControls(newx, newy, con);
    34. }
    35. }
    36. }
    37. }
    38. public Form1()
    39. {
    40. InitializeComponent();
    41. x = this.Width;
    42. y = this.Height;
    43. setTag(this);
    44. }
    45. private void Form1_Resize(object sender, EventArgs e)
    46. {
    47. float newx = (this.Width) / x;
    48. float newy = (this.Height) / y;
    49. setControls(newx, newx, this);
    50. }
    51. #endregion

    3.关于Form不用的控件(按错)系统自动生成的代码如pictureBox_clicked 等怎么删除
    点击控件-找到控件的事件-全选-删除
    /或者 删除已经删除控件的代码,可以在编译生成指向Error时删除掉与这个控件相关的代码

    4.picturebox各种使用:
    PictureBox不是容器控件,所以他没有AutoScrollMinSize和AutoScrollPosition两个属性,所以不能直接设置,有3个方案:1)改为在Panel控件中画:
    然后通过设置AutoScrollMinSize属性,设置滚动条的滚动范围,然后滚动的时候刷新图即可(注意此时绘制使用的X,Y坐标,要加上滚动条的滚动值)。
    2)自己在PictureBox右边和下面添加两个滚动条。
    3)在内存位图上画,然后把位图拷贝到PictureBox控件的Image属性内。

    picturebox使用滚动条:
    将picturebox放到Panel上,并设置以下属性。
    1.panel的AutoScroll设置为ture;
    2.picturebox的SizeMode设置为AutoSize;
    3.picturebox的Dock设置为None.
    4.picturebox的Anchor设置为上,左停靠。

    5.两个窗体传值
    Form2(子窗体)
    image.png
    Form1(主窗体)
    image.png

    功能是:
    首先Form1窗体内有初始定义好的GDI图形(xy轴,间隔,栅格点等等)
    窗体一中的菜单栏设置打开Form2窗体——>Form2窗体改变textbox或者复选框checkbox的值,并将值传递给form1进行属性的修改

    FORM1内写:
    <首先是初值的赋值>
    static Graphics g;
    public int axis_x = 50;//用户定义x坐标轴间距(初始x间距为50)
    public int axis_y = 60;//用户定义y坐标轴间距(初始y间距为60)
    public int axisLine = 8;//坐标轴的线高
    public int grid = 50;//栅格间距
    public int grid_visiable = 1;//显示栅格
    int pic_height = 1500;
    int pic_width = 2000;
    static bool flash = false;

    private void pictureboxSet()
    {
    pictureBox2.Height = pic_height;
    pictureBox2.Width = pic_width;
    }

    1. private void pictureBox2_Paint(object sender, PaintEventArgs e)
    2. {
    3. g = e.Graphics;//画板
    4. Pen p = new Pen(Color.Black, 2);
    5. if (flash)
    6. {
    7. g = e.Graphics;
    8. g.Clear(Color.White);
    9. DrawGrid(e);
    10. flash = false;
    11. }
    12. else
    13. {
    14. DrawGrid(e);
    15. }
    16. }
    1. private void DrawGrid(PaintEventArgs e)//PaintEventArgs e
    2. {
    3. pictureboxSet();
    4. g = e.Graphics;//实例化Graphics
    5. Brush brush = new SolidBrush(Color.Black);
    6. Pen pen = new Pen(Color.Black, 2);
    7. Font font = new Font(Control.DefaultFont, FontStyle.Regular);
    8. SysSetting sysset = new SysSetting();
    9. int row = pic_height / axis_y;
    10. int colums = pic_width / axis_x;
    11. for (int i = 0; i < colums; i++)//画x轴
    12. {
    13. PointF pointF = new PointF(i * pic_width / colums, 0);
    14. string s = Convert.ToString(i * axis_x);
    15. int x1 = (i + 1) * pic_width / colums;
    16. int y1 = 0;
    17. int x2 = x1;
    18. int y2 = axisLine;
    19. g.DrawLine(pen, x1, y1, x2, y2);//初始坐标轴轴线的高度为8
    20. if (i % 1 == 0)
    21. {
    22. g.DrawString(s, font, brush, pointF);
    23. }
    24. }
    25. for (int i = 0; i < row; i++)//画y轴
    26. {
    27. PointF pointF = new PointF(0, i * pic_height / row);
    28. string s = Convert.ToString(i * axis_y);
    29. int x1 = 0;
    30. int x2 = axisLine;
    31. int y1 = (i + 1) * axis_y;
    32. int y2 = y1;
    33. g.DrawLine(pen, x1, y1, x2, y2);
    34. if (i % 1 == 0)
    35. {
    36. g.DrawString(s, font, brush, pointF);
    37. }
    38. }
    39. if(grid_visiable==1)
    40. {
    41. for (int i = 0; i < pictureBox2.Width / grid; i++)//画栅格点
    42. {
    43. for (int j = 0; j < pictureBox2.Height / grid; j++)
    44. {
    45. PointF pointF = new PointF((i + 1) * grid, (j + 1) * grid);
    46. g.DrawEllipse(pen, (i + 1) * grid, (j + 1) * grid, 2, 2);
    47. }
    48. }
    49. }
    50. }

    <设置按钮————-按下设置按钮打开Form2>

    1. private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
    2. {
    3. SysSetting SysSet = new SysSetting();
    4. SysSet.ShowDialog(this);//传值窗口
    5. }

    ——-form1的函数可以由form2调用??主要是因为在form2的button1_click中定义Form1 form1 = (Form1)this.Owner;

    1. public void GetMessage(string[] str)
    2. {
    3. MessageBox.Show("设置完成");
    4. pic_width = Convert.ToInt32(str[0]);
    5. pic_height = Convert.ToInt32(str[1]);
    6. grid = Convert.ToInt32(str[2]);
    7. grid_visiable = Convert.ToInt32(str[3]);
    8. axis_x = Convert.ToInt32(str[4]);
    9. axis_y = Convert.ToInt32(str[5]);
    10. flash = true;
    11. pictureBox2.Refresh();//刷新picturebox2的工作区并重绘制
    12. }

    SystemSetting系统设置

    1. #region 窗口传递textbox及checkbox的值
    2. string str;
    3. private string GetMessage(Control con)//textbox获取信息
    4. {
    5. try
    6. {
    7. if (con is TextBox && con.Text != null)
    8. {
    9. str = con.Text.ToString();
    10. }
    11. else
    12. {
    13. str = "0";
    14. }
    15. }
    16. catch (Exception ex)
    17. {
    18. MessageBox.Show(ex.ToString());
    19. }
    20. return str;
    21. }
    22. public string[] conSet = new string[7];
    23. private void button1_Click(object sender, EventArgs e)
    24. {
    25. Form1 form1 = (Form1)this.Owner;
    26. conSet[0]=GetMessage(textBox1);
    27. conSet[1] = GetMessage(textBox2);
    28. conSet[2] = GetMessage(textBox3);
    29. if (checkBox1.Checked == true)
    30. { conSet[3] = "1"; }
    31. else { conSet[3] = "0"; }
    32. conSet[4]= GetMessage(textBox5);
    33. conSet[5] = GetMessage(textBox6);
    34. conSet[6] = GetMessage(textBox4);
    35. form1.GetMessage(conSet);
    36. this.Close();
    37. }
    38. #endregion