Загрузка...

It is necessary to modify from the source, accordingly, payment (c ++, WinApi)

Thread in C/C++ created by Wertuyska Sep 27, 2021. 112 views

  1. Wertuyska
    Wertuyska Topic starter Sep 27, 2021 0 Sep 26, 2021
    Учесть ситуацию, что когда пользователь дойдет до конечно числа, но при этом пользователь нажимает, что число не то. Тогда программа выдает сообщение об этом
    Code
    #include <windows.h>
    #include <tchar.h>
    #define APPNAME _T("LabWork1")
    #define APPCLS _T("Nuklon")
    #define IDC_SMALL 100
    #define IDC_BIG 101
    #define IDC_ANSWER 102
    #define _creatButton(name, id, x, y) \
    CreateWindow(_T("BUTTON"), name, WS_CHILD | BS_USERBUTTON | WS_VISIBLE,\
    x, y, 200, 60, hwnd, (HMENU)id, (HINSTANCE)lParam, NULL)
    //////////////////////////////////////////////////////////
    LRESULT CALLBACK _wndProc(HWND, UINT, WPARAM, LPARAM);
    int _creatWindow(HINSTANCE, const TCHAR*);
    void _helloDlg(HWND, int, int);
    void _answerCPU(HWND, int, int, char);
    /// /////////////////////////////////////////////////////
    int f_n = 1;
    int l_n = 1000;
    //////////////////////////////////////////////////////////
    int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int) { return _creatWindow(hInstance, _T("LabWork1. Less or more?")); }
    ///////////////////////ОКНО//////////////////////
    int _creatWindow(HINSTANCE hInst, const TCHAR* caption) {
    HWND hwnd = NULL;
    MSG msg = { 0 };
    WNDCLASS ofs = {
    0, _wndProc, 0, 0, hInst, 0, LoadCursor(NULL, IDC_ARROW),
    (HBRUSH)(COLOR_WINDOW + 2), 0, APPCLS };
    if (!RegisterClass(&ofs))
    return 1;
    hwnd = CreateWindow(APPCLS, caption, WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_DESKTOP,
    NULL, hInst, hInst);
    if (hwnd == NULL) {
    UnregisterClass(APPCLS, hInst);
    return 1;
    }
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }
    ////////////////////////////////////////////////////////
    LRESULT CALLBACK _wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static HWND bbig = NULL;
    static HWND bsmall = NULL;
    RECT rc;
    switch (msg) {
    case WM_CREATE:
    GetClientRect(hwnd, &rc);
    bsmall = _creatButton(_T("МЕНЬШЕ"), IDC_SMALL, rc.right / 10 + 350, rc.bottom / 2 + 30);
    bbig = _creatButton(_T("БОЛЬШЕ"), IDC_BIG, rc.right / 10 + 350, rc.bottom / 2 - 40);
    SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_ANSWER, 0), 0);
    break;
    case WM_COMMAND:
    switch (LOWORD(wParam)) {
    case IDC_SMALL:
    _answerCPU(hwnd, f_n, l_n, '<');
    break;
    case IDC_BIG:
    _answerCPU(hwnd, f_n, l_n, '>');
    break;
    case IDC_ANSWER:
    _helloDlg(hwnd, f_n, l_n);
    _answerCPU(hwnd, f_n, l_n, 0);
    break;
    }
    break;
    case WM_DESTROY:
    DestroyWindow(bsmall);
    DestroyWindow(bbig);
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    }
    ////////////////////НАЧАЛО/////////////////////////////////
    void _helloDlg(HWND hwnd, int f, int l) {
    TCHAR buf[64];
    wsprintf(buf, _T("Для начала игры задумайте число от %d до %d"), f, l);
    MessageBox(hwnd, buf, APPNAME, MB_OK | MB_ICONINFORMATION);
    }
    ///////////////////////////////////////////////////////
    void _answerCPU(HWND hwnd, int _f, int _l, char ch) {
    static int f = _f;
    static int l = _l;
    static int num = 0;
    TCHAR buf[64];
    int id;

    switch (ch) {
    case '<': // МЕНЬШЕ
    l = num;
    break;
    case '>': // БОЛЬШЕ
    f = num + 1;
    break;
    }
    num = (f + l) / 2;

    wsprintf(buf, _T("Загаданным числом является %d. Да/Нет?"), num);
    id = MessageBox(hwnd, buf, APPNAME, MB_YESNO | MB_ICONQUESTION);
    if (id != IDYES)
    return;


    wsprintf(buf, _T("Получилось! Ваше число %d"), num);

    id = MessageBox(hwnd, buf, APPNAME, MB_OKCANCEL | MB_ICONINFORMATION);
    if (id == IDCANCEL)
    SendMessage(hwnd, WM_DESTROY, 0, 0);
    else {
    f = _f;
    l = _l;
    SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_ANSWER, 0), 0);
    }
    if (f = l = _f = _l) { wsprintf(buf, _T("Получилось!")); };
    }
     
Top
Loading...