gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:33:03 25 4
gpt4 key购买 nike

我最近一直在玩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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com