Buttons in Dialogfenster einbinden
-
Hi,
ich habe folgendes Problem:
Ich öffne mit der "CreateWindow"- Funktion einen Button.#include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE d2, LPSTR d3, int d4) { MSG msg; HWND hwnd; hwnd = CreateWindow("BUTTON", "Bitte Klicken", WS_VISIBLE | BS_CENTER, 100, 100, 100, 80,NULL, NULL, hInstance, NULL); while (GetMessage(&msg, NULL, 0, 0)) { if (msg.message == WM_LBUTTONUP) { DestroyWindow(hwnd); PostQuitMessage(0); } DispatchMessage(&msg); } return msg.wParam; }Dann habe ich noch ein programm geschrieben:
Es öffnet ein Dialog-Fenster und ein Button-Fenster
Mein Problem:
Ich schaffe es einfach nicht den Button in das Dialog-Fenster einzubinden!
Kann mir einer helfen?
-
Such dir ein richtiges Tutorial...das was da steht bzw. das was du da gepostet hast... ist Quatsch...
http://www.c-plusplus.net/forum/viewtopic-var-t-is-156426.htmlzu deinem Problem: Deinem Button fehlt das Parent-Window.

BTW: Man erstellt einen Button und öffnet ihn nicht.
-
Falls du ein Standard-Window .bzw. Dialog-Konstrukt suchst, guck mal hier: www.winapi.net
-
Ein Fenster oder einen Button öffnet man nicht. Ein User öffnet ein Fenster oder klickt auf einen Butten. Ein Programmierer erstellt ein Fenster oder einen Button.
Dein Button muss ein child deines Dialogs sein.

Greetz, Swordfish
-
Kannst du mir vielleicht ein Beispiel schreiben, wie man so einen Button in ein Dialogfenster programmiert?
-
Lou schrieb:
Kannst du mir vielleicht ein Beispiel schreiben, wie man so einen Button in ein Dialogfenster programmiert?
Nagut, ich will mal nicht so sein, hier:
BTW: Google ist manchmal dein Freund
.#include <windows.h> // Die Resource ist der Standard-Dialog, wenn man im // Resourceneditor von MS Visual C++ einen Dialog erstellt. #include "Resource.h" // Identifikationsnummer des Buttons: #define BTN_EXAMPLE (6000) BOOL CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { return DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc); } BOOL CALLBACK DialogProc (HWND hDlg, UINT uiMessage, WPARAM wParam, LPARAM lParam) { static HWND hbtnExample; switch(uiMessage) { case WM_INITDIALOG: hbtnExample = CreateWindow(TEXT("BUTTON"), TEXT("Aufschrift des Buttons"), WS_CHILD | WS_VISIBLE, 10, 10, 100, 20, hDlg, reinterpret_cast<HMENU>(BTN_EXAMPLE), reinterpret_cast<LPCREATESTRUCT>(lParam)->hInstance, NULL); // Hier weiteres... return (TRUE); case WM_DESTROY: case WM_CLOSE: // Hier wird der Dialog geschlossen EndDialog(hDlg,0); return (TRUE); case WM_COMMAND: // Hier werden die beiden Buttons, vom Standard-Dialog // des MS Visual Studio Resourcengenerators abgefragt. switch (LOWORD(wParam)) { case IDCANCEL: EndDialog(hDlg,0); return (TRUE); case IDOK: MessageBox(hDlg, TEXT("Standard-Button gedrückt"), TEXT("Info"), MB_OK | MB_ICONINFORMATION); return (TRUE); case BTN_EXAMPLE: MessageBox(hDlg, TEXT("Beispiel-Button gedrückt - wurde unter WM_INITDIALOG erstellt."), TEXT("Info"), MB_OK | MB_ICONINFORMATION); return (TRUE); default: break; } return (FALSE); } return (FALSE); }PS: Der Code ist
nicht
getestet!
-
Ich habe den Code einmal getstet.
Leider erkennt mein Editor nur Error-Anzeigen!
Trotzdem waren einige C-Codes wirklich hilfreich!
Vielen dank für den beitrag

-
Weiß vielleicht einer von euch, wie man die Ein -und Ausgabe unter Windows Programmiert.
(Ich mein, durch eine Edit-Box Text eingeben und den anschließend in einem Dialogfenster ausgeben?!)

