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: 188
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)

Virtuelle Konsole in einer Windowsanwendung integriert


Autor: Konstantin Gross
Sprache: C#
Bewertung:
9.65 (9 votes)
Anzahl der Aufrufe: 22585
  
Kick it on dotnet-kicks.de  

Beschreibung:

Mit meiner folgender Klasse, kann man eine 'diskrete' Konsole in seine Anwendung einbauen. Wie man die Konsole mit weiteren Befehlen erweitert, kann einfach in der Konsoleklasse eingesehen werden. Aufgerufen wird sie mit der Taste (^ - Befindet sich links neben der Taste '1').

Hinweiße:
- Eure Form muss von IMessageFilter erben. (Bsp.: public partial class Form1 : Form, IMessageFilter)

- In eurem Formkonstruktor müsst ihr folgende Funktion aufrufen (Application.AddMessageFilter(this);)

- In das Form_Load Event:
my_console = new VirtualConsole(this);
my_console.SetVisibility(false);

- Un zu guter letzt, folgende Funktion in die Form, wo die Konsole angezeigt werden soll:
// Fange die gedrückte Tasten ^,Bildhoch, Bildrunter und andere Tasten der Anwendung ab
public bool PreFilterMessage(ref Message msg)
{
if (msg.Msg == WM_KEYDOWN)
{
if (msg.WParam.ToString() == "220")
{
// Taste ^
my_console.SetVisibility(!my_console.GetVisibility());
return true;
}
else
{
// Erlaube das scrollen in der Konsole nur wenn die Konsole sichtbar ist
if (my_console.GetVisibility() == true)
{
if (msg.WParam.ToString() == "33")
{
// Bildhoch
my_console.Scroll_Up();
return true;
}
else if (msg.WParam.ToString() == "34")
{
// Bildrunter
my_console.Scroll_Down();
return true;
}
}
}
}

return false;
}


Abgelegt unter: Konsole, Console, virtuell, virtual, Shell.



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * VirtualConsole - Darstellung einer Konsole in einer Windowsanwendung
 * --------------------------------------------------------------------
 * Version: 1.0
 * Copyright © 2007 Konstantin Gross
 * http://www.texturenland.de
 *
