当被另一个控件覆盖时,如何防止编辑控件出现?

How to keep edit control from showing up when covered by another control?

我创建了一个编辑框和一个列表框。当它们重叠时,列表框在顶部,鼠标光标在列表框覆盖部分编辑框的部分上方时变为工字梁,并出现编辑框的轮廓:

鼠标悬停之前:

鼠标悬停后:

用于创建控件的代码:

    HWND hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "xxx", WS_CHILD | WS_VISIBLE| ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
        50, 100, 200, 100, hWnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

    HWND hList = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T(""), WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
        70, 150, 200, 100, hWnd, (HMENU)ID_LIST_VIEW, GetModuleHandle(NULL), NULL);

我尝试使用 SetWindowPos 调整 x 顺序,但它不会改变行为。无论如何,我认为创建控制顺序应该将列表框放在编辑控件之上。

为什么要这样做,更重要的是,我该如何修复它以覆盖编辑内容?

要重现,请在VS2015中创建一个win32项目。将以上两行连同适当的包含定义等一起添加到 WM_CREATE。就差不多了。下面的代码没有向 listview.

添加任何项目

更新:根据大众需求,这里是完整的代码:

#include "stdafx.h"
#include "Win32Project3.h"
#include <commctrl.h>

#define MAX_LOADSTRING 100
#define IDC_MAIN_EDIT   140
#define ID_LIST_VIEW 143

#pragma comment( lib, "comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                 _In_opt_ HINSTANCE hPrevInstance,
                 _In_ LPWSTR    lpCmdLine,
                 _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WIN32PROJECT3, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_WIN32PROJECT3));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT3));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WIN32PROJECT3);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable
   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        HWND hList = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTBOX, _T(""), WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
        70, 150, 200, 100, hWnd, (HMENU)ID_LIST_VIEW, GetModuleHandle(NULL),NULL);

        HWND hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, _T("xxx"), WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
        50, 100, 200, 160, hWnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
    }
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

该效果是由于一个控件呈现到另一个控件的客户区,因为它正在处理 WM_PAINT 消息。

为防止这种情况发生,需要裁剪渲染,以便排除被其他控件占用的区域。在控件上设置 WS_CLIPSIBLINGS window style 指示系统应用适当的剪辑区域:

Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated. If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.