如何在回调过程中创建第二个窗口? Win32的

时间:2013-02-17 17:22:47

标签: c++ winapi user-interface

如果我的第一个窗口中的对话框以某种方式返回,我正在尝试创建第二个窗口。不幸的是,如果我从第一个窗口的回调过程中调用CreateWindowEx(),它就无法识别第二个窗口的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。

0 个答案:

没有答案