c - c-如果将hdc传递给方法,则不会绘制win32 api矩形

标签 c winapi

我最近一直在玩C和win32,以下内容使我陷入困境:

     case WM_PAINT:
         g_crntRect = (RECT*) malloc(sizeof(RECT));
         GetWindowRect(hwnd, g_crntRect);

         hpen = CreatePen(PS_SOLID, 1, RGB(255,25,5));

         hdc = BeginPaint (hwnd, &ps) ;

         oldPen = SelectObject(hdc, hpen);
         drawRects(hwnd, hdc);
         //Rectangle(hdc, 0, 0, 840, 525);

         SelectObject(hdc,oldPen);
         DeleteObject(hpen);

         EndPaint (hwnd, &ps) ;
         return 0 ;

因此,如果我在上面调用我自己的方法来绘制矩形,则它不会绘制任何内容,但是我上面评论过的在WM_PAINT中绘制矩形的调用没有问题。

这是我的方法:
BOOL drawRects(HWND hwnd, HDC hdc)
{

char buffer[50];
BOOL res = FALSE;
RECT tempRect = {0};
char quadStr[6] = "";
int i = 0;
quadStr[i]='*';
OutputDebugString("Going to draw");
for (i = 1; i <= 4; i++)
{
    //get rect for each quadrent from the parent
    OutputDebugString("inside for");
    getRect(g_crntRect, &tempRect, i);

    OutputDebugString("got rectr");;
    res = Rectangle(hdc, tempRect.right, tempRect.top, tempRect.right, tempRect.bottom);

    if (res == FALSE)
    {
        OutputDebugString("false");;
        sprintf(buffer, "Error: %ld", GetLastError());
        OutputDebugString(buffer);
    }
    else
    {
        OutputDebugString("drew");;
    }

    quadStr[i]='*';
    printRect(quadStr, &tempRect);
}

return TRUE;

}

查看调试输出,一切似乎都很好。正确的值将传递给Rectangle方法。但是,我想知道我是否没有正确通过HDC?

有任何想法吗?

最佳答案

看起来像是一个简单的错字。在您的方法中,您有:

res = Rectangle(hdc, tempRect.right, tempRect.top, tempRect.right, tempRect.bottom);

第二个参数应该是tempRect.left而不是tempRect.right。您正在尝试绘制一个零宽度的矩形。

关于c - c-如果将hdc传递给方法,则不会绘制win32 api矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4958677/

相关文章:

ios - 带有 ARC 的 Objective C 中的对象数组

c++ - DirectX 9 高级教程 C++

c++ - 用C++读取二进制文件并翻译其中的一些内容

c++ - 为什么在 WndProc 中转换不正确?

C++ 如何在不使用 winapi 的情况下 move 文件并将它们从一个磁盘复制到不同的磁盘?

c - 如何从 pid 中杀死(和关闭)标签?

c - 如何用C做意大利面条?

c - 数据包解析错误

c - 我应该如何处理从未使用过的值?

c - 为什么我的无符号长数组指针递增 8 个字节而不是四个字节?