2012-10-18 35 views
2

我正試圖創建簡單的Windows應用程序uisng WinAPi。 下面是代碼:
WinApi,不顯示按鈕

 #include "stdafx.h" 
     #include "APIup.h" 

     #define MAX_LOADSTRING 100 
     HWND hWnd,cw13; 
     // Global Variables: 
     HINSTANCE hInst;        // current      instance 
     TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
     TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 

     // Forward declarations of functions included in this code module: 
     ATOM    MyRegisterClass(HINSTANCE hInstance); 
     ATOM Register(HINSTANCE hInstance); 
     BOOL    InitInstance(HINSTANCE, int); 
     LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
     LRESULT CALLBACK WndProc_dla13(HWND, UINT, WPARAM, LPARAM); 
     INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

     int APIENTRY _tWinMain(HINSTANCE hInstance, 
          HINSTANCE hPrevInstance, 
          LPTSTR lpCmdLine, 
          int  nCmdShow) 
     { 
     UNREFERENCED_PARAMETER(hPrevInstance); 
     UNREFERENCED_PARAMETER(lpCmdLine); 

      // TODO: Place code here. 
     MSG msg; 
       HACCEL hAccelTable; 

     // Initialize global strings 
     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
     LoadString(hInstance, IDC_APIUP, szWindowClass, MAX_LOADSTRING); 
     MyRegisterClass(hInstance); 
     Register(hInstance); 

     // Perform application initialization: 
     if (!InitInstance (hInstance, nCmdShow)) 
     { 
      return FALSE; 
     } 

     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_APIUP)); 

     // Main message loop: 
     while (GetMessage(&msg, NULL, 0, 0)) 
     { 
      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
      { 
       TranslateMessage(&msg); 
       DispatchMessage(&msg); 
      } 
     } 

     return (int) msg.wParam; 
     } 





     ATOM MyRegisterClass(HINSTANCE hInstance) 
     { 
      WNDCLASSEX wcex; 

      wcex.cbSize = sizeof(WNDCLASSEX); 

      wcex.style   = CS_HREDRAW | CS_VREDRAW; 
      wcex.lpfnWndProc = WndProc; 
      wcex.cbClsExtra  = 0; 
      wcex.cbWndExtra  = 0; 
      wcex.hInstance  = hInstance; 
      wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APIUP)); 
      wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
      wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
      wcex.lpszMenuName = MAKEINTRESOURCE(IDC_APIUP); 
      wcex.lpszClassName = szWindowClass; 
      wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 


      return RegisterClassEx(&wcex); 
       } 

       ATOM Register(HINSTANCE hInstance) 
       { 
      WNDCLASSEX cw13class; 

      cw13class.cbSize = sizeof(WNDCLASSEX); 

      cw13class.style   = CS_HREDRAW | CS_VREDRAW; 
      cw13class.lpfnWndProc = WndProc_dla13; 
      cw13class.cbClsExtra  = 0; 
      cw13class.cbWndExtra  = 0; 
      cw13class.hInstance  = hInstance; 
      cw13class.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APIUP)); 
      cw13class.hCursor  = LoadCursor(NULL, IDC_ARROW); 
      cw13class.hbrBackground = GetSysColorBrush(COLOR_3DFACE); 
      cw13class.lpszMenuName = NULL; 
      cw13class.lpszClassName = L"13class"; 
      cw13class.hIconSm  = LoadIcon(cw13class.hInstance,        MAKEINTRESOURCE(IDI_SMALL)); 
      return RegisterClassEx(&cw13class); 

     } 

     BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
     { 




      hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 
       CW_USEDEFAULT, 0, 100, 100, NULL, NULL, hInstance, NULL); 
      cw13=CreateWindow(L"13class",L"Ćwiczenie       13",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,300,300,NULL,NULL,hInst,NULL); 


      if (!hWnd) 
      { 
       return FALSE; 
      } 

      ShowWindow(hWnd, nCmdShow); 
      UpdateWindow(hWnd); 

      return TRUE; 
     } 


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 

    switch (message) 
    { 
    case WM_COMMAND: 
     wmId = LOWORD(wParam); 
     wmEvent = HIWORD(wParam); 
     // Parse the menu selections: 

     switch (wmId) 
     { 
     case IDM_ABOUT: 
      DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
      break; 
     case IDM_EXIT: 
      DestroyWindow(hWnd); 
      break; 
     case ID_32771: 
      ShowWindow(cw13, 1); 
      break; 

     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
     break; 
    case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     // TODO: Add any drawing code here... 
     EndPaint(hWnd, &ps); 
     break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

LRESULT CALLBACK WndProc_dla13 (HWND hWnd, UINT msg/*message*/, WPARAM wParam,LPARAM lParam) 
     { 
switch(msg) 
    { 
    case WM_CREATE: 

     CreateWindowW(L"button", L"Beep", 
      WS_VISIBLE | WS_CHILD , 
      20, 50, 80, 25, 
      cw13, (HMENU) 1, hInst, NULL); 

     CreateWindowW(L"button", L"Quit", 
      WS_VISIBLE | WS_CHILD , 
      120, 50, 80, 25, 
      cw13, (HMENU) 2, NULL, NULL); 
     break; 

    case WM_COMMAND: 

     if (LOWORD(wParam) == 1) { 
     Beep(40, 50); 
     } 

     if (LOWORD(wParam) == 2) { 
     PostQuitMessage(0); 
     } 

     break; 

    case WM_DESTROY: 

     PostQuitMessage(0); 
     break; 
     default: 
     return DefWindowProc(hWnd, msg, wParam, lParam); 
    } 
return 0; 
} 




// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    UNREFERENCED_PARAMETER(lParam); 
    switch (message) 
    { 
    case WM_INITDIALOG: 
     return (INT_PTR)TRUE; 

    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
     { 
      EndDialog(hDlg, LOWORD(wParam)); 
      return (INT_PTR)TRUE; 
     } 
     break; 
    } 
    return (INT_PTR)FALSE; 
} 

而問題在於:當我創建窗口「cw13」它被創建和顯示時,但推按鈕not.I創建兩個單獨的WndProc功能。其他消息正確執行(如WM_QUIT),只有CreateWindowW根據WM_CREATE,女巫應創建並顯示按鈕不是。哪裏有問題?

回答

1

處理WM_CREATE消息時,您對CreateWindow(L"13class")的呼叫仍在運行。您的cw13變量尚未分配,因此您將分配一個無效的HWND作爲按鈕的父項。這就是他們沒有出現的原因。使用WndProc_dla13()hwnd參數代替父項。這將是相同的HWND,一旦CreateWindow(L"13class")退出,它將被分配到cw13變量。