Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1562 | Anzahl registrierter User: 1893 | Besucher online: 302
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)

Console in eigene Form einbinden


Autor: BlackDragon
Sprache: C#
Bewertung: 5,8
(1 Bewertung)
Anzahl der Aufrufe: 18035
  
Kick it on dotnet-kicks.de  

Beschreibung:

Ob es nun Sinn macht oder nicht, manche Personen wollen die Windows Console
in ihr eigenes Programm einbinden mit dieser kleinen Klasse könnt Ihr dieses Realisieren.

TIP: sobald Ihr in der StartConsole ein anderes Programm reinsetzt
und die Constante ClassName etwas abändert könnt Ihr jedes Fenster in
eure App mit einbinden.


Abgelegt unter: Console, Externes Programm in eigene Anwendung Andocken, .



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
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;

namespace Tools{
    class CmdDocking {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr
        FindWindow([MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
        [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
           int Y, int cx, int cy, uint uFlags);

        public CmdDocking(){
            StartConsole();
        }
        private const String ClassName = "ConsoleWindowClass";
        /// <summary>
        /// Die System Console CMD.EXE starten
        /// </summary>
        private void StartConsole() {
            Process process = new Process();
            process.StartInfo = new ProcessStartInfo(string.Format(@"{0}\cmd.exe",Environment.GetFolderPath(Environment.SpecialFolder.System)));
            process.Start();
        }

        /// <summary>
        /// Console in eigene Form einbinden
        /// </summary>
        /// <param name="formToDock">Form in die, die Console eingebunden werden soll</param>
        /// <param name="posX">Position X des Consolen Fensters</param>
        /// <param name="posY">Position Y des Consolen Fensters</param>
        /// <param name="width">Breite des consolen Fensters</param>
        /// <param name="hight">Höhe des consolen Fensters</param>
        public void DockConsole(Form formToDock, int posX, int posY, int width, int hight) {
            SetWindowPos(GetHwnd(), IntPtr.Zero, posX, posY, width, hight, 0);
            SetParent(GetHwnd(), formToDock.Handle);
        }

        private IntPtr GetHwnd() {
            return FindWindow(ClassName, null);
        }
        
    }
}

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.)

Niklas schrieb am:  17.10.2010 22:19:43

Und was muss ich für Classname einsetzen, um andere Programme einzubinden?


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