Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:48:34+00:00 2026-06-12T18:48:34+00:00

// Draw a grid background. int width = static_cast<int>(rtSize.width); int height = static_cast<int>(rtSize.height); for

  • 0
// Draw a grid background.
        int width = static_cast<int>(rtSize.width);
        int height = static_cast<int>(rtSize.height);

        for (int x = 0; x < width; x += 10)
        {
            m_pRenderTarget->DrawLine(
                D2D1::Point2F(static_cast<FLOAT>(x), 0.0f),
                D2D1::Point2F(static_cast<FLOAT>(x), rtSize.height),
                m_pLightSlateGrayBrush,
                0.5f
                );
        }

This is the sample in the documentation. I’ve included “D2d1.h”, I just don’t know how to create a “m_pRenderTarget”. I’m writing a Kinect project, I want to draw a line on the image. I’m really new, please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:48:35+00:00Added an answer on June 12, 2026 at 6:48 pm

    Have you see this page?

    Create an ID2D1HwndRenderTarget

    The quick start tutorial has a detail steps of how to use Direct2D.

    You can also download the Windows SDK, the samples contains Direct2D demo which has the full steps of how to create Direct2D render target

    I have write a program to draw a rectangle, with a little change, it can draw a line, just for your reference

    #include <windows.h>
    #include <D2D1.h>
    
    #define SAFE_RELEASE(P) if(P){P->Release() ; P = NULL ;}
    
    ID2D1Factory*           g_pD2DFactory   = NULL; // Direct2D factory
    ID2D1HwndRenderTarget*  g_pRenderTarget = NULL; // Render target
    ID2D1SolidColorBrush*   g_pBlackBrush   = NULL; // A black brush, reflect the line color
    
    VOID CreateD2DResource(HWND hWnd)
    {
        if (!g_pRenderTarget)
        {
            HRESULT hr ;
    
            hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory) ;
            if (FAILED(hr))
            {
                MessageBox(hWnd, "Create D2D factory failed!", "Error", 0) ;
                return ;
            }
    
            // Obtain the size of the drawing area
            RECT rc ;
            GetClientRect(hWnd, &rc) ;
    
            // Create a Direct2D render target
            hr = g_pD2DFactory->CreateHwndRenderTarget(
                D2D1::RenderTargetProperties(),
                D2D1::HwndRenderTargetProperties(
                hWnd, 
                D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top)
                ), 
                &g_pRenderTarget
                ) ;
            if (FAILED(hr))
            {
                MessageBox(hWnd, "Create render target failed!", "Error", 0) ;
                return ;
            }
    
            // Create a brush
            hr = g_pRenderTarget->CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::Black),
                &g_pBlackBrush
                ) ;
            if (FAILED(hr))
            {
                MessageBox(hWnd, "Create brush failed!", "Error", 0) ;
                return ;
            }
        }
    }
    
    VOID DrawLine(HWND hwnd)
    {
        CreateD2DResource(hwnd) ;
    
        g_pRenderTarget->BeginDraw() ;
    
        // Clear background color to White
        g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
    
        // Draw Rectangle
        g_pRenderTarget->DrawLine(
            D2D1::Point2F(100.0f, 100.0f),
            D2D1::Point2F(500.0f, 500.0f),
            g_pBlackBrush
            );
    
        HRESULT hr = g_pRenderTarget->EndDraw() ;
        if (FAILED(hr))
        {
            MessageBox(NULL, "Draw failed!", "Error", 0) ;
            return ;
        }
    }
    
    
    VOID Cleanup()
    {
        SAFE_RELEASE(g_pRenderTarget) ;
        SAFE_RELEASE(g_pBlackBrush) ;
        SAFE_RELEASE(g_pD2DFactory) ;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   
    {
        switch (message)    
        {
        case   WM_PAINT:
            DrawLine(hwnd) ;
            return 0 ;
    
        case WM_KEYDOWN: 
            { 
                switch( wParam ) 
                { 
                case VK_ESCAPE: 
                    SendMessage( hwnd, WM_CLOSE, 0, 0 ); 
                    break ; 
                default: 
                    break ; 
                } 
            } 
            break ; 
    
        case WM_DESTROY: 
            Cleanup(); 
            PostQuitMessage( 0 ); 
            return 0; 
        }
    
        return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
    {
    
        WNDCLASSEX winClass ;
    
        winClass.lpszClassName = "Direct2D";
        winClass.cbSize        = sizeof(WNDCLASSEX);
        winClass.style         = CS_HREDRAW | CS_VREDRAW;
        winClass.lpfnWndProc   = WndProc;
        winClass.hInstance     = hInstance;
        winClass.hIcon         = NULL ;
        winClass.hIconSm       = NULL ;
        winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
        winClass.hbrBackground = NULL ;
        winClass.lpszMenuName  = NULL;
        winClass.cbClsExtra    = 0;
        winClass.cbWndExtra    = 0;
    
        if (!RegisterClassEx (&winClass))   
        {
            MessageBox ( NULL, TEXT( "This program requires Windows NT!" ), "error", MB_ICONERROR) ;
            return 0 ;  
        }   
    
        HWND hwnd = CreateWindowEx(NULL,  
            "Direct2D",                 // window class name
            "Draw Rectangle",           // window caption
            WS_OVERLAPPEDWINDOW,        // window style
            CW_USEDEFAULT,              // initial x position
            CW_USEDEFAULT,              // initial y position
            600,                        // initial x size
            600,                        // initial y size
            NULL,                       // parent window handle
            NULL,                       // window menu handle
            hInstance,                  // program instance handle
            NULL) ;                     // creation parameters
    
            ShowWindow (hwnd, iCmdShow) ;
            UpdateWindow (hwnd) ;
    
            MSG    msg ;  
            ZeroMemory(&msg, sizeof(msg)) ;
    
            while (GetMessage (&msg, NULL, 0, 0))  
            {
                TranslateMessage (&msg) ;
                DispatchMessage (&msg) ;
            }
    
            return msg.wParam ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am declaring this Grid Splitter: <GridSplitter HorizontalAlignment=Right VerticalAlignment=Stretch Grid.Column=1 Grid.RowSpan=1 Grid.Row=1 Width=5 Background=#FFBCBCBC
This is my code. I want to override the Draw method import wx.grid as
So my grid at this point dynamically expands to whatever height and I can
I'm trying to draw grid using the XNA framework, this grid should have a
I would like to draw a rectangular grid on the background using HTML5 Canvas
I'm trying to draw a grid of white lines on a black background. The
I'm trying to draw a 10x10 grid with Actionscript 3 with a vanishing point
Is it possible to include a draw component (circle or rectangle) inside a grid
I draw my background every frames : void Window::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
I would like to draw a grid in perspective and draw a circle in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.