Entwickler-Ecke

WinForms - Image per Tastendruck in der form verschieben


TheLol - Do 31.07.08 01:56
Titel: Image per Tastendruck in der form verschieben
hmmm ich wollte fragen ob es eine bessere lösung als das hier gibt:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch(e.KeyData)
            {
             case Keys.Down:
                if (pictureBox1.Location.Y == 230)
                {
                    break;
                }
                this.pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X ,pictureBox1.Location.Y + 10);
                toolStripStatusLabel1.Text = "Y: " + pictureBox1.Location.Y.ToString();
                break;
             case Keys.Up:
                if (pictureBox1.Location.Y == 0)
                {
                    break;
                }
                this.pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X, pictureBox1.Location.Y - 10);
                toolStripStatusLabel1.Text = "Y: " + pictureBox1.Location.Y.ToString();
                break;
             case Keys.Left:
                if (pictureBox1.Location.X == 0)
                {
                    break;
                }
                this.pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X - 10, pictureBox1.Location.Y);
                toolStripStatusLabel2.Text = "X: " + pictureBox1.Location.X.ToString();
                break;
             case Keys.Right:
                if (pictureBox1.Location.X == 280)
                {
                    break;
                }
                this.pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X + 10, pictureBox1.Location.Y);
                toolStripStatusLabel2.Text = "X: " + pictureBox1.Location.X.ToString();
                break;
            }
        }
    }
}


Auf die benamsung jetzt einfach mal nicht achten, da das nur ein test ist ;)
hmmm das x und y ist auch verdreht, da der ausgangspunkt glaube links oben ist...

Aber meine Frage ist hauptsächlich wie man es am besten lösen kann das das bild nicht aus der form raus geht, also dem nach dann mehr oder weniger unsichbar ist. den das if (pictureBox1.Location.X == 280) ist glaube nicht die beste lösung.
Evtl. habt ihr ja eine Idee.

mmfg. TheLol


Th69 - Do 31.07.08 09:52

Der exakte Vergleich ist nicht so toll, denn wenn die Ausgangskoordinaten nicht durch 10 teilbar sind, du aber immer um 10 Pixel das Bild verschiebst, dann verschwindet das Bild dann doch außerhalb der Form.
Du kannst das ganze einfach mit Math.Min() bzw. Math.Max() lösen:

C#-Quelltext
1:
2:
3:
4:
5:
int X = Math.Max(pb.Location.X - 100);

// bzw. für rechts

int X = Math.Min(pb.Location.X + 10, Width - pb.Width);

Das äquivalente dann für die Y-Position...


TheLol - Do 31.07.08 13:06

Blos ein Problem wenn man Width vom Fenster nimmt wird oben der Fenster titel mit berechnet, aber das bild kann sich nur innerhalb des Feldes bewegen

Moderiert von user profile iconNarses: Bild als Anhang hochgeladen


Kha - Do 31.07.08 13:56

Schau dir einmal die Eigenschaft ClientSize an.