从主窗口重新打开窗口。

时间:2016-02-08 21:01:08

标签: c++ winapi

我打开了主窗口。它是使用visual studio为我创建的。我试图做的是有一个主屏幕,有9到12个按钮。然后这些按钮将打开其他窗口。我正在尝试使用一个按钮,当前是" InventoryBtn"。库存窗口打开,我可以关闭它。我第二次尝试打开窗口时遇到错误。似乎窗口已经注册,无法重新创建。我做错了什么?我踏上了它,然后去了#34; Destroy:"被称为。我第二次尝试显示库存窗口时手柄未初始化。

#include "stdafx.h"
#include "VehManager.h"
#include "M_Inventory.h"  
#define MAX_LOADSTRING  100
#define InvWindowBtn 101 
#define InvBtn 103 
using namespace boost::gregorian; 
HINSTANCE hInst; 
WCHAR szTitle[MAX_LOADSTRING];  
WCHAR szWindowClass[MAX_LOADSTRING];  
ATOM                MyRegisterClass(HINSTANCE hInstance); 
BOOL                InitInstance(HINSTANCE, int); 
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK    InvWindowProcess(HWND, UINT, WPARAM, LPARAM);  
HWND handleforInvWin; 
WNDCLASSEX wxInv;  
void CreateInventoryWindow(); 


int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);  
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_VEHMANAGER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE    (IDC_VEHMANAGER));

    MSG msg; 

    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW 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_VEHMANAGER));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_VEHMANAGER);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance;  

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); 

   if (!hWnd)
   {
        MessageBoxA(NULL, "Main window creation failed.", "904.VehicleManager", MB_OK | MB_ICONINFORMATION);
        return FALSE;
   }

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

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HFONT hfDefault;
    switch (message)
    {
        case WM_CREATE:
        {  
            HWND hInvBtn = CreateWindowEx(NULL, L"Button", L"INVENTORY", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                50, 220, 100, 24, hWnd, (HMENU)InvBtn, GetModuleHandle(NULL), NULL);
            if (hInvBtn == NULL)
                MessageBoxA(hWnd, "Could not create inventory button", "904.VehcleManager", NULL);
            hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
            SendMessage(hInvBtn, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)); 
        }
        case WM_COMMAND:
            {
                int wmId = LOWORD(wParam); 
                switch (wmId)
                {
                case InvBtn:
                { 
                    CreateInventoryWindow();
                    break;
                }

                case IDM_ABOUT:
                    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                    break;
                case IDM_EXIT:
                    DestroyWindow(hWnd);
                    break;
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
                }
            }
            break;
        case WM_RBUTTONDOWN:
            MessageBox(NULL, L"Right button mouse clicks not allowed.", L"904.VehicleManager", NULL);
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hWnd, &ps); 
                EndPaint(hWnd, &ps);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, 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;
}

void CreateInventoryWindow()
{ 
    ZeroMemory(&wxInv, sizeof(WNDCLASSEX));
    wxInv.cbClsExtra = NULL;
    wxInv.cbSize = sizeof(WNDCLASSEX);
    wxInv.cbWndExtra = NULL;
    wxInv.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wxInv.hCursor = LoadCursor(NULL, IDC_ARROW);
    wxInv.hIcon = NULL;
    wxInv.hIconSm = NULL;
    wxInv.hInstance = hInst;
    wxInv.lpfnWndProc = (WNDPROC)InvWindowProcess;
    wxInv.lpszClassName = L"Inventory";
    wxInv.lpszMenuName = NULL;
    wxInv.style = CS_HREDRAW | CS_VREDRAW;
    if (!RegisterClassEx(&wxInv))
    {
        int nResult = GetLastError();
        MessageBox(NULL, L"Inventory window class registration failed.", L"904.VehicleManager", MB_ICONEXCLAMATION);
        return;
    } 
    handleforInvWin = CreateWindowEx(NULL, wxInv.lpszClassName, L"Open Window 2", WS_OVERLAPPEDWINDOW,
        200, 170, 640, 480, NULL, NULL, hInst, NULL); 
    if (!handleforInvWin)
    {
        MessageBox(NULL, L"Error with the inventory window handle.", L"904.VehManager", MB_ICONEXCLAMATION);
        return;
    }
    ShowWindow(handleforInvWin, SW_SHOWNOACTIVATE);
    UpdateWindow(handleforInvWin);
}
LRESULT CALLBACK InvWindowProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    HFONT hfDefault;
    switch (message)
    {
    case WM_CREATE:
    {
        HWND hInvBtn = CreateWindowEx(NULL, L"Button", L"Close", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            50, 220, 100, 24, hwnd, (HMENU)InvBtn, GetModuleHandle(NULL), NULL);
        if (hInvBtn == NULL)
            MessageBoxA(hwnd, "Could not create inventory button", "904.VehcleManager", NULL);
        hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
        SendMessage(hInvBtn, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
        break;
    }
    case WM_DESTROY:
    { 
        break;
    }
    case WM_COMMAND:
        case InvBtn:
        { 
            DestroyWindow(hwnd); 
            break;
        }

        break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);

}

0 个答案:

没有答案