wieso kein button?
-
Hallo. ich bin neu in c++ und winapi. ich habe ein fenster mit button. leider begreife ich nicht wieso der nicht angezeigt wird. kann mir jemand helfen?
#include <windows.h> #include "gui.h" #define buttons 4001 HWND but = NULL; /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); /* Make the class name into a global variable */ char szClassName[ ] = "CodeBlocksWindowsApp"; int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; /* The class is registered, let's create the program*/ hwnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "Code::Blocks Template Windows App", /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 544, /* The programs width */ 375, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); but = CreateWindow( "BUTTON", "Join", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 285, 280, 100, 28, hwnd,(HMENU)buttons,hThisInstance,NULL); /* Make the window visible on the screen */ ShowWindow (hwnd, nFunsterStil); UpdateWindow(hwnd); /* Run the message loop. It will run until GetMessage() returns 0 */ while (GetMessage (&messages, NULL, 0, 0)) { /* Translate virtual-key messages into character messages */ TranslateMessage(&messages); /* Send message to WindowProcedure */ DispatchMessage(&messages); } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; } /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) /* handle the messages */ { /*case WM_CREATE: new GUI(hwnd, ((LPCREATESTRUCT) lParam) -> hInstance); break;*/ case WM_DESTROY: PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
-
Du behandelst den Button als ChildWindow. Normalerweise erzeugt man den Button bei der Behandlung von WM_CREATE, also in der CALLBACK-Funktion.
Hier ein Beispiel:
Zu ChildWindows:
http://www.win-api.de/tutorials.php?tutid=12zu Buttons:
http://www.win-api.de/tutorials.php?tutid=15
-
also nach den tuts ist jede componente ein WS_CHILD, oder nicht? weiter habe ich das mit den chlidwindows so verstanden. Wenn man eine LRESULT CALLBACK methode verwendet und das WS_CHILD mit RegisterClass() registriert, dann gibt es ein "echtes" child window.
Also, mein code sollte funktionieren wenn ich die createwindow("BUTTON",...) in die CALLBACK nehme?
klingt für mich unlogisch.
mfg
-
Ist aber so.
Einen Button zu erzeugen ist Aufgabe des Fensters, nicht Aufgabe der WinMain.Theoretisch sind alle Elemente bei Windows Childs. Es gibt einen Haufen vordefinierte Child-Windows, die Buttons eben, die mit entsprechend anderen Anweisungen und Stilen erzeugt werden, im Grunde ist das Prinzip aber immer das Gleiche.
Wie Du in den Tuts sehen kannst, gibt es für jedes Child auch eine eigene CALLBACK (die dann z. B. ChildProc heißt). Nur bei einigen Buttons reicht die WM_COMMAND.
Versuchs einfach mal. Kann sehr lehrreich sein.
-

ich schaffs einfach nich. es spielt keine rolle wo ich die button oder den static text erstelle. er erscheint einfach nicht. ich habe das tut (also nur den abschnitt CreateWindowsEx(0,"BUTTON",...)) kopiert und gepastet. jedoch ohne erfolg. das main fenster erscheint, aber weit und breit kein spur vom Button/static text.
CreateWindowEx(0,"STATIC","some text", WS_CHILD | WS_VISIBLE,120,130,135,28,hwnd, (HMENU) 40001, hThisInstance,NULL);das sollte mir doch einen statischen text im hwnd ausgeben, nicht? und es spielt doch keine rolle wo ich diesen befehl aufrufe.
-
Da kann ich Dir im Moment auch nicht weiterhelfen. Poste mal den ganzen Codeblock, nicht nur die eine Zeile.
-
ich habs glaub ich rausgefunden. es muss eine einstellung vom comiler sein oder sowas. ich verwende code::blocks. ich habe nochmals eine neue default win32 anwendung erstellt. und den button eingefügt. das hat geklappt. dann habe ich den ganzen code kopiert und in meinem project eingefügt. dort hat wiederum nichts funktioniert. ganz komisch finde ich. immerhin weiss ich nun das es nicht am code liegt

danke vielmal trozdem.
mfg
-
So auf die schnelle fällt mir auch nichts auf, ausser dass Du keine CREATESTRUCT übergibst. Versuch das mal.
Edit: Damit hat sich wohl auch mein Kommentar erledigt, hätte wahrscheinlich eh nix genützt.
-
Problem hat sich behoben. Es hat sich um ein problem der IDE ghandelt.
danke trotzdem