Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1551 | Anzahl registrierter User: 1841 | Besucher online: 252
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Fixes Form in Höhe und Breite verdoppeln, Inhalt skalieren


Autor: ole
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 3286
  
Kick it on dotnet-kicks.de  

Beschreibung:

Ein einfaches Form, wo alles feste Größe hat, soll in der Höhe und Breite doppelt so groß werden. (bei mir, wenn Höhe des Bildschirms einen Wert 640 überschreitet.)

Anchor steht bei mir immer auf links-oben.

Schriftgrößen und Controlinhalte sollen mit wachsen.

Nicht perfekt. Radiobuttons und Checkboxen wachsen leider nicht komplett mit. Funktioniert sonst aber bei mir wie gewünscht.


Abgelegt unter: Form, Size, Skalieren.



C#
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        public int FormScaleFactor = 1;


        /// <summary>
        /// Form ggf. skalieren, wenn Bildschirmhöhe größer 640
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Shown(object sender, EventArgs e)
        {

            if (this.DesignMode) return;

            int intY = Screen.PrimaryScreen.Bounds.Height;
            if (intY >= 640)
            {
                FormScaleFactor = 2;

                this.SuspendLayout();

                //Font skalieren
                float currentSize = this.Font.SizeInPoints;
                currentSize = currentSize * FormScaleFactor;
                this.Font = new Font(this.Font.Name, currentSize,
                    this.Font.Style);

                //Form vergrößern
                this.Size = new Size(
                                this.Width * FormScaleFactor, 
                                this.Height * FormScaleFactor);

                //Controls des Forms skalieren
                foreach (Control ctrl in this.Controls)
                    ReSizeByFactor(ctrl);

                this.ResumeLayout();

            }
        }

        /// <summary>
        /// Skaliert ein Control und ruft rekursiv die Childcontrols auf
        /// </summary>
        /// <param name="ctrl">Control</param>
        /// <remarks>Faktor in FormScaleFactor</remarks>
        public void ReSizeByFactor(Control ctrl)
        {

            if (FormScaleFactor == 1) return;            

            DockStyle olddock = ctrl.Dock;
            AnchorStyles oldanchor = ctrl.Anchor;
            Point oldLocation = new Point(ctrl.Left, ctrl.Top);
           
            ctrl.Dock = DockStyle.None;
            ctrl.Anchor = AnchorStyles.None;

            ctrl.Size = new Size(
                ctrl.Width * FormScaleFactor, 
                ctrl.Height * FormScaleFactor);
            ctrl.Location = new Point(
                oldLocation.X * FormScaleFactor, 
                oldLocation.Y * FormScaleFactor);

            if (ctrl.Font != ctrl.Parent.Font)
            {
                //Font vergrößern, wenn Control eine eigene Schrift hat
                float currentSize = ctrl.Font.SizeInPoints;
                currentSize = currentSize * FormScaleFactor;
                ctrl.Font = new Font(ctrl.Font.Name, currentSize,
                    ctrl.Font.Style);
            }

            //bestimmte Control-Klassen noch gesondert behandeln
            if (ctrl is Button)
            {
                //Bild vergrößern 
                //Die Methode ResziePicByWitdh in anderem Snippet suchen!
                Button btn = ctrl as Button;
                if (btn.Image != null)
                    btn.Image = ResizePicByWidth(
                        btn.Image, btn.Image.Width * FormScaleFactor);
                if (btn.BackgroundImage != null)
                    btn.BackgroundImage = ResizePicByWidth(
                        btn.BackgroundImage, btn.BackgroundImage.Width * FormScaleFactor);
            }
            else if (ctrl is ListView)
            {
                ListView lst = ctrl as ListView;
                foreach (ColumnHeader clh in lst.Columns)
                {
                    clh.Width = clh.Width * FormScaleFactor;
                }
            }
            //else if (ctrl is ...)

            ctrl.Dock = olddock;
            ctrl.Anchor = oldanchor;

            //Rekursiv die enthaltenen Controls weiter skalieren
            foreach (Control ctrlchild in ctrl.Controls)
                ReSizeByFactor(ctrlchild);

        }
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.