Autor Beitrag
TheLol
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Do 31.07.08 01:56 
hmmm ich wollte fragen ob es eine bessere lösung als das hier gibt:
ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: 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:
ausblenden 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 Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: 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
Einloggen, um Attachments anzusehen!
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 31.07.08 13:56 
Schau dir einmal die Eigenschaft ClientSize an.