lParam或WCHAR []出错了

时间:2013-03-14 00:15:42

标签: c++ arrays string pointers lparam

首先,多次调用此函数。应该注意的是,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应该为非零,因为log包含多个'\ 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.出了什么问题?

1 个答案:

答案 0 :(得分:2)

“出了什么问题?”

最有可能的是,您正在处理的WM_PAINT并非来自您的SendMessage

一般建议:不要发送或发布WM_PAINT,让系统生成该消息(当您从线程的消息队列中检索消息并且窗口需要重新绘制时,它会执行此消息)

相关问题