Полоса прокрутки win32 не работает в программе C / C ++ - PullRequest
0 голосов
/ 25 мая 2020

полоса прокрутки Win32 на неработающем снимке экрана

Полоса прокрутки Win32 не работает. Это мой код в win32 C / C ++

#include <windows.h>
#include <stdio.h>


/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND text, text2, button, mm;
HMENU hmenu;
void AddMenus(HWND);
char textsave[20];



/*  Make the class name into a global variable  */
char szClassName[ ] ="CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
HWND hwnd;               /* This is the handle for our window */
MSG messages;            /* Here messages to the application are saved */
WNDCLASSEX wincl;        /* Data structure for the windowclass */

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

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

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
    return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
       0,                   /* Extended possibilites for variation */
       szClassName,         /* Classname */
       "",       /* Title Text */
       WS_OVERLAPPEDWINDOW | WS_VSCROLL, /* default window */
       CW_USEDEFAULT,       /* Windows decides the position */
       CW_USEDEFAULT,       /* where the window ends up on the screen */
       544,                 /* The programs width */
       375,                 /* 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;
}



/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)                  /* handle the messages */
{
    case WM_CREATE:

        CreateWindow("EDIT", "jdslfkjssf\r\n\r\n\r\nflkjsdf\r\nflskdjfkl",
                            WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
                            20, 70, 490, 130, hwnd, NULL, NULL, NULL);

         CreateWindow("EDIT", "jdslfkjssf\r\n\r\n\r\nflkjsdf\r\nflskdjfkl",
                            WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
                            20, 270, 490, 230, hwnd, NULL, NULL, NULL);



        break;

    case WM_COMMAND:

        break;


    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
}



return 0;
}

Полоса прокрутки здесь не работает. Я хочу, чтобы полоса прокрутки была опущена, чтобы была видна остальная часть дна. Как написать этот код, будет ли работать полоса прокрутки?

1 Ответ

0 голосов
/ 27 мая 2020

Как прокручивать по вертикали

1. Получить единицу прокрутки.

Единицы прокрутки обычно устанавливаются при обработке сообщения WM_CREATE. Например, в вашем случае вы отображаете текст в элементе управления редактированием, поэтому здесь мы можем взять высоту символьной ячейки плюс внешний интерлиньяж как одну единицу вертикальной прокрутки. Чтобы получить размеры шрифта для указанного c D C, используйте функцию GetTextMetrics.

2. Обработка сообщения WM_SIZE

При обработке сообщения WM_SIZE удобно настроить диапазон прокрутки и положение прокрутки, чтобы отразить размеры клиентской области.

Функция SetScrollInfo устанавливает минимальные и максимальные значения положения, размер страницы и положение прокрутки полосы прокрутки.

3. Обработка сообщений WM_VSCROLL.

Когда пользователь щелкает верхнюю стрелку, нижнюю стрелку, вал полосы прокрутки над полем прокрутки, вал полосы прокрутки под полем прокрутки и перетаскивает ползунок, полоса прокрутки отправляет WM_VSCROLL сообщений оконной процедуре.

Когда сообщение WM_VSCROLL обрабатывается, проверяется код запроса полосы прокрутки и вычисляется приращение прокрутки. После применения приращения к текущей позиции прокрутки окно прокручивается до новой позиции с помощью функции ScrollWindowEx, а положение поля прокрутки регулируется с помощью функции SetScrollInfo.

После прокрутки окна часть его клиентской области становится недействительной. Чтобы гарантировать обновление недопустимой области, функция UpdateWindow используется для генерации сообщения WM_PAINT.

Ниже приведен пример прокрутки всей клиентской области главного окна, к которому вы можете обратиться.

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    TEXTMETRIC tm;
    SCROLLINFO si;

    // These variables are required to display text. 
    static int xClient;     // width of client area 
    static int yClient;     // height of client area 
    static int yChar; // vertical scrolling unit
    static int yPos;  // current vertical scrolling position

    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:
    {
        editCtl1 = CreateWindow("EDIT", "jdslfkjssf\r\n\r\n\r\nflkjsdf\r\nflskdjfkl",
            WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
            20, 70, 490, 130, hwnd, NULL, NULL, NULL);

        editCtl2 = CreateWindow("EDIT", "jdslfkjssf\r\n\r\n\r\nflkjsdf\r\nflskdjfkl",
            WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
            20, 270, 490, 230, hwnd, NULL, NULL, NULL);

        // Get the handle to the client area's device context. 
        hdc = GetDC(hwnd);

        // Extract font dimensions from the text metrics. 
        GetTextMetrics(hdc, &tm);
        yChar = tm.tmHeight + tm.tmExternalLeading;

        // Free the device context. 
        ReleaseDC(hwnd, hdc);

        break;
    }

    case WM_SIZE:
    {
        // Retrieve the dimensions of the client area. 
        yClient = HIWORD(lParam);
        xClient = LOWORD(lParam);

        // Get y-coordinate (bottom) of the second edit control 
        RECT editCtl2Rect = { 0 };
        GetWindowRect(editCtl2, &editCtl2Rect);
        POINT point = { 0 };
        point.x = editCtl2Rect.right;
        point.y = editCtl2Rect.bottom;

        // Convert screen coordinate to parent client-area coordinates
        ScreenToClient(hwnd, &point);

        // Set the vertical scrolling range and page size
        si.cbSize = sizeof(si);
        si.fMask = SIF_RANGE | SIF_PAGE;
        si.nMin = 0;
        si.nMax = point.y / yChar;
        si.nPage = yClient / yChar;
        SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
        break;
    }

    case WM_VSCROLL:
    {
        // Get all the vertial scroll bar information.
        si.cbSize = sizeof(si);
        si.fMask = SIF_ALL;
        GetScrollInfo(hwnd, SB_VERT, &si);

        // Save the position for comparison later on.
        yPos = si.nPos;
        switch (LOWORD(wParam))
        {
            // User clicked the top arrow.
        case SB_LINEUP:
            si.nPos -= 1;
            break;

            // User clicked the bottom arrow.
        case SB_LINEDOWN:
            si.nPos += 1;
            break;

            // User clicked the scroll bar shaft above the scroll box.
        case SB_PAGEUP:
            si.nPos -= si.nPage;
            break;

            // User clicked the scroll bar shaft below the scroll box.
        case SB_PAGEDOWN:
            si.nPos += si.nPage;
            break;

            // User dragged the scroll box.
        case SB_THUMBTRACK:
            si.nPos = si.nTrackPos;
            break;

        default:
            break;
        }

        // Set the position and then retrieve it.  Due to adjustments
        // by Windows it may not be the same as the value set.
        si.fMask = SIF_POS;
        SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
        GetScrollInfo(hwnd, SB_VERT, &si);

        // If the position has changed, scroll window and update it.
        if (si.nPos != yPos)
        {
            ScrollWindow(hwnd, 0, yChar * (yPos - si.nPos), NULL, NULL);
            UpdateWindow(hwnd);
        }

        return 0;
    }

    case WM_DESTROY:
        PostQuitMessage(0);       
        break;
    default:                      
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0;
}

См. « Использование полос прокрутки » для получения более подробной информации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...