C++ WIN32多窗口

C++ WIN32多窗口,c++,winapi,C++,Winapi,我试图显示3个窗口,但它无法创建窗口2,我不知道为什么。它创建了第一个窗口,我对其他两个窗口也做了同样的操作 代码如下: #include <Windows.h> // Store handles to the main window and application instance globally. HWND ghMainWnd = 0; HWND ghSecdWnd = 0; HWND ghThrdWnd = 0; HINSTANCE ghApp

我试图显示3个窗口,但它无法创建窗口2,我不知道为什么。它创建了第一个窗口,我对其他两个窗口也做了同样的操作

代码如下:

#include <Windows.h>

// Store handles to the main window and application instance globally.
HWND      ghMainWnd = 0;
HWND      ghSecdWnd = 0;
HWND      ghThrdWnd = 0;
HINSTANCE ghAppInst = 0;

//========================================================================================
// WINDOW 1
// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch( msg )
    {
    // Handle left mouse button click message.
    case WM_LBUTTONDOWN:
        MessageBox(hWnd, "Left Mouse Button Click", "Message", MB_OK);
        return 0;

    // Handle key down message.
    case WM_KEYDOWN:
        if( wParam == VK_ESCAPE )
            if( MessageBox(hWnd, "Are you sure?", "Quit", MB_YESNO) == IDYES )
                DestroyWindow(ghMainWnd);
        return 0;

    // Handle destroy window message.
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    // Forward any other messages we didn't handle to the default window procedure.
    return DefWindowProc(hWnd, msg, wParam, lParam);

}
//========================================================================================
// WINDOW 2
//========================================================================================
LRESULT CALLBACK
WndProc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch( msg )
    {
    // Handle down arrow pressed.
    case WM_KEYDOWN:
        if( wParam == VK_DOWN )
            MessageBox(hWnd, "Down arrow pressed 2.", "Information", MB_OK);
        return 0;
    case WM_LBUTTONDOWN:
        MessageBox(hWnd, "Window 2", "Window 2", MB_OK);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}
