繁体   English   中英

win32 滚动条在 C/C++ 程序中不起作用

[英]win32 scrollbar in not working in C/C++ program

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;
}

滚动条在这里不起作用。 我想把滚动条往下拉,这样就可以看到底部的rest了。 这段代码怎么写,滚动条会起作用吗?

如何垂直滚动

1.获取滚动单元。

滚动单位通常在处理WM_CREATE消息时设置。 例如,在您的情况下,您在编辑控件中显示文本,所以在这里我们可以将字符单元格的高度加上外部前导作为一个垂直滚动单元。 要检索特定 DC 的字体尺寸,请使用GetTextMetrics function。

2. 处理WM_SIZE消息

在处理WM_SIZE消息时,可以方便的调整滚动范围,滚动position来反映客户区的尺寸。

SetScrollInfo function 设置滚动条的最小和最大 position 值、页面大小和滚动 position。

3. 处理WM_VSCROLL消息。

当用户单击顶部箭头、底部箭头、滚动框上方的滚动条轴、滚动框下方的滚动条轴并拖动滚动框时,滚动条向 window 程序发送WM_VSCROLL消息。

当处理WM_VSCROLL消息时,检查滚动条请求代码并计算滚动增量。 After the increment is applied to the current scrolling position, the window is scrolled to the new position by using the ScrollWindowEx function, and the position of the scroll box is adjusted by using the SetScrollInfo function.

window 滚动后,部分客户区无效。 为确保更新无效区域, UpdateWindow function 用于生成WM_PAINT消息。

下面是滚动主window整个客户区的例子,大家可以参考。

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;
}

有关详细信息,请参阅“ 使用滚动条”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM