【发布时间】:2013-03-02 04:37:53
【问题描述】:
首先,这个函数被多次调用。需要注意的是 wString[] 确实包含字符常量 '\n'。
void D2DResources::PutToLog(WCHAR wString[])
{
int strLen=wcslen(wString);
int logLen=wcslen(log);
if(strLen+logLen>=MaxLogSize)
wcsncpy(log, log, logLen-strLen);
wcscat (log, wString);
int nLines=0;
for(int x=0; x<wcslen(log); x++)
{
if(log[x]=='\n')
{
nLines++;
if(nLines>5)
{
log[x]='\0';
}
}
}
SendMessage (m_hWnd, WM_PAINT, NULL, (LPARAM)nLines);
}
最后,发送一条 WM_PAINT 消息,而 nLines 应为非零,因为日志包含多个 '\n'。我的 WndProc 接收消息并处理它。
case WM_PAINT:
{
pD2DResources->OnRender((int)lParam);
ValidateRect(hWnd, NULL);
}
break;
在此之后,使用(假定的)非零 int 作为 lParam 调用 OnRender。
void D2DResources::OnRender(int nLogLines)
{
D2D1_SIZE_F screenSize = pCurrentScreen->GetSize();
D2D1_SIZE_F rTSize = pRT->GetSize();
pRT->BeginDraw();
pRT->DrawBitmap(
pCurrentScreen,
D2D1::RectF(0.0f, 0.0f, screenSize.width, screenSize.height)
);
pRT->DrawText(
log,
ARRAYSIZE(log) - 1,
pTextFormat,
D2D1::RectF(0, rTSize.height - ((nLogLines*textSize)+textSize) , rTSize.width, rTSize.height),
pWhiteBrush
);
pRT->EndDraw();
}
由于某种原因,在 OnRender 函数中,nLogLines 的值为 0。这是怎么回事?
【问题讨论】:
-
发送或发布 WM_PAINT 不是一个好主意。使需要重新绘制的窗口的任何部分无效。然后 WM_PAINT 在您下次访问消息队列时生成。
-
我现在使用了 WCHAR*,nLines 确实获得了应有的价值。但是,我确实遇到了另一个问题,这可能与发送的 WM_PAINT 消息有关。我将使用 invalidateRect 代替。谢谢。
标签: c++ arrays string pointers lparam