*/


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace TL.Console
{
    /// <summary>
    /// Darstellung einer 'virtuellen' Konsole, die man in eine Windowsanwendung integrieren kann.
    /// 
    /// VirtualConsole - © Konstantin Gross
    /// </summary>
    class VirtualConsole
    {
        delegate void EditLabelDelegate(Label lbl, string str);        

        // Ausgeführte Kommandos
        string[] cmd_history_list;
        int cmd_history_pointer;
        int cmd_scroll_index;

        // Konsolen Log Bildschirm
        string[] con_log_list;
        int con_log_pointer;
        int scroll_index;

        // Sonstiges
        int displayed_line;
        Form parent_form;

        #region Form Controls

        System.Windows.Forms.Panel ConsolePanel;
        System.Windows.Forms.Label ConsoleLabel;
        System.Windows.Forms.TextBox ConsoleCommandTextBox;
        System.Windows.Forms.Label[] ConsoleTextBox;

        private void InitializeConsoleComponent()
        {
            ConsolePanel = new System.Windows.Forms.Panel();
            ConsoleTextBox = new System.Windows.Forms.Label[displayed_line];
            ConsoleCommandTextBox = new System.Windows.Forms.TextBox();
            ConsoleLabel = new System.Windows.Forms.Label();
            ConsolePanel.SuspendLayout();

            // 
            // ConsolePanel
            // 
            this.ConsolePanel.BackColor = System.Drawing.Color.MediumBlue;
            this.ConsolePanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            for (int i = 0; i < ConsoleTextBox.Length; i++)
            {
                this.ConsoleTextBox[i] = new Label();
                this.ConsolePanel.Controls.Add(ConsoleTextBox[i]);
            }

            this.ConsolePanel.Controls.Add(this.ConsoleCommandTextBox);
            this.ConsolePanel.Controls.Add(this.ConsoleLabel);
            this.ConsolePanel.Dock = System.Windows.Forms.DockStyle.Top;
            this.ConsolePanel.Location = new System.Drawing.Point(0, 410);
            this.ConsolePanel.Name = "ConsolePanel";
            this.ConsolePanel.Size = new System.Drawing.Size(818, 35 + displayed_line * 18);
            this.ConsolePanel.TabIndex = 0;
            this.ConsolePanel.Visible = false;
            // 
            // ConsoleTextBox's
            // 
            for (int i = 0; i < ConsoleTextBox.Length; i++)
            {
                this.ConsoleTextBox[i].BackColor = System.Drawing.Color.MediumBlue;
                this.ConsoleTextBox[i].Dock = System.Windows.Forms.DockStyle.Top;
                this.ConsoleTextBox[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.ConsoleTextBox[i].ForeColor = System.Drawing.Color.White;
                this.ConsoleTextBox[i].Location = new System.Drawing.Point(0, i * 18);
                this.ConsoleTextBox[i].Name = "ConsoleLabel" + i;
                this.ConsoleTextBox[i].Size = new System.Drawing.Size(816, 18);
                this.ConsoleTextBox[i].Text = "" + i;
                this.ConsoleTextBox[i].Click += new System.EventHandler(this.ConsoleLabel_Click);
            }

            // 
            // ConsoleCommandTextBox
            // 
            this.ConsoleCommandTextBox.BackColor = System.Drawing.Color.Navy;
            this.ConsoleCommandTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.ConsoleCommandTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.ConsoleCommandTextBox.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ConsoleCommandTextBox.ForeColor = System.Drawing.Color.White;
            this.ConsoleCommandTextBox.Location = new System.Drawing.Point(0, 89);
            this.ConsoleCommandTextBox.Name = "ConsoleCommandTextBox";
            this.ConsoleCommandTextBox.Size = new System.Drawing.Size(816, 15);
            this.ConsoleCommandTextBox.TabIndex = 2;
            this.ConsoleCommandTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ConsoleCommandTextBox_KeyDown);
            // 
            // ConsoleLabel
            // 
            this.ConsoleLabel.BackColor = System.Drawing.Color.Navy;
            this.ConsoleLabel.Dock = System.Windows.Forms.DockStyle.Top;
            this.ConsoleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ConsoleLabel.ForeColor = System.Drawing.Color.White;
            this.ConsoleLabel.Location = new System.Drawing.Point(0, 0);
            this.ConsoleLabel.Name = "ConsoleLabel";
            this.ConsoleLabel.Size = new System.Drawing.Size(816, 18);
            this.ConsoleLabel.TabIndex = 0;
            this.ConsoleLabel.Text = "- Konsole -";
            this.ConsoleLabel.Click += new System.EventHandler(this.ConsoleLabel_Click);

            this.ConsolePanel.ResumeLayout(false);
            this.ConsolePanel.PerformLayout();

            parent_form.Controls.Add(this.ConsolePanel);
        }

        private void ConsoleTextBox_Click(object sender, EventArgs e)
        {
            ConsoleCommandTextBox.Focus();
        }

        private void ConsoleLabel_Click(object sender, EventArgs e)
        {
            ConsoleCommandTextBox.Focus();
        }

        private void ConsoleCommandTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                //Wenn ENTER gedrückt wurde, führe das Kommando aus
                string cmd = ConsoleCommandTextBox.Text;
                ConsoleCommandTextBox.Text = "";
                ExecuteCommand(cmd);
            }
            else if (e.KeyValue == 38)
            {
                // Go down in the history list
                cmd_scroll_index--;
                if (cmd_scroll_index < 0) cmd_scroll_index = cmd_history_list.Length - 1;


                // Display command in testbox
                int projected_index = (cmd_history_list.Length + cmd_history_pointer + cmd_scroll_index) % (cmd_history_list.Length);
                ConsoleCommandTextBox.Text = cmd_history_list[projected_index];
            }
            else if (e.KeyValue == 40)
            {
                // Go down in the history list
                cmd_scroll_index++;
                if (cmd_scroll_index > cmd_history_list.Length - 1) cmd_scroll_index = 0;

                // Display command in testbox
                int projected_index = (cmd_history_list.Length + cmd_history_pointer + cmd_scroll_index) % (cmd_history_list.Length);
                ConsoleCommandTextBox.Text = cmd_history_list[projected_index];
            }
        }


        #endregion

        /// <summary>
        /// In welcher Form soll die Konsole angezeigt werden
        /// </summary>
        /// <param name="parent">Das Parent-Form mitteilen</param>
        public VirtualConsole(Form parent)
        {
            parent_form = parent;

            // Konstruktor mit Standardwerten
            con_log_list = new String[100];
            con_log_pointer = 0;
            scroll_index = 0;

            // Ausgeführte Kommandos
            cmd_history_list = new String[50];
            cmd_history_pointer = 0;
            cmd_scroll_index = 0;

            displayed_line = 6;

            InitializeConsoleComponent();

            WriteLine("Geben Sie help ein, um eine Liste der Kommandos anzuzeigen");
            WriteLine("----------------------------------------------------------");
            WriteLine(" - Benutze ^ um die Konsole anzuzeigen/zu verstecken");
            WriteLine(" - Benutze Hoch/Runter um die letzten Kommandos zu sehen");
            WriteLine(" - Benutze Bildhoch/Bildrunter um in der Konsolenlog zu scrollen");
            WriteLine("- © 2007 Konstantin Gross - www.texturenland.de -");
        }

        /// <summary>
        /// Setzt die Sichtbarkeit der Konsole.
        /// </summary>
        /// <param name="visible">Soll die Konsole sichtbar sein?</param>
        public void SetVisibility(bool visible)
        {
            ConsolePanel.Visible = visible;

            if (visible == true)
            {
                ConsoleCommandTextBox.Focus();
            }
            else
            {
                parent_form.Focus();
            }
        }

        /// <summary>
        /// Wird ausgeführt wenn man hochscrollt im Log Bildschirm.
        /// </summary>
        public void Scroll_Up()
        {
            if (scroll_index >= con_log_list.Length - 1 - displayed_line) return;
            scroll_index++;

            RefreshLayout();
        }

        /// <summary>
        /// Wird ausgeführt wenn man runterscrollt im Log Bildschirm.
        /// </summary>
        public void Scroll_Down()
        {
            if (scroll_index <= 0) return;

            scroll_index--;

            RefreshLayout();
        }

        /// <summary>
        /// Rufe den aktuellen Sichtbarkeitsstauts der Konsole ab.
        /// </summary>
        /// <returns>Ist die Konsole sichtbar?</returns>
        public bool GetVisibility()
        {
            return ConsolePanel.Visible;
        }

        /// <summary>
        /// Führt die eingegebenen Kommandos aus.
        /// </summary>
        /// <param name="command">Kommando</param>
        private void ExecuteCommand(String command)
        {
            cmd_scroll_index = 0;

            // Wenn kein Kommando angegeben wurde, egal =)
            if (command == "") return;

            scroll_index = 0;

            // Speichere die Kommandis in der History
            cmd_history_list[cmd_history_pointer] = command;
            cmd_history_pointer++;

            if (cmd_history_pointer > cmd_history_list.Length - 1)
                cmd_history_pointer = 0;
            
            WriteLine(">" + command);
            command = command.ToLower();

            // Führe das Kommando aus, wenn es existiert
            switch (command)
            {
                case "exit":
                    Application.Exit();
                    break;

                case "help":
                    WriteLine("Das ist die Liste der vorhandenen Kommandos:");
                    WriteLine(" - help : Dieser Bildschirm.");
                    WriteLine(" - version : Erhält die Programmversion.");
                    WriteLine(" - hide : Verstecke die Konsole.");
                    WriteLine(" - clear : Lösche den Text in der Konsole.");
                    WriteLine(" - message : Zeigt eine MessageBox an.");
                    WriteLine(" - test : Teste eine interne Funktion.");
                    WriteLine(" - exit : Beende die Anwendung.");
                    break;

                case "hide":
                    SetVisibility(false);
                    break;

                case "clear":
                    Clear();
                    break;

                case "version":
                    WriteLine(Application.ProductName + " - v" + Application.ProductVersion);
                    break;

                case "message":
                    MessageBox.Show("ICH LEBE!!!!!");
                    break;

                case "test":
                    for (int i = 0; i <= 100; i++)
                    {
                        for (int j = 0; j <= 500; j++)
                        {
                            Application.DoEvents();
                        }

                        WriteLineForProgressive("WoW ein Fortschritt", i, 100);
                    }
                    break;


                default:
                    WriteLine("Unbekanntes Kommando '" + command + "'.");
                    break;
            }
        }

        /// <summary>
        /// Schreibe eine Text in die Konsole.
        /// </summary>
        /// <param name="str">Zu übergebender Text</param>
        public void WriteLine(string str)
        {
            con_log_list[con_log_pointer] = str;

            con_log_pointer++;

            if (con_log_pointer > con_log_list.Length - 1)
                con_log_pointer = 0;

            RefreshLayout();
        }

        /// <summary>
        /// Schreibe einen Text mit Fortschrittsanzeige
        /// </summary>
        /// <param name="str">Zu übergebender Text</param>
        /// <param name="progress">Fortschritt</param>
        /// <param name="total">Maximal</param>
        public void WriteLineForProgressive(string str, int progress, int total)
        {
            // Message in more states
            if ((ReadlastLine() != null) && (ReadlastLine().Length >= str.Length) && ((ReadlastLine()).Substring(0, str.Length) == str))
            {
                int prozent = (progress * 100) / total;
                WriteErasingCurrentLine(str + " [" + prozent + "%]");
            }
            else
            {
                int prozent = (progress * 100) / total;
                WriteLine(str + " [" + prozent + "%]");
            }
        }

        /// <summary>
        /// Lese letzte Zeile in der Konsole.
        /// </summary>
        /// <returns>Letze Zeile in der Konsole</returns>
        private String ReadlastLine()
        {
            return con_log_list[(con_log_pointer == 0 ? con_log_list.Length - 1 : con_log_pointer - 1)];
        }

        /// <summary>
        /// Schreibe einen Text in die Konsole.
        /// </summary>
        /// <param name="str">Zu übergebender Text</param>
        public void Write(String str)
        {
            con_log_list[con_log_pointer] = con_log_list[con_log_pointer] + str;
            RefreshLayout();
        }

        /// <summary>
        /// Lösche aktuelle Zeile
        /// </summary>
        /// <param name="str">Zu löschender Text</param>
        private void WriteErasingCurrentLine(String str)
        {

            con_log_list[(con_log_pointer == 0 ? con_log_list.Length - 1 : con_log_pointer - 1)] = str;
            RefreshLayout();
        }

        /// <summary>
        /// Löscht den Text in der Konsolenanzeige.
        /// </summary>
        public void Clear()
        {
            con_log_list = new String[50];
            con_log_pointer = 0;

            RefreshLayout();
        }

        /// <summary>
        /// Aktualisiere alle Controls.
        /// </summary>
        private void RefreshLayout()
        {
            int projected_index = 0;
            for (int line_counter = 0; line_counter < displayed_line; line_counter++)
            {
                projected_index = ((con_log_list.Length - 1) + con_log_pointer - line_counter - scroll_index) % (con_log_list.Length);                
                parent_form.Invoke(new EditLabelDelegate(EditLabel), ConsoleTextBox[line_counter], con_log_list[projected_index]);
            }
        }

        private void EditLabel(Label lbl, String str)
        {
            lbl.Text = str;
        }
    }
}
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.)

Konstantin Gross schrieb am:  13.12.2007 10:21:28

Screenshots von der Konsole findet ihr auf meinem Blog:
http://blog.texturenland.de/?p=58


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