3

最初のウィンドウからのダイアログ ボックスが特定の方法で返される場合、2 番目のウィンドウを作成しようとしています。残念ながら、最初のウィンドウのコールバック プロセス内から CreateWindowEx() を呼び出すと、2 番目のウィンドウの HWND が認識されません。また、WinMain で最初のウィンドウを定義し、コールバック内で表示できるようにしましたが、HWND をもう一度認識できませんでした。私はこれを間違ってやろうとしていますか、それとも何かが足りないだけですか?

編集:

#include "stdafx.h"
#include "md5.h"
#include "Coursework.h"
#include "accounts.h"

const char g_szClassName[] = "myMainWindow";
const char g_szLoggedInClass[] = "loggedInWindow";
#define IDC_CREATE_ACCOUNT 101
#define IDC_LOG_IN 110
MYSQL *conn;
static std::string strUserName;

BOOL CALLBACK CreateDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
    case WM_INITDIALOG:
    return TRUE;
    case WM_COMMAND: 
        switch (LOWORD(wParam))
        {
            case IDOK:
                {
                CreateAccount(hwnd);
                }
                break;
            case IDCANCEL:
                EndDialog(hwnd, IDCANCEL);
            break;
        }
    default:
        return FALSE;
}
}
BOOL CALLBACK LogInDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
    case WM_INITDIALOG:
    return TRUE;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDCANCEL:
            EndDialog(hwnd, IDCANCEL);
        break;
        case IDOK:
            {
                if(bLogIn(hwnd, strUserName))
                {
                    EndDialog(hwnd, IDOK);

                }
            }
        break;
        }
        break;
    default:
        return FALSE;
}
}
LRESULT CALLBACK LoggedInProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_CLOSE:
        PostQuitMessage(0);
    break;
    case WM_CREATE:
        {
        if (*mysql_error(conn))                                                                         //Checks if the connection succeeded, and
        {                                                                                               //exits the program if false.
            MessageBox(hwnd, "No connection to the server could be established.\r\nPlease contact your administrator.", "Error", MB_ICONERROR);
            exit(-1);
        }
        HWND hCreateButton = CreateWindowEx(NULL, "Button", "Create account", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 500, 250, 120, 24, hwnd, (HMENU)IDC_CREATE_ACCOUNT, GetModuleHandle(NULL), NULL);
        HWND hLogInButton = CreateWindowEx(NULL, "Button", "Log in", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 500, 210, 120, 24, hwnd, (HMENU)IDC_LOG_IN, GetModuleHandle(NULL), NULL);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
    break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_CREATE_ACCOUNT:
            {
            int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, CreateDlgProc);
            if (ret == -1)
            {
                MessageBox(hwnd, "Dialog failed to appear", "Error", MB_OK | MB_ICONINFORMATION);
            }
            }
            break;
        case IDC_LOG_IN:
            {
            int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), hwnd, LogInDlgProc);
            if(ret == -1)
            {
                MessageBox(hwnd, "Dialog failed to appear", "Error", MB_OK | MB_ICONINFORMATION);
            }
            else if(ret == IDOK)
            {
                ShowWindow(hwnd, SW_HIDE);
            }
            }
        }
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int     nCmdShow)
{

WNDCLASSEX wc;
WNDCLASSEX wc2;
HWND hwnd;
HWND hLoggedIn;
MSG Msg;
conn = mysql_init(NULL);                                                                    //Initialises mySQL connection
mysql_real_connect(conn, "localhost", "root", "SecurityRules", "coursework", 0, NULL, 0);
//Registering the Window Class
wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = 0;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName  = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

//Registering the second Window Class
wc2.cbSize       = sizeof(WNDCLASSEX);
wc2.style         = 0;
wc2.lpfnWndProc   = LoggedInProc;
wc2.cbClsExtra    = 0;
wc2.cbWndExtra    = 0;
wc2.hInstance     = hInstance;
wc2.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc2.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc2.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc2.lpszMenuName  = NULL;
wc2.lpszClassName = g_szLoggedInClass;
wc2.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
    MessageBox(NULL, "Window Registration Failed!\r\nContact your administrator.", "Error!", MB_ICONEXCLAMATION | MB_OK);
    return 0;
}
if(!RegisterClassEx(&wc2))
{
    MessageBox(NULL, "Window Registration failed!\r\nContact your administrator.", "Error!", MB_ICONEXCLAMATION|MB_OK);
    return 0;
}
//Creating the main Window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Coursework", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1080, 640, NULL, NULL, hInstance, NULL);
hLoggedIn = CreateWindowEx(WS_EX_CLIENTEDGE, g_szLoggedInClass, "Coursework", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1080, 640, NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
    MessageBox(NULL, "Window Creation Failed!\r\nContact your adminstrator.", "Error!", MB_ICONEXCLAMATION | MB_OK);
    return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);


// The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
mysql_close(conn);
return Msg.wParam;
}
}

編集 2: Intellisense (私は Visual Studio を使用しています) は、「識別子 hLoggedIn は未定義です」と nCmdShow と述べています。

4

0 に答える 0