更好地控制我的客户区(WIN32)

时间:2015-02-02 04:37:55

标签: c++ winapi

我正在尝试在窗口上用黑色笔制作3 x 3网格。但我希望它居中,例如我的网格在白色空间内,  顶部,右侧,左侧和底部的10%。即使我们调整窗口大小,我的网格也会适应剩余的80%。

现在我可以制作网格但经过多次尝试创建10%区域后,感到很沮丧。

case WM_SIZE:
    //get the 10% range.
    cxInvalid = LOWORD(lParam) * 0.1;
    cyInvalid = HIWORD(lParam) * 0.1;

    //get the grid, DIVISIONS = 3
    cxBlock = LOWORD(lParam) / DIVISIONS;
    cyBlock = HIWORD(lParam) / DIVISIONS;
    return 0;

Thanks in advaced :)
case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);

    for (x = 0; x < DIVISIONS; x++)
        for (y = 0; y < DIVISIONS; y++)
        {
            Rectangle(hdc, x * cxBlock, y * cyBlock,
                (x + 1) * cxBlock, (y + 1) * cyBlock);

        }
    EndPaint(hwnd, &ps);
    return 0;

1 个答案:

答案 0 :(得分:3)

这正是打算使用Windows映射模式的那种问题。目前我将假设你希望你的网格保持正方形,无论它的窗口形状如何。

一种方法是从默认的MM_TEXT映射模式切换到MM_ISOTROPIC映射模式(但如果我们希望网格改变周围窗口的形状,我们会使用{ {1}}代替)。

使用它,我们可以将窗口设置为1200 x 1200单元格的虚拟网格,然后在其上绘制3x3网格。我选择了1200 x 1200,所以我们关心的部件将是一个漂亮,方便的1000 x 1000网格。

MM_ANISOTRCOPIC

重申// set up the mapping mode: RECT rect; GetClientRect(hWnd, &rect); SetMapMode(hDC, MM_ISOTROPIC); SetViewportExt(rect.x, rect.y); // The virtual width/height for our window: static const int width = 1200; static const int height = 1200; SetWindowExt(width, height); SetWindowOrg(-100, -100); // Set the virtual 0 point ~10% of the way into the window. // And then draw the grid. We always draw in a 1000 x 1000 grid, and Windows // scales that to the actual window size for us. // static const int grid_size = 1000; static const int step = grid_size / 3; for (int i = step; i < grid_size-1; i += step) { MoveTo(hDC, i, 0); LineTo(hDC, i, grid_size); MoveTo(hDC, 0, i); LineTo(hDC, grid_size, i); } MM_ISOTROPIC之间的区别,以下是网格的屏幕截图。首先,它是用MM_ANISOTROPIC绘制的:

enter image description here

...然后用MM_ISOTROPIC绘制:

enter image description here