Benutzer mit den meisten Antworten
Combobox Problem, neues Fenster, wieder Combobox

Frage
-
C++ Combobox Problem Neues
Fenster wieder ComboboxHallo, Ich habe ein ComboBox Problem womit ich mich schon
länger herumschlage.
Ich möchte eine Combobox erstellen, wenn man einen
Eintrag wählt, sollte ein Fenster erstellt werden. Soweit geht dies, jedoch
möchte ich, dass die Combobox weiterhin im Hauptfenster aktiv bleibt und dass
keine neue Combobox im neuen Fenster kommt. Ich werde später eben im neuen
Fenster ein Bitmap einsetzen.
Es ist ein Win32 Projekt mit MS Visual
Express 2010, bei Eigenschaften Projekt "nicht festgelegt".
Hier der
Code:#include <windows.h> #define ID_LIST 1#define ID_TEXT 2 HINSTANCE hChildInst = 0; LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); char szClassName[ ] = "ComboBox App";int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument,int nFunsterStil) { HWND hwnd; MSG messages; WNDCLASSEX wincl; WNDCLASSEX wincl2; wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof (WNDCLASSEX); wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; if (!RegisterClassEx (&wincl))return 0; /* The Window structure */ wincl2.hInstance = hChildInst;wincl2.lpszClassName = "Proc2"; wincl2.lpfnWndProc = WindowProcedure; wincl2.style = CS_DBLCLKS; wincl2.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl2.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl2.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl2.hCursor = LoadCursor (NULL, IDC_ARROW); wincl2.lpszMenuName = NULL; wincl2.cbClsExtra = 0; wincl2.cbWndExtra = 0; wincl2.hbrBackground = (HBRUSH) COLOR_BACKGROUND; if (!RegisterClassEx (&wincl2))return 0; hwnd = CreateWindowEx ( 0, szClassName, "ComboBox App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL ); ShowWindow (hwnd, nFunsterStil); while (GetMessage (&messages, NULL, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); } return messages.wParam; } void FillListBox(HWND hwndList) {CHAR *pVarName[] = {"Item1", "Item2", "Item3", "Item4", "Item5"}; for(int i=0; i<5; i++) SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName[i]); } HWND hwndtest; LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {static HWND hwndList, hwndText, hwndComboWindow;int iIndex, iLength, cxChar, cyChar; CHAR pVarName[30]; switch (message) { case WM_CREATE: cxChar = LOWORD(GetDialogBaseUnits()); cyChar = HIWORD(GetDialogBaseUnits()); //Create Display WindowhwndText = CreateWindow(TEXT("static"),NULL, WS_CHILD | WS_VISIBLE | SS_LEFT, cxChar, cyChar, GetSystemMetrics(SM_CXSCREEN), cyChar, hwnd, (HMENU)ID_TEXT, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); //Create ComboBoxhwndList = CreateWindow(TEXT("Combobox"), NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, cxChar, cyChar*3, cxChar*16 + GetSystemMetrics(SM_CXVSCROLL), cyChar*5, hwnd, (HMENU)ID_LIST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); //Populate ComboBox FillListBox(hwndList); //Set a Default Selection SendMessage(hwndList, CB_SETCURSEL, 0, 0); GetWindowText(hwndList, pVarName, 30); SetWindowText(hwndText, pVarName); return 0; case WM_COMMAND: if( LOWORD(wParam)==ID_LIST && HIWORD(wParam)==CBN_SELCHANGE ) { //Get Selection from Combo Box GetWindowText(hwndList, pVarName, 30); //Show Text in Display Window SetWindowText(hwndText, pVarName); // HIER ERSTELLE ICH EIN NEUES FENSTER, ABER ES WIRD WIEDER "combobox" UND "static" IN DIESEM FENSTER ANGEZEIGT. // ZUDEM WIRD COMBOBOX HIER AKTIV, SOLLTE AUCH NICHT SEIN.if( strcmp(pVarName,"Item3") == 0 ){ CreateWindowEx( WS_EX_CLIENTEDGE,"Proc2", (LPCSTR) pVarName, WS_OVERLAPPEDWINDOW | WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS, 760, 0, 400, 440, 0, NULL, hChildInst, NULL ); if( hwnd == NULL ){MessageBox( NULL, "Create Window failed", "Error", MB_OK ); } // UpdateWindow( hwndComboWindow ); }if( strcmp(pVarName,"Item4") == 0 ){ CreateWindowEx( WS_EX_CLIENTEDGE,"Proc2", (LPCSTR) pVarName, WS_OVERLAPPEDWINDOW | WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS, 760, 0, 400, 440, hwnd, NULL, hChildInst, NULL ); if( hwnd == NULL ){MessageBox( NULL, "Create Window failed", "Error", MB_OK ); } // UpdateWindow( hwndComboWindow ); } } break; return 0; case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (hwnd, message, wParam, lParam); }return 0; }
Antworten
-
Das Problem liegt in Deinem Fall daran, dass Dein Hauptfenster die Fensterklasse "Proc2" nutzt, und damit auch die entsprechende Window Prozedur. Wenn Du bei Item3 und item4 ein Fenster erstellst, nutzt du wieder die Fensterklasse "Proc2". Demnach rennst du wieder im WM_CREATE in den code, der Dir Dein Static und Deine Combobox erzeugt.
Du wirst um eine weitere Fensterklasse und eine weitere windowprocedure nicht herum kommen.
Best regards
Bordon
Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.- Als Antwort vorgeschlagen Martin Richter Montag, 14. Januar 2013 09:36
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Freitag, 16. Januar 2015 07:43