Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c++ - IDWriteFactory::CreateTextLayout method causes window to suddenly close

In the app that I'm building using the Windows API, I am currently working on writing text to the screen. I am trying to use the CreateTextLayout method, but with that particular function, I am running into problems that I can't figure out how to solve. When I just run my app, I starts fine and the just exits with no error warnings of any kind. Using breakpoints, I traced the issue to that function. Here is the code I am using to call it:

IDWriteFactory* writeFactory;
IDWriteTextFormat* writeTextFormat;
IDWriteTextLayout* writeTextLayout;

 HRESULT App::CreateDeviceIndependentResources()
{
    HRESULT hr;
// Create a Direct2D factory.
hr = D2D1CreateFactory(
    D2D1_FACTORY_TYPE_SINGLE_THREADED,
    &writeFactory
);


if (SUCCEEDED(hr))
{
    hr = DWriteCreateFactory(
        DWRITE_FACTORY_TYPE_SHARED,
        __uuidof(writeFactory),
        reinterpret_cast<IUnknown**>(&writeFactory)
    );

}

if (writeFactory == NULL)
        OutputDebugStringA("NULL
");

if (SUCCEEDED(hr))
{
    hr =  writeFactory->CreateTextFormat(
        L"Times New Roman",
        NULL,
        DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STYLE_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL,
        14.0f,
        L"EN-US",
        &writeTextFormat
    );
}
if (SUCCEEDED(hr))
{
    // Create a DirectWrite text format object.
    hr = writeFactory->CreateTextLayout(
        L"Projects",      // The string to be laid out and formatted.
        8,  // The length of the string.
        writeTextFormat,  // The text format to apply to the string (contains font information, etc).
        10.0f,         // The width of the layout box.
        10.0f,        // The height of the layout box.
        &writeTextLayout  // The IDWriteTextLayout interface pointer.
    );
}
}

I feel like this is pretty basic becuase I just followed the tutorial on Microsoft.com; however, something is clearly wrong. My guess is that it could be related to writeTextFormat. It is an IDWriteTextFormat object. Additionally, all of these objects are initialized in the CreateDeviceIndependentResources() functions. Should I initialize them elsewhere?

question from:https://stackoverflow.com/questions/66056260/idwritefactorycreatetextlayout-method-causes-window-to-suddenly-close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You confuse the use of D2D1CreateFactory and DWriteCreateFactory.

D2D1CreateFactory is used to create the ID2D1Factory interface, which is used to create an ID2D1HwndRenderTarget and an ID2D1SolidColorBrush to render text.

DWriteCreateFactory is used to create an IDWriteFactory interface, which is the root factory interface for all DirectWrite objects. And the DirectWrite objects are used to format the text.

Such as:

ID2D1HwndRenderTarget* pRT_;
...

pRT_->DrawText(
    wszText_,        // The string to render.
    cTextLength_,    // The string's length.
    pTextFormat_,    // The text format.
    layoutRect,       // The region of the window where the text will be rendered.
    pBlackBrush_     // The brush used to draw the text.
    );

Maybe you can start from Part 2: Create Device Independent Resources.

Some code:

IDWriteFactory* pDWriteFactory_;

hr = DWriteCreateFactory(
       DWRITE_FACTORY_TYPE_SHARED,
       __uuidof(IDWriteFactory),
       reinterpret_cast<IUnknown**>(&pDWriteFactory_)
       );
...

if (SUCCEEDED(hr))
{
    // Create a DirectWrite text format object.
    hr = pDWriteFactory_->CreateTextLayout(
        L"Projects",      // The string to be laid out and formatted.
        8,  // The length of the string.
        writeTextFormat,  // The text format to apply to the string (contains font information, etc).
        10.0f,         // The width of the layout box.
        10.0f,        // The height of the layout box.
        &writeTextLayout  // The IDWriteTextLayout interface pointer.
    );
}

Official example: HelloWorld


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...