Form1.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace HelloAppp
  11. {
  12. public partial class Form1 : Form
  13. {
  14. Point moveStart; // точка для перемещения
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. this.FormBorderStyle = FormBorderStyle.None;
  19. this.BackColor = Color.Yellow;
  20. Button button1 = new Button
  21. {
  22. Location = new Point
  23. {
  24. X = this.Width / 3,
  25. Y = this.Height / 3
  26. }
  27. };
  28. button1.Text = "Закрыть";
  29. button1.Click += button1_Click;
  30. this.Controls.Add(button1); // добавляем кнопку на форму
  31. this.Load += Form1_Load;
  32. this.MouseDown += Form1_MouseDown;
  33. this.MouseMove += Form1_MouseMove;
  34. }
  35. private void button1_Click(object sender, EventArgs e)
  36. {
  37. this.Close();
  38. }
  39. private void Form1_Load(object sender, EventArgs e)
  40. {
  41. System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath();
  42. // создаем эллипс с высотой и шириной формы
  43. myPath.AddEllipse(0, 0, this.Width, this.Height);
  44. // создаем с помощью элипса ту область формы, которую мы хотим видеть
  45. Region myRegion = new Region(myPath);
  46. // устанавливаем видимую область
  47. this.Region = myRegion;
  48. }
  49. private void Form1_MouseDown(object sender, MouseEventArgs e)
  50. {
  51. // если нажата левая кнопка мыши
  52. if (e.Button == MouseButtons.Left)
  53. {
  54. moveStart = new Point(e.X, e.Y);
  55. }
  56. }
  57. private void Form1_MouseMove(object sender, MouseEventArgs e)
  58. {
  59. // если нажата левая кнопка мыши
  60. if ((e.Button & MouseButtons.Left) != 0)
  61. {
  62. // получаем новую точку положения формы
  63. Point deltaPos = new Point(e.X - moveStart.X, e.Y - moveStart.Y);
  64. // устанавливаем положение формы
  65. this.Location = new Point(this.Location.X + deltaPos.X,
  66. this.Location.Y + deltaPos.Y);
  67. }
  68. }
  69. }
  70. }