【问题标题】:Draw an image in a Picture Control在优化校准中绘制图像
【发布时间】:2013-02-06 10:31:50
【问题描述】:

我有一个名为IDC_PICTURECONTROL 的图片控件和一个名为LPPICTURElpPicutre

当我的窗口收到WM_PAINT 时,我像这样调用我的函数drawPicture(HWND, LPPICTURE)

drawPicture(GetDlgItem(hDlg, IDC_PICTURECONTROL), lpPicture);

现在的写法,控件周围的黑色边框刚刚消失,完全没有画图。

如果我编辑该函数,使其不绘制到图片控件,而是绘制到对话框本身 (hDlg),那么它会正确绘制在窗口客户区的背景上。 (不是我想要的)。

这是paint函数中的代码:

void drawPicture(HWND hWnd, LPPICTURE picture)
{
    PAINTSTRUCT ps;
    HDC hdc;

    hdc = BeginPaint(hWnd, &ps);
  //hdc = BeginPaint(hDlg, &ps); (works, but draws on window instead of control)

    if (picture)
    {
        long hmWidth;
        long hmHeight;
        picture->get_Width(&hmWidth);
        picture->get_Height(&hmHeight);

        int nWidth  = MulDiv(hmWidth,  GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
        int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);

        RECT rc;
        GetClientRect(hWnd, &rc); // I have tried GetWindowRect() also

        int w = 0, h = 0, x = 0, y = 0;
        if (hmWidth == hmHeight)
        {
            // square
            w = (rc.right - rc.left);
            h = (rc.bottom - rc.top);

            x = rc.left;
            y = rc.top;
        }
        else if (hmWidth > hmHeight)
        {
            // wide
            w = (rc.right - rc.left);
            h = (w * hmHeight) / hmWidth;

            x = rc.left;
            y = (rc.bottom - rc.top - h) / 2;
        }
        else
        {
            //tall
            h = (rc.bottom - rc.top);
            w = (h * hmWidth) / hmHeight;

            y = rc.top;
            x = (rc.right - rc.left - w) / 2;
        }

        picture->Render(hdc, x, y, w, h, 0, hmHeight, hmWidth, -hmHeight, &rc);
    }

    EndPaint(hWnd, &ps);
  //EndPaint(hDlg, &ps);
}

hWnd是图片控件的句柄,hDlg是对话框的句柄。

我想它可能是从某个地方拉出窗口,所以我将 x 和 y 设置为 0,将宽度和高度设置为 1000,但这并没有改变任何东西。

我做错了什么?

【问题讨论】:

    标签: c++ windows winapi


    【解决方案1】:

    如果此代码与 hDlg 一起使用,则没问题,问题可能出在静态控件本身。确保它具有 SS_BITMAP 样式。在 Visual Studio 资源编辑器中称为 Type,默认设置为 SS_BLACKFRAME (Frame)。

    【讨论】:

      【解决方案2】:

      此代码适用于 CStatic。

      CPictureView* image = new CPictureView(std::string("E:\\My Documents\\..jpeg"));
      image->Create("", SS_BLACKRECT | SS_OWNERDRAW | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 300), this, IDC_STATIC1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2011-12-08
        • 2018-04-23
        • 1970-01-01
        相关资源
        最近更新 更多