DialogBox aufrufen
-
Hiho!
Zuerst würde ich gern mal meinen Code vorausschießen:
#include <windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); BOOL CALLBACK InfoDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); // const UINT PM_DIALOQUE = WM_APP + 1; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szAppName[] = "HalloWelt"; HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); wndclass.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR)); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wndclass.lpszClassName = szAppName; RegisterClass(&wndclass); hwnd = CreateWindow( szAppName, // Klassenzugehörigkeit "Das erste Fenster", // Titelzeile WS_OVERLAPPEDWINDOW, // Fensterart CW_USEDEFAULT, // x-Wert der oberen linken Ecke CW_USEDEFAULT, // y-Wert der oberen linken Ecke CW_USEDEFAULT, // Breite des Fensters CW_USEDEFAULT, // Höhe des Fensters NULL, // Kinderfenster? NULL, // Menühandle hInstance, // Handle zur Instanz NULL ); // Parameter zur Weitergabe an WM_PAINT ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int cxChar, cyChar, cxCaps; TEXTMETRIC tm; HINSTANCE hInstance ; switch(message) { case WM_CREATE: hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; hdc = GetDC(hwnd); GetTextMetrics (hdc, &tm); cxChar = tm.tmAveCharWidth; cyChar = tm.tmHeight + tm.tmExternalLeading; cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2; //__________________________________________ // HIER //------------------------------------------ DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, InfoDialogProc); ReleaseDC(hwnd, hdc); return 0; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_DATEI_FERTIG: hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; //--------------------------------------------------------------------------- // UND HIER //--------------------------------------------------------------------------- DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, InfoDialogProc); }; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 5*cxChar, 2*cyChar, "Hallo Windows-Welt !", 20); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); } BOOL CALLBACK InfoDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: MessageBox(hDlg, "Test1", "Test2", NULL); case IDCANCEL: EndDialog(hDlg, 0); return TRUE; } break; } return FALSE; }DEr erste Aufruf der Dialogbox findest ja wärend der WM_CREATE statt, also sozusagen gleich am Anfang, dabei wird die Dialogbox auch ohne Fehler aufgerufen, aber wenn ich sie dann innerhalb der WM_COMMAND, oder meiner angedeuteten (auskommentierten PM_DIALOQUE) aufrufe, dann kommt die Fehlermeldung, dass Windows ein Problem festgestellt hat und beenden musste etc.
Mir stellt sich die Frage, wo der Unterschied ist, ob ich die DialogBox nun wärend der WM_CREATE und anderen Messages aufrufe!?
THX
-
Hast Du schon mal versucht zu debuggen? Wenn diese von Dir gezeigte Meldung kommt, dann deutet es darauf hin, dass es eine Exception in Deinem Programm gab!
-
Dann grieg ich diese Meldung:
"Unbehandelte Ausnahme in Resourcetest.exe: 0xC0000005; Acess Violation."Wenn ich mit "OK" bestätige wird auf die Zeile gezeigt, die in meinem geposteten Code unter dem Kommentar "UND HIER" steht ...
Ich denke in meinem Code ist einfach irgendein Standartfehler drin, den ich als Anfänger nciht finde
-
Hm, ich geb euch mal den ganzen Code, also inklusive Header und Resourcescript, weil ich hab das "Programm" jetzt mal auf den Dialog beschränkt und vlt. wird damit mein Problem deutlicher!
main.cpp:
#include <windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); const UINT PM_DIALOQUE = WM_APP + 1; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szAppName[] = "Name" ; HWND hwnd ; MSG msg ; WNDCLASSEX wndclass ; wndclass.cbSize = sizeof (wndclass) ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wndclass.lpszClassName = szAppName ; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ; RegisterClassEx (&wndclass) ; hwnd = CreateWindow (szAppName, "Fenstername", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE : HINSTANCE hInstance; hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG),hwnd,DlgProc); return 0 ; case WM_PAINT : return 0 ; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_1_DIALOGAUFRUF: SendMessage(hwnd,PM_DIALOQUE,0,0); break ; }; return 0; case PM_DIALOQUE: HINSTANCE hInstance2; hInstance2 = ((LPCREATESTRUCT) lParam)->hInstance ; DialogBox(hInstance2,MAKEINTRESOURCE(IDD_DIALOG),hwnd,DlgProc); return 0; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDCANCEL: EndDialog(hDlg,0); return true; } break; } return FALSE; }resource.h:
//{{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file. // Used by res.rc // #define IDD_DIALOG 101 #define IDR_MENU 102 #define ID_1_DIALOGAUFRUF 40001 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 103 #define _APS_NEXT_COMMAND_VALUE 40002 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endifres.rc:
//Microsoft Developer Studio generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #include "afxres.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // Deutsch (Deutschland) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_DEU) #ifdef _WIN32 LANGUAGE LANG_GERMAN, SUBLANG_GERMAN #pragma code_page(1252) #endif //_WIN32 ///////////////////////////////////////////////////////////////////////////// // // Dialog // DIALOG01 DIALOGEX 0, 0, 285, 201 STYLE DS_CENTER | WS_POPUP | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU CAPTION "Win32-Konstrukt" CLASS "DIALOG01" FONT 8, "MS Sans Serif" BEGIN CONTROL "Progress1",IDC_BALKEN,"msctls_progress32",PBS_SMOOTH | WS_BORDER | WS_TABSTOP,74,49,122,16 PUSHBUTTON "weiter",IDC_WEITER,78,82,57,16,BS_FLAT, WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE PUSHBUTTON "zurück",IDC_ZURUECK,147,81,49,15 CONTROL "Progress2",IDC_PROGRESS2,"msctls_progress32",WS_BORDER, 74,107,146,26 END #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE DISCARDABLE BEGIN "resource.h\0" END 2 TEXTINCLUDE DISCARDABLE BEGIN "#include ""afxres.h""\r\n" "\0" END 3 TEXTINCLUDE DISCARDABLE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Icon // // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_ICON2 ICON DISCARDABLE "ICON1.ICO" #endif // Deutsch (Deutschland) resources ///////////////////////////////////////////////////////////////////////////// #ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 3 resource. // ///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKEDDANKESCHÖN schon im Voraus!
-
case ID_DATEI_FERTIG: hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;wundert mich dass du das versuchst.
Mach hInstance static in deiner WndProc, also ganz am Anfang:LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static HINSTANCE hinstance; ........ }und benutz den Pointer auf die Createstruct nur dort wo er Sinn macht, nämlich in WM_CREATE.
case WM_CREATE : hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG),hwnd,DlgProc); return 0 ;WM_COMMAND liefert keinen Pointer mehr mit.
Dann kannst du bei deinen Dialogbox-Aufrufen immer hInstance verwenden. Alternativ geht auch GetModuleHandle(NULL); z.B.:
case PM_DIALOQUE: DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG),hwnd,DlgProc); return 0;Hoffe das hilft Dir weiter,
Gruß
Irvan
-
oops
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static HINSTANCE hInstance; // != hinstance! ........ }sollte es natürlich sein^