Direct2d是否可以按监视器大小在窗口上绘制而不管窗口大小如何?

如何解决Direct2d是否可以按监视器大小在窗口上绘制而不管窗口大小如何?

我有用Direct2D渲染的DirectX窗口。由于renderTarget已调整大小,因此当窗口调整大小绘制的2D内容时,我的问题是按窗口大小进行缩放。我不想使用等于监视器大小的“儿童窗口”。我应该画在同一扇窗户上。我发现 HwndRenderTarget 不管窗口大小如何变化,都可以帮助渲染2D。我不确定这是否用于此。

我的2D渲染目标:

    D2D1_FACTORY_OPTIONS options2d;
options2d.debugLevel = D2D1_DEBUG_LEVEL_NONE;
result = D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_MULTI_THREADED,options2d,&m_factory2d);

m_screenSize.x = screenWidth; m_screenSize.y = screenHeight;

if (FAILED(result))
{
    return false;
}

// set up the D2D render target using the back buffer

m_swapChain->GetBuffer(0,IID_PPV_ARGS(&m_dxgiBackbuffer));

D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN,D2D1_ALPHA_MODE_PREMULTIPLIED));

m_factory2d->CreateDxgiSurfaceRenderTarget(m_dxgiBackbuffer,props,&m_d2dRenderTarget);

HWND渲染目标:

    RECT rc;
GetClientRect(hwnd,&rc);

D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top);

D2D1_RENDER_TARGET_PROPERTIES RTprops = D2D1::RenderTargetProperties();
D2D1_HWND_RENDER_TARGET_PROPERTIES RT_hWndProps = D2D1::HwndRenderTargetProperties(hwnd,size,D2D1_PRESENT_OPTIONS::D2D1_PRESENT_OPTIONS_IMMEDIATELY);
result = m_factory2d->CreateHwndRenderTarget(RTprops,RT_hWndProps,&m_d2dHwndRenderTarget);

和:

void D2DClass::TryRenderHWNDText() {
        static const WCHAR sc_helloWorld[] = L"Hello,World!";

        // Retrieve the size of the render target.
        D2D1_SIZE_F renderTargetSize = m_d2dHwndRenderTarget->GetSize();

        m_d2dHwndRenderTarget->BeginDraw();
        m_d2dHwndRenderTarget->Clear(ColorF(ColorF::Red));

        m_d2dHwndRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
        m_whiteBrush->SetColor(ColorF(ColorF::Orange));
        if(textFormat){
        m_d2dHwndRenderTarget->DrawTextW(
            sc_helloWorld,ARRAYSIZE(sc_helloWorld) - 1,textFormat,D2D1::RectF(0,renderTargetSize.width,renderTargetSize.height),m_whiteBrush
        );
        }
        m_d2dHwndRenderTarget->EndDraw();
}

在渲染循环中:

bool GraphicsClass::Render()
{
    m_Direct3D->BeginScene(0.0f,0.0f,1.0f);
    /*RENDER 2D renderTarget NOT hwndTarget and 3D target*/
    m_Direct3D->EndScene();

    m_Direct2D->TryRenderHWNDText();

}

BeginScene:

void D3DClass::BeginScene(float red,float green,float blue,float alpha)
{
    float color[4];


    // Setup the color to clear the buffer to.
    color[0] = red;
    color[1] = green;
    color[2] = blue;
    color[3] = alpha;

    // Clear the back buffer.

    m_deviceContext->ClearRenderTargetView(m_renderTargetView,color);

    // Clear the depth buffer.
    m_deviceContext->ClearDepthStencilView(m_depthStencilView,D3D11_CLEAR_DEPTH,1.0f,0);

    return;
}

结束场景:

void D3DClass::EndScene()
{
    // Present the back buffer to the screen since rendering is complete.
    if (m_vsync_enabled)
    {
        // Lock to screen refresh rate.
        m_swapChain->Present(1,0);
    }
    else
    {
        // Present as fast as possible.
        m_swapChain->Present(0,0);
    }

    return;
}

