在CC++程序中,win32滚动条不能使用。

问题描述 投票:0回答:1

win32滚动条失效截图

Win32滚动条无法使用。这是我在win32 CC++中的代码

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

这里的滚动条不起作用。我想把滚动条往下拉,这样就可以看到底部的其他部分。这个代码怎么写,滚动条能用吗?

c++ c .net winapi
1个回答
0
投票

如何垂直滚动

1. 获取滚动单位。

滚动单位一般是在处理的时候设置的。WM_CREATE 消息。例如,在你的例子中,你在编辑控件中显示文本,所以这里我们可以把一个字符单元的高度,加上外部前导作为一个垂直滚动单位。要检索一个特定DC的字体尺寸,使用 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;
}

参考"使用滚动条"了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.