-
*mein_wienerisch_auspack* Jo no-na!? *wienerisch_wieder_einpack*
zB:
#include <windows.h> const unsigned int id_edit_box = 100; const unsigned int id_show_button = 101; long __stdcall main_window_proc( HWND window, unsigned int message, unsigned int first, long second ); int __stdcall WinMain( HINSTANCE instance, HINSTANCE prev_instance, char *cmd_line, int show_state ) { char main_window_name[ ] = "test"; char main_window_class_name[ ] = "test_window_class"; WNDCLASS wc; memset( &wc, 0, sizeof( WNDCLASS ) ); wc.lpszClassName = main_window_class_name; wc.hInstance = instance; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = main_window_proc; wc.hIcon = LoadIcon( 0, IDI_APPLICATION ); wc.hCursor = LoadCursor( 0, IDC_ARROW ); wc.hbrBackground = reinterpret_cast< HBRUSH >( COLOR_WINDOW + 1 ); if( !RegisterClass( &wc ) ) { MessageBox( 0, "Could not register Window-Class!", "Error:", MB_OK ); return EXIT_FAILURE; } HWND main_window = CreateWindow( main_window_class_name, main_window_name, WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 320, 65, 0, 0, instance, 0 ); if( !main_window ) { UnregisterClass( main_window_class_name, instance ); MessageBox( 0, "Could not create the main window!", "Error:", MB_OK ); return EXIT_FAILURE; } UpdateWindow( main_window ); ShowWindow( main_window, show_state ); MSG msg; while( GetMessage( &msg, main_window, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return static_cast< int >( msg.wParam ); } long __stdcall main_window_proc( HWND window, unsigned int message, unsigned int first, long second ) { static HWND edit_box; static HWND show_button; switch( message ) { case WM_CREATE: edit_box = CreateWindow( "EDIT", "", WS_CHILD | WS_BORDER | WS_VISIBLE, 10, 10, 200, 20, window, reinterpret_cast< HMENU >( id_edit_box ), 0, 0 ); if( !edit_box ) { return -1; } show_button = CreateWindow( "BUTTON", "&Show Text", WS_CHILD | WS_BORDER | WS_VISIBLE, 220, 10, 80, 20, window, reinterpret_cast< HMENU >( id_show_button ), 0, 0 ); if( !show_button ) { return -1; } return 0; case WM_COMMAND: if( LOWORD( first ) == id_show_button ) { char *buffer; int length; length = GetWindowTextLength( edit_box ) + 1; buffer = new char[ length ]; GetWindowText( edit_box, buffer, length ); MessageBox( window, buffer, "Input:", MB_OK ); delete [ ] buffer; return 0; } break; case WM_CLOSE: PostQuitMessage( 0 ); return 0; } return static_cast< long >( DefWindowProc( window, message, first, second ) ); }Greetz, Swordfish
-
Ach ja, dem Code den CodeFinder gepostet hat musst du ein Ressource-Script
ressource.rc
IDD_DIALOG1 DIALOGEX 0, 0, 186, 95 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Dialog" FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN ENDund eine
ressource.h
#define IDD_DIALOG1 100hinzufügen. Dann musst du leider noch folgende Zeilen
case WM_INITDIALOG: hbtnExample = CreateWindow( TEXT( "BUTTON" ), TEXT( "Aufschrift des Buttons" ), WS_CHILD | WS_VISIBLE, 10, 10, 100, 20, hDlg, reinterpret_cast< HMENU >( BTN_EXAMPLE ), reinterpret_cast<LPCREATESTRUCT>(lParam)->hInstance, NULL ); // Hier weiteres... return (TRUE);durch
case WM_INITDIALOG: hbtnExample = CreateWindow( TEXT( "BUTTON" ), TEXT( "Aufschrift des Buttons" ), WS_CHILD | WS_VISIBLE, 10, 10, 100, 20, hDlg, reinterpret_cast<HMENU>( BTN_EXAMPLE ), GetModuleHandle( 0 ), 0 ); return TRUE;ersetzten.
@CodeFinder: DialogBox( ) spendiert der WM_INITDIALOG-Message keine CREATESTRUCT!
Greetz, Swordfish
-
Ich glaube wirklich du solltest dir mal ein paar gute Tutorials über WinApi-Programmierung zu Gemüte führen.
Hier mal ein paar Hinweise wo du sowas findest:
1. www.winapi.net
2. www.win-api.de
3. http://www.henkessoft3000.de/
4. ... (Google wird da sicher noch ein paar kennen)
//edit: Ganz vergessen, die FAQ dieses Forums ist auch manchmal sehr hilfreich
-
Swordfish schrieb:
@CodeFinder: DialogBox( ) spendiert der WM_INITDIALOG-Message keine CREATESTRUCT!
Jop, my fault. War ne Copy&Paste Aktion...deswegen der Fehler...GetModuleHandle(NULL) muss da natürlich hin. Danke.