我看到2D和3D,但没有看到HWNDrenderTarget。我在这里想念什么? 我有什么解决办法吗?

谢谢您的建议

解决方法

这是解决方案:

注意: 此解决方案适用于您具有无边界子窗口且其大小与监视器相同的情况。

您应该修改程序的视口大小和屏幕外观, 只需在D3D11_VIEWPORT处定义即可。然后设置宽度(父窗口宽度),高度(父窗口高度),SCREEN_NEAR(0.1),SCREEN_DEPTH(1000)和screenAspect。

这是结果:

<!-- Window.xaml -->
<StackPanel>
     <ContentControl IsTabStop="False" Content="{Binding UCtrl}"/>
</StackPanel>

<!-- UserControl.xaml -->
<UserControl.Resources>
    <Style x:Key="TabItemStyle" TargetType="{x:Type TreeViewItem}">
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle"/>
     </Style>
</UserControl.Resources>
<StackPanel>
    <TreeView IsTabStop="False" ItemContainerStyle="{StaticResource TabItemStyle}">
        <TreeViewItem IsExpanded="True" HorizontalContentAlignment="Stretch" >
            <TreeViewItem.Header>
                <TextBox TabIndex="1" Width="100"/>
            </TreeViewItem.Header>
        </TreeViewItem>
        <TreeViewItem IsExpanded="True" HorizontalContentAlignment="Stretch" >
            <TreeViewItem.Header>
                <TextBox TabIndex="2" Width="100"/>
            </TreeViewItem.Header>
        </TreeViewItem>
        <TreeViewItem IsExpanded="True" HorizontalContentAlignment="Stretch" >
            <TreeViewItem.Header>
                <TextBox TabIndex="3" Width="100"/>
            </TreeViewItem.Header>
        </TreeViewItem>
    </TreeView>
</StackPanel>

只需调用上面的void即可使其在消息中起作用。为防止消息 WM_SIZING WM_SIZE WM_MOVE 之间发生冲突,请在 Is_WM_SIZING 中添加一个bool并允许 UpdateFrame() 仅当生成 WM_SIZE WM_MOVE 消息时为假。在WM_EXITSIZEMOVE消息中将此值设置为false,并且在调用 UpdateFrame()之后也将其设置为false。这样可以防止某些类型的渲染问题,具体取决于您的present(...)调用。

其他操作: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowrgn

有时,如果您从左,左上,左下和左上的窗口调整大小的速度过快,则会产生严重的伪影,并向右或向右溢出。 Microsoft文档说SetWindowRgn可以防止在其内容之外进行绘制,这使我的程序变得更好。

void SystemClass::UpdateFrame() {

    if (ApplicationHandle->m_Graphics) {

            GetClientRect(ApplicationHandle->m_hwndOWNER,&clientRect);
            int w = ApplicationHandle->ClientSize.x = RECTWIDTH(clientRect);
            int h = ApplicationHandle->ClientSize.y = RECTHEIGHT(clientRect);
    
            ApplicationHandle->m_Graphics->clientSize = { w,h };
    
    
            viewport.Width = RECTWIDTH(clientRect);
            viewport.Height = RECTHEIGHT(clientRect);
    
            ApplicationHandle->m_Graphics->m_Direct3D->GetDeviceContext()->RSSetViewports(1,&viewport);
    
            float fieldOfView = 3.141592654f / 4.0f;
            float screenAspect = (float)RECTWIDTH(clientRect) / (float)RECTHEIGHT(clientRect);
    
            ApplicationHandle->m_Graphics->m_Direct3D->m_projectionMatrix = XMMatrixPerspectiveFovLH(fieldOfView,screenAspect,SCREEN_NEAR,SCREEN_DEPTH);
    
            ApplicationHandle->m_Graphics->Frame();
            DXRGN = CreateRectRgn(0,w,h);
            SetWindowRgn(ApplicationHandle->m_hwnd,DXRGN,0);
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-