Guest User

Untitled

a guest
May 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  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 WindowsFormsApplication2
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void PB1_Click(object sender, EventArgs e)
  20.         {
  21.  
  22.         }
  23.  
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.  
  27.         }
  28.  
  29.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  30.         {
  31.             e.Handled = true;
  32.             switch (e.KeyCode)
  33.            {
  34.                 case Keys.Up:
  35.                     PB1.Top -= 5;
  36.                     break;
  37.                 case Keys.Down:
  38.                     PB1.Top += 5;
  39.                     break;
  40.                 case Keys.Left:
  41.                     PB1.Left -= 5;
  42.                     break;
  43.                 case Keys.Right:
  44.                     PB1.Left += 5;
  45.                     break;
  46.                 default:
  47.                     break;
  48.                    
  49.                  
  50.  
  51.             }
  52.             if (PB1.Top <= 0)
  53.                 PB1.Top = 0;
  54.             if (PB1.Top >= this.Height-68)
  55.                 PB1.Top = this.Height-68;
  56.             if (PB1.Left <= 0)
  57.                 PB1.Left = 0;
  58.             if (PB1.Left >= this.Width-42)
  59.                 PB1.Left = this.Width-42;
  60.  
  61.         }
  62.  
  63.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  64.         {
  65.  
  66.         }
  67.  
  68.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  69.         {
  70.             if (e.Button == MouseButtons.Left)
  71.             {
  72.                 if (e.X <= 0) return;
  73.                 if (e.Y <= 0) return;
  74.                 if (e.X >= this.Width -PB1.Width) return;
  75.                 if (e.Y >= this.Height - PB1.Height) return;
  76.                 PB1.Left = e.X;
  77.                 PB1.Top = e.Y;
  78.             }
  79.         }
  80.     }
  81. }
Add Comment
Please, Sign In to add comment