Fenster aus Konsole herraus starten
-
Hi Leute;
wie kann ich in einer Konsolenanwendung eine Fensterklasse starten? ich benutze VC++6.0. Die Fensterklassen an sich sind kein Problem, wenn ich ein Projekt als win32 erstellt habe,. Aber wie kann ich eine Konsolenawendung so umändern, dass ich neben der Konsole auch ein Fenster öffnen kann?
-
hi
ich würde als grundlage ein win32 anwendung nehmen, da die konsole auch nur ein fenster ist.
den folgenden code habe ich mir mal irgendwo geholt.
poste ich einfach mal rein, zieh raus, was du brauchen kannst. vergiss nicht, eine win32 anwendung und nicht eine konsolenanwendung anzulegen.
so long#include <windows.h> #include <stdio.h> #include <fcntl.h> #include <io.h> #include <iostream> #include <string> VOID CreateConsole() { AllocConsole(); HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hConsoleInput = GetStdHandle(STD_INPUT_HANDLE); HANDLE hConsoleError = GetStdHandle(STD_ERROR_HANDLE); #ifdef __BORLANDC__ dup2(_open_osfhandle(reinterpret_cast<LONG>(hConsoleInput), _O_TEXT), 0); setvbuf(stdout, NULL, _IONBF, 0); dup2(_open_osfhandle(reinterpret_cast<LONG>(hConsoleOutput), _O_TEXT), 1); setvbuf(stdout, NULL, _IONBF, 0); dup2(_open_osfhandle(reinterpret_cast<LONG>(hConsoleError), _O_TEXT), 2); setvbuf(stdout, NULL, _IONBF, 0); #else FILE* NewStandardInput = _fdopen(_open_osfhandle(reinterpret_cast<LONG>(hConsoleInput), _O_TEXT), "r"); if(NewStandardInput != NULL) { *stdin = *NewStandardInput; } setvbuf(stdin, NULL, _IONBF, 0); FILE* NewStandardOutput = _fdopen(_open_osfhandle(reinterpret_cast<LONG>(hConsoleOutput), _O_TEXT), "w"); if(NewStandardOutput != NULL) { *stdout = *NewStandardOutput; } setvbuf(stdout, NULL, _IONBF, 0); FILE* NewStandardError = _fdopen(_open_osfhandle(reinterpret_cast<LONG>(hConsoleError), _O_TEXT), "w"); if(NewStandardError != NULL) { *stderr = *NewStandardError; } setvbuf(stderr, NULL, _IONBF, 0); #endif std::ios::sync_with_stdio(); } ///////////////////////////////////////////////////// // Prototypes ///////////////////////////////////////////////////// LRESULT CALLBACK MainWindowProcedure(HWND hWindowHandle, UINT uMessage, WPARAM wParam, LPARAM lParam); ///////////////////////////////////////////////////// // The application's entry point ///////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hInstanceHandle, HINSTANCE hPreviousInstanceHandle, LPSTR lpszCommandLine, INT nShowState) { ///////////////////////////////////////////////////// // Register main window class ///////////////////////////////////////////////////// WNDCLASSEX MainWindowClass = {0}; MainWindowClass.cbSize = sizeof(WNDCLASSEX); MainWindowClass.hInstance = hInstanceHandle; MainWindowClass.lpszClassName = "MainWindow"; MainWindowClass.lpfnWndProc = MainWindowProcedure; MainWindowClass.style = CS_HREDRAW | CS_VREDRAW; MainWindowClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); MainWindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); MainWindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); MainWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW); MainWindowClass.lpszMenuName = NULL; MainWindowClass.cbClsExtra = 0; MainWindowClass.cbWndExtra = 0; if(RegisterClassEx(&MainWindowClass) == FALSE) { MessageBox(NULL, "Can't register MainWindowClass!", "Information", MB_OK); return EXIT_FAILURE; } ///////////////////////////////////////////////////// // Create main window ///////////////////////////////////////////////////// HWND hMainWindow = CreateWindowEx( 0, "MainWindow", "Simple Window Application", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, GetModuleHandle(NULL), NULL); if(hMainWindow == NULL) { MessageBox(NULL, "Can't create main window!", "Information", MB_OK); return EXIT_FAILURE; } ///////////////////////////////////////////////////// // Create a console and do some console i/o ///////////////////////////////////////////////////// CreateConsole(); std::cout << "Hello World!" << std::endl; std::cout << "Eingabe: "; std::string s; std::getline(std::cin, s); std::cout << "Ausgabe: " << s; ///////////////////////////////////////////////////// // Do the message loop ///////////////////////////////////////////////////// MSG Message = {0}; while(GetMessage(&Message, NULL, 0, 0)) { TranslateMessage(&Message); DispatchMessage(&Message); } return Message.wParam; } ///////////////////////////////////////////////////// // Callback function for the main window ///////////////////////////////////////////////////// LRESULT CALLBACK MainWindowProcedure(HWND hWindowHandle, UINT uMessage, WPARAM wParam, LPARAM lParam) { switch(uMessage) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWindowHandle, uMessage, wParam, lParam); } return 0; }
-
Wesentlich einfacher ist die Benutzung von AllocConsole()
Edit: Nein du benützt kein "MfG SideWinder" du Bengel :p
-
Jop AllocConsole ist aber gut
MfG SideWinder
-
Ok, dankeschön, mal sehn ob ich das so verwenden kann ..