//========================================================================================
// WINDOW 3
//========================================================================================
LRESULT CALLBACK
WndProc3(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch( msg )
    {
    // Handle down arrow pressed.
    case WM_KEYDOWN:
        if( wParam == VK_DOWN )
            MessageBox(hWnd, "Down arrow pressed 3.", "Information", MB_OK);
        return 0;
    case WM_LBUTTONDOWN:
        MessageBox(hWnd, "Window 3", "Window 3", MB_OK);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

// WinMain: Entry point for windows application.
int WINAPI
    WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{

    // Save handle to application instance.
    ghAppInst = hInstance;

    // Step 2: Fill out a WNDCLASS instance.
    WNDCLASS wc;
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = ghAppInst;
    wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
    wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = 0;
    wc.lpszClassName = "MyWndClassName";

    // Window 2
    WNDCLASS wc2;
    wc2.style         = CS_HREDRAW | CS_VREDRAW;
    wc2.lpfnWndProc   = WndProc2;
    wc2.cbClsExtra    = 0;
    wc2.cbWndExtra    = 0;
    wc2.hInstance     = ghAppInst;
    wc.hIcon          = ::LoadIcon(0, IDI_APPLICATION);
    wc2.hCursor       = ::LoadCursor(0, IDC_ARROW);
    wc2.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    wc2.lpszMenuName  = 0;
    wc2.lpszClassName = "MyWndClassTwo";

    // Window 3
    WNDCLASS wc3;
    wc3.style         = CS_HREDRAW | CS_VREDRAW;
    wc3.lpfnWndProc   = WndProc3;
    wc3.cbClsExtra    = 0;
    wc3.cbWndExtra    = 0;
    wc3.hInstance     = ghAppInst;
    wc3.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
    wc3.hCursor       = ::LoadCursor(0, IDC_ARROW);
    wc3.lpszMenuName  = 0;
    wc3.lpszClassName = "MyWndClassThree";


    // Step 3: Register with WNDCLASS instance with windows.
    RegisterClass( &wc );
    RegisterClass( &wc2 );
    RegisterClass( &wc3 );

    // Step 4: Create the window, and save the handle in global window handle variable ghMainWnd.
    ghMainWnd = ::CreateWindow("MyWndClassName",  "Window 1", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0,   0,   500, 500, 0, 0, ghAppInst, 0);
    ghSecdWnd = ::CreateWindow("MyWndClassTwo",   "Window 2", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 510, 0,   500, 500, 0, 0, ghAppInst, 0);
    ghThrdWnd = ::CreateWindow("MyWndClassThree", "Window 3", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0,   510, 500, 500, 0, 0, ghAppInst, 0);

    if( ghMainWnd == 0 )
    {
        ::MessageBox(0, "Create Window 1 - Failed", 0, 0);
        return false;
    }

    if( ghSecdWnd == 0 )
    {
        ::MessageBox(0, "Create Window 2 - Failed", 0, 0);
        return false;
    }

    if( ghThrdWnd == 0 )
    {
        ::MessageBox(0, "Create Window 3 - Failed", 0, 0);
        return false;
    }

    // Step 5: Show and update the window.
    ShowWindow(ghMainWnd, showCmd);
    UpdateWindow(ghMainWnd);

    ShowWindow(ghSecdWnd, showCmd);
    UpdateWindow(ghSecdWnd);

    ShowWindow(ghThrdWnd, showCmd);
    UpdateWindow(ghThrdWnd);

    // Step 6: Enter the message loop and don't quit until a WM_QUIT message is received.
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while( GetMessage(&msg, 0, 0, 0) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Return exit code back to operating system.
    return (int)msg.wParam;

}
#包括
//全局存储主窗口和应用程序实例的句柄。
HWND-ghMainWnd=0;
HWND-ghSecdWnd=0;
HWND ghthdwnd=0;
HINSTANCE Ghappist=0;
//========================================================================================
//窗口1
//步骤1:定义并实现窗口过程。
LRESULT回调
WndProc(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM)
{
开关(msg)
{
//处理鼠标左键单击消息。
案例WM_LBUTTONDOWN:
消息框(hWnd,“鼠标左键单击”,“消息”,MB_确定);
返回0;
//处理键关闭消息。
案例WM_键控:
if(wParam==VK_转义)
if(MessageBox(hWnd,“确定吗?”,“退出”,MB_YESNO)=idies)
破坏窗口(ghMainWnd);
返回0;
//处理销毁窗口消息。
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
//将我们未处理的任何其他消息转发到默认窗口过程。
返回DefWindowProc(hWnd、msg、wParam、lParam);
}
//========================================================================================
//窗口2
//========================================================================================
LRESULT回调
WNDPOC2(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM)
{
开关(msg)
{
//按下手柄向下箭头。
案例WM_键控:
如果(wParam==VK_DOWN)
消息框(hWnd,“按下向下箭头2.”,“信息”,MB_OK);
返回0;
案例WM_LBUTTONDOWN:
消息框(hWnd,“窗口2”,“窗口2”,MB_OK);
返回0;
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hWnd、msg、wParam、lParam);
}
//========================================================================================
//窗口3
//========================================================================================
LRESULT回调
WNDPOC3(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM)
{
开关(msg)
{
//按下手柄向下箭头。
案例WM_键控:
如果(wParam==VK_DOWN)
消息框(hWnd,“按下向下箭头3.”,“信息”,MB_OK);
返回0;
案例WM_LBUTTONDOWN:
消息框(hWnd,“窗口3”,“窗口3”,MB_OK);
返回0;
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hWnd、msg、wParam、lParam);
}
//WinMain:windows应用程序的入口点。
int WINAPI
WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、PSTR cmdLine、int showCmd)
{
//将句柄保存到应用程序实例。
Ghappist=hInstance;
//步骤2:填写WNDCLASS实例。
WNDCLASS wc;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=Ghappist;
wc.hIcon=::加载图标(0,IDI_应用程序);
wc.hCursor=::加载光标(0,IDC_箭头);
wc.hbrBackground=(HBRUSH)::GetStockObject(白色画笔);
wc.lpszMenuName=0;
wc.lpszClassName=“MyWndClassName”;
//窗口2
WNDCLASS wc2;
wc2.style=CS_HREDRAW | CS_VREDRAW;
wc2.lpfnWndProc=WndProc2;
wc2.cbClsExtra=0;
wc2.cbWndExtra=0;
wc2.hInstance=Ghappist;
wc.hIcon=::加载图标(0,IDI_应用程序);
wc2.hCursor=::加载光标(0,IDC_箭头);
wc2.hbrBackground=(HBRUSH)::GetStockObject(白色画笔);
wc2.lpszMenuName=0;
wc2.lpszClassName=“MyWndClassTwo”;
//窗口3
WNDCLASS wc3;
wc3.style=CS_HREDRAW | CS_VREDRAW;
wc3.lpfnWndProc=WndProc3;
wc3.cbClsExtra=0;
wc3.cbWndExtra=0;
wc3.hInstance=Ghappist;
wc3.hIcon=::加载图标(0,IDI_应用程序);
wc3.hCursor=::加载光标(0,IDC_箭头);
wc3.lpszMenuName=0;
wc3.lpszClassName=“MyWndClassThree”;
//步骤3:向windows注册WNDCLASS实例。
注册类(&wc);
注册类(&wc2);
注册类(&wc3);
//步骤4:创建窗口,并将句柄保存在全局窗口句柄变量ghMainWnd中。
ghMainWnd=::CreateWindow(“MyWndClassName”,“Window 1”,WS|u OVERLAPPEDWINDOW | WS|HSCROLL | WS|u VSCROLL,0,0500500,0,0,Ghappist,0);
ghSecdWnd=::CreateWindow(“MyWndClassTwo”,“Window 2”,WS|u重叠窗口| WS|HSCROLL | WS|u VSCROLL,510,0500500,0,0,Ghappist,0);
ghThrdWnd=::CreateWindow(“MyWndClassThree”,“Window 3”,WS|u重叠窗口| WS|u HSCROLL | WS|u VSCROLL,0,510,500,500,0,0,Ghappins,0);
如果(ghMainWnd==0)
{
::消息框(0,“创建窗口1-失败”,0,0);
返回false;
}
如果(ghSecdWnd==0)
{
::消息框(0,“创建窗口2-失败”,0,0);
返回false;
}
如果(ghThrdWnd==0)
{
::消息框(0,“创建窗口3-失败”,0,0);
返回false;
}
//步骤5:显示并更新窗口。
ShowWindow(ghMainWnd、showCmd);
更新域(ghMainWnd);
显示窗口(ghSecdWnd、showCmd);
更新窗口(ghSecdWnd);
显示窗口(ghThrdWnd、showCmd);
更新域(ghThrdWnd);
//步骤6:进入消息循环,直到收到WM_quit消息后才退出。
味精;
零内存(&msg,sizeof(msg));
while(GetMessage(&msg,0,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
//将退出代码返回到操作系统。
返回(int)msg.wParam;
}

它不会抛出任何错误,但在ghSecdWnd==0上,它会显示该错误消息。

有两个问题:一个是您遇到的问题,另一个是
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc3.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
if (!RegisterClass(&wc2)) { //returns 0 if failed
    std::cout << "Failed to register class: ";
    std::cout << GetLastError() << '\n'; //or however you want to show it
}

if (!(ghSecdWnd = CreateWindow( /*...*/)) { //or ghSecdWnd = ...; if (!ghSecdWnd)
    std::cout << "Failed to create window: ";
    std::cout << GetLastError() << '\n'; //look at FormatMessage for a message
}