2010-11-29 22 views
5

Tengo un problema extraño. Estoy haciendo una aplicación Win32 en VC++ 2008, haciendo una clase para encapsular la mayor parte del trabajo para facilitar la repetición al llamar a MessageBox. ¡El cuadro de mensaje` se crea (creo) pero no aparece a menos que presione la tecla Alt!Win32 MessageBox no aparece

¿Qué pasa exactamente es:

  1. que ejecutar el programa

  2. pulse Intro

  3. la ventana principal perder el foco

  4. dan pitido cuando hago clic en el ventana principal como si un MessageBox modal está presente

  5. Oprima Escape ... se gana el enfoque O presione Alt y luego aparecerá MessageBox con la tecla Alt presionada (es decir menú caerá) !!!!!!

P.S. Funcionaba bien, pero de repente sucedió. No encontré ninguna diferencia, ¡incluso hice un nuevo proyecto!

Esto se supone que el programa principal:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int  nCmdShow) 
{ 
    MSG msg; 
    CWnd cMainWindow(TEXT("DentoMan"), TEXT("Bejkoman")); // pass The class name and window name to the constructor 

    cMainWindow.CreateDef(); //Create the Window 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return (int)msg.wParam; 
} 

Si bien este es el archivo de clase

CWnd::CWnd() { 
}; 

CWnd::CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName) { 
    CWnd::lpszClassName  = lpszClassName; 
    CWnd::lpszWindowName = lpszWindowName; 
}; 

CWnd::~CWnd() { 
}; 

// Create the window with default parameters 
HWND CWnd::CreateDef(void) { 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = StaticWndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = (HINSTANCE)GetModuleHandle(NULL); 
    wcex.hIcon   = 0; 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 4); 
    wcex.lpszMenuName = 0; 
    wcex.lpszClassName = lpszClassName; 
    wcex.hIconSm  = 0; 

    RegisterClassEx(&wcex); 
    g_hWnd = CreateWindowEx(0,lpszClassName, lpszWindowName, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, this); 
    hInst = wcex.hInstance; //Store hInstance in the class hInst variable 

    if (!g_hWnd) return false; 
    ShowWindow(g_hWnd, SW_SHOW); 
    UpdateWindow(g_hWnd); 

    return g_hWnd; 
} 

LRESULT CALLBACK CWnd::StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { 
    /* The Only Message we take here so we store the 'this' pointer within the window to identify messages 
    comming from it by the 'this' pointer*/ 
    if (Message == WM_CREATE) { 
     SetWindowLong(hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams); 
    } 

    /* Store the window pointer in the class pointer we just created in order to run the right public WndPRoc */ 
    CWnd *Destination = (CWnd*)GetWindowLong(hWnd, GWL_USERDATA); 

    // If the hWnd has a related class, pass it through 
    if (Destination) { 
     return Destination->WndProc(hWnd, Message, wParam, lParam); 
    } 

    // No destination found, defer to system... 
    return DefWindowProc(hWnd, Message, wParam, lParam); 
}; 

LRESULT CWnd::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { 
    // Determine message type 
    switch (Message) { 
     case WM_LBUTTONDOWN: 
      { 
       /* this is a common trick for easy dragging of the window.this message fools windows telling that the user is 
       actually dragging the application caption bar.*/ 
       SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION,NULL); 
       break; 
      } 

     /*case WM_CREATE: 
      break; 
    */ 

     case WM_CLOSE: 
      PostQuitMessage(0); 
      break; 

     case WM_DESTROY: 
      UnregisterClass(lpszClassName, hInst); 
      PostQuitMessage(0); 
      break; 

     case WM_KEYDOWN: //KeyBoard keys 
      // Which key was pressed? 
      switch (wParam) { 
       case VK_ESCAPE: //close through escape key 
        PostQuitMessage(0); 
        return 0; 
       case VK_RETURN: 
        MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), NULL); 
        return 0; 
      } // End Switch 

      break; 

     case WM_COMMAND: 
      /*switch(LOWORD(wParam)) 
     { 
     }*/ 
     break; 

     case WM_PAINT: 
      break; 

     default: 
      return DefWindowProc(hWnd, Message, wParam, lParam); 

    } // End Message Switch 

return 0; 
}; 

La cabecera de la clase:

class CWnd { 
    public: 
     CWnd(); 
     CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName); 
     virtual ~CWnd(); 
     virtual HWND CreateDef(void);   // Create the window with default parameters 
     virtual LRESULT  WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam); 

    private: 
     static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam); 
     HWND  g_hWnd;  //Global window handle for this window 
     HINSTANCE hInst;  //Global instance for this window 

     LPTSTR   lpszClassName; 
     LPTSTR   lpszWindowName; 
}; 

P. S. He incluido todos los archivos de cabecera necesarios, todo va bien, excepto de mensaje

Este es también un enlace al código en here

+2

Publique el código correspondiente aquí. Además, una foto en la oscuridad: especifique una ventana principal al mostrar el cuadro de mensaje. –

+4

Editado, vinculado quitado. No publique archivos .exe inseguros. Simplemente pegue el código correspondiente. – Ruel

+0

... o puede poner el código en [ideone] (http://ideone.com/) o [pastebin] (http://pastebin.com/) – Vlad

Respuesta

8

Ohhhhhhh fin encontré la solución de este problema ... y para que todos se beneficien el problema estaba en WndProc (.......) en el mensaje WM_PAINT escribí algo de código en él y se retira todo el código junto con las funciones BeginPaint y EndPaint para que el programa ingrese un período de congelación una vez que se haya pintado todo, incluido ese MessageBox, pero solo se muestra cuando presiono Alt. Creo que el control se transfiere al sistema en ese paso para mostrar el Menú del sistema (creo)

la solución o bien eliminar el controlador de mensajes WM_PAINT o añadir las funciones normales BeginPaint y EndPaint

Gracias por todos los que pasaban en mi pregunta

2

Al crear su cuadro de mensaje debe pasar WS_CHILD en CreateWindowEx.
EDIT 2:
Ok, inténtelo.

MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), MB_OK); 
+0

No puedo hacerlo Ramilol esta es mi ventana principal, estoy llamando a la función MessageBox a través de ella ... –

+0

lo siento, no me di cuenta de que ... Realmente no puedo entender lo que dices ... Tu cuadro de mensaje doesn ¿aparece o su ventana principal? – Ramilol

+0

Lo que sucede es exactamente: 1 - ejecuto el programa 2 - pulse Intro 3 - la ventana principal perder el foco 4 - dar pitido cuando hago clic en la ventana principal como si un cuadro de mensaje modal está presente 5 - Oprima Escape ... se gana el enfoque O presiona Alt y luego aparece MessageBox con la tecla alt presionada (es decir, el menú se caerá) !!!!!! –

3

Si alguien sigue interesado, este método funciona:

MessageBox(NULL,L"error",L"Error",MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY); 
2

que tenía un problema similar que fue la causa por WM_PAINT como alguien anteriormente mencionado.Lo resolvió agregando return DefWindowProc(hWnd, Message, wParam, lParam); allí.