मैं Win32 API सीख रहा हूं। मेरी मुख्य/पहली विंडो में मैं एक खुली खिड़की 2 बटन रखना चाहता हूं जो फिर एक नई विंडो खोलता है। तो विचार यह है कि, जब बटन दबाया जाता है तो विंडो 2 खोलने के लिए एक फ़ंक्शन कहा जाता है। फिर भी मुझे यकीन नहीं है कि इस फ़ंक्शन को यहां कैसे कॉल करें:

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
    {
        case WM_COMMAND:
        {
            switch(wParam) {
            case OPEN_WINDOW2_BUTTON:
        {}                              // <------------------------- what do I put here?
            }
        }
        case WM_CREATE:{
            AddControls1(hwnd);
            break;
        }
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:

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

और यहाँ मेरा पूरा कोड है:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <iostream>
#include <tchar.h>
#include <windows.h>


#define OPEN_WINDOW2_BUTTON 1

using namespace std;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2,UINT message,WPARAM wParam,LPARAM lParam);

void AddControls1(HWND);
void createwindow2 (WNDCLASSEX wincl_2, HWND& hwnd, HINSTANCE hThisInstance, int nCmdShow);

HWND hMainWindow, hwnd, hHeader;

TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain(HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    bool endprogram=false;

    //create window 1

    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl_1;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl_1.hInstance = hThisInstance;
    wincl_1.lpszClassName = szClassName;
    wincl_1.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl_1.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl_1.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl_1.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl_1.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl_1.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl_1.lpszMenuName = NULL;                 /* No menu */
    wincl_1.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl_1.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl_1.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
 //----

    if (!RegisterClassEx (&wincl_1))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           1800,                 /* The programs width */
           920,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    /* Make the window visible on the screen */
    ShowWindow(hwnd,nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

void createwindow2 (WNDCLASSEX wincl_2, HWND& hwnd, HINSTANCE hThisInstance, int nCmdShow)
{
    if (!RegisterClassEx (&wincl_2))
    wincl_2.hInstance = hThisInstance;
    wincl_2.lpszClassName = szClassName;
    wincl_2.lpfnWndProc = (WNDPROC)windowprocessforwindow2;      /* This function is called by windows */
    wincl_2.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl_2.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl_2.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl_2.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl_2.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl_2.lpszMenuName = NULL;                 /* No menu */
    wincl_2.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl_2.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl_2.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    HWND handleforwindow2 = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           330,                 /* The programs width */
           320,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    /* Make the window visible on the screen */
    ShowWindow(handleforwindow2,nCmdShow);
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
    {
        case WM_COMMAND:
        {
            switch(wParam) {
            case OPEN_WINDOW2_BUTTON:
        {}                              // <------------------------- what do I put here?
            }
        }
        case WM_CREATE:{
            AddControls1(hwnd);
            break;
        }
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:

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

LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
         case WM_COMMAND:
        {
            switch(wParam) {

            }
        }
        case WM_CREATE:{
            AddControls1(hwnd);
            break;
        }
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:

    return DefWindowProc(handleforwindow2,msg,wParam,lParam);
    }
    return 0;
}

void AddControls1(HWND hwnd)
{
    CreateWindowW(L"Button",L" Open Window 2", WS_VISIBLE | WS_CHILD, 215,285,300,30,hwnd, (HMENU)OPEN_WINDOW2_BUTTON,NULL,NULL);
}


किसी भी तरह की सहायता का स्वागत किया जाएगा। धन्यवाद!

0
Janis22 24 जुलाई 2020, 16:20

1 उत्तर

सबसे बढ़िया उत्तर

सबसे पहले, आप GetWindowLong फ़ंक्शन।

फिर एक WNDCLASSEX संरचना करें और इसे अपने createwindow2 फ़ंक्शन में पास करें।

अंत में, इसे OPEN_WINDOW2_BUTTON संदेश (मूल createwindow2 फ़ंक्शन में hwnd पैरामीटर की आवश्यकता नहीं है) में कहा जाता है।

संशोधित कोड इस प्रकार है:

#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include <tchar.h>
#include <iostream>
#include <tchar.h>
#include <windows.h>


#define OPEN_WINDOW2_BUTTON 1

using namespace std;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2, UINT message, WPARAM wParam, LPARAM lParam);

void AddControls1(HWND);
void createwindow2(WNDCLASSEX& wincl_2, HINSTANCE& hThisInstance, int nCmdShow);
void AddControls2(HWND hwnd);
HWND hMainWindow, hwnd, hHeader;

TCHAR szClassName[] = _T("CodeBlocksWindowsApp");
TCHAR szClassName2[] = _T("CodeBlocksWindowsApp2");

int WINAPI WinMain(HINSTANCE hThisInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszArgument,
    int nCmdShow)
{
    bool endprogram = false;

    //create window 1

    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl_1;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl_1.hInstance = hThisInstance;
    wincl_1.lpszClassName = szClassName;
    wincl_1.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl_1.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl_1.cbSize = sizeof(WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl_1.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl_1.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl_1.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl_1.lpszMenuName = NULL;                 /* No menu */
    wincl_1.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl_1.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl_1.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    //----

    if (!RegisterClassEx(&wincl_1))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx(
        0,                   /* Extended possibilites for variation */
        szClassName,         /* Classname */
        _T("Code::Blocks Template Windows App"),       /* Title Text */
        WS_OVERLAPPEDWINDOW, /* default window */
        CW_USEDEFAULT,       /* Windows decides the position */
        CW_USEDEFAULT,       /* where the window ends up on the screen */
        1800,                 /* The programs width */
        920,                 /* and height in pixels */
        HWND_DESKTOP,        /* The window is a child-window to desktop */
        NULL,                /* No menu */
        hThisInstance,       /* Program Instance handler */
        NULL                 /* No Window Creation data */
    );
    /* Make the window visible on the screen */
    ShowWindow(hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage(&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

void createwindow2(WNDCLASSEX &wincl_2,HINSTANCE &hThisInstance, int nCmdShow)
{
    wincl_2.hInstance = hThisInstance;
    wincl_2.lpszClassName = szClassName2;
    wincl_2.lpfnWndProc = (WNDPROC)windowprocessforwindow2;      /* This function is called by windows */
    wincl_2.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl_2.cbSize = sizeof(WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl_2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl_2.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl_2.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl_2.lpszMenuName = NULL;                 /* No menu */
    wincl_2.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl_2.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl_2.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    if (!RegisterClassEx(&wincl_2))
        return;
    HWND handleforwindow2 = CreateWindowEx(
        0,                   /* Extended possibilites for variation */
        szClassName2,         /* Classname */
        _T("Code::Blocks Template Windows App"),       /* Title Text */
        WS_OVERLAPPEDWINDOW, /* default window */
        CW_USEDEFAULT,       /* Windows decides the position */
        CW_USEDEFAULT,       /* where the window ends up on the screen */
        330,                 /* The programs width */
        320,                 /* and height in pixels */
        HWND_DESKTOP,        /* The window is a child-window to desktop */
        NULL,                /* No menu */
        hThisInstance,       /* Program Instance handler */
        NULL                 /* No Window Creation data */
    );
    /* Make the window visible on the screen */
    ShowWindow(handleforwindow2, nCmdShow);
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        static HINSTANCE hInstance;
    case WM_COMMAND:
    {
        switch (wParam) {
        case OPEN_WINDOW2_BUTTON:
        {
            WNDCLASSEX w2;
            hInstance = (HINSTANCE)::GetWindowLong(hwnd, GWL_HINSTANCE);
            createwindow2(w2, hInstance,SW_SHOW);
        }                              // <------------------------- what do I put here?
        }
    }
    case WM_CREATE: {
        AddControls1(hwnd);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:

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

LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_COMMAND:
    {
        switch (wParam) {

        }
    }
    return 0;
    case WM_CREATE: {
        AddControls2(handleforwindow2);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:

        return DefWindowProc(handleforwindow2, msg, wParam, lParam);
    }
    return 0;
}

void AddControls1(HWND hwnd)
{
    CreateWindowW(L"Button", L" Open Window 2", WS_VISIBLE | WS_CHILD, 215, 285, 300, 30, hwnd, (HMENU)OPEN_WINDOW2_BUTTON, NULL, NULL);
}

void AddControls2(HWND hwnd)
{
    CreateWindowW(L"Button", L" A NEW WINDOW", WS_VISIBLE | WS_CHILD, 215, 285, 300, 30, hwnd, (HMENU)2, NULL, NULL);
}
0
Song Zhu - MSFT 28 जुलाई 2020, 10:34