Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win32 + Direct2D

Win32 + Direct2D

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fareed Rizkalla
    wrote on last edited by
    #1

    I've never used GDI in any of my projects. But had some programming introduction to it. Since I've been using Windows 7, I'm trying to build a new project with Direct2D. It compiles and runs, but it doesn't display anything! Does anyone have any experience, what could be the reason?

    #include
    #include

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

    int WINAPI WinMain(HINSTANCE hCurrInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    WNDCLASS SetFileTime = {};
    SetFileTime.hInstance = hCurrInstance;
    SetFileTime.lpfnWndProc = WndProc;
    SetFileTime.lpszClassName = L"D2D";

    RegisterClass(&SetFileTime);
    
    CreateWindow(
    	L"D2D",
    	L"Direct2D",
    	WS\_OVERLAPPEDWINDOW|WS\_VISIBLE,
    	100, 100, 800, 600,
    	NULL,
    	NULL,
    	hCurrInstance,
    	NULL);
    
    MSG WindowMessages = { };
    while (GetMessage(&WindowMessages, NULL, 0, 0))
    {
        TranslateMessage(&WindowMessages);
        DispatchMessage(&WindowMessages);
    }
    

    }

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch (msg)
    {
    case WM_CREATE:
    {
    ID2D1Factory* pD2DFactory = NULL;
    D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);

    		RECT rc;
    		GetClientRect(hwnd, &rc);
    
    		ID2D1HwndRenderTarget\* pRT = NULL;
    		pD2DFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), 
    			D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU( rc.right - rc.left, rc.bottom - rc.top)),
    			&pRT);
    
    
    
    		ID2D1SolidColorBrush\* m\_pLightSlateGrayBrush = NULL;
    		pRT->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::LightSlateGray), &m\_pLightSlateGrayBrush);
    		
    
    		D2D\_RECT\_F OriginalRect;
    		OriginalRect.left = 10.f;
    		OriginalRect.top = 10.f;
    		OriginalRect.bottom = 100.f;
    		OriginalRect.right = 100.f;
    
    		D2D1\_ROUNDED\_RECT RoundedRect;
    		RoundedRect.rect = OriginalRect;
    		RoundedRect.radiusX = 1.0f;
    		RoundedRect.radiusY = 1.0f;
    
    		ID2D1RoundedRectangleGeometry\* Card = NULL;
    		pD2DFactory->CreateRoundedRectangleGeometry(RoundedRect, &Card);
    
    		pRT->BeginDraw();
    		pRT->FillRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush);
    		pRT->DrawRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush);
    		pRT->EndDraw();
    
    		return 0;
    	}
    	case WM\_CLOSE:
    	{
    		DestroyWindow(hwnd);
    		return 0;
    	}
    	case WM\_DESTROY:
    	{
    		PostQuitMessage(0);
    		return 0;
    	}
    	default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    

    }

    C 1 Reply Last reply
    0
    • F Fareed Rizkalla

      I've never used GDI in any of my projects. But had some programming introduction to it. Since I've been using Windows 7, I'm trying to build a new project with Direct2D. It compiles and runs, but it doesn't display anything! Does anyone have any experience, what could be the reason?

      #include
      #include

      LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

      int WINAPI WinMain(HINSTANCE hCurrInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
      {
      WNDCLASS SetFileTime = {};
      SetFileTime.hInstance = hCurrInstance;
      SetFileTime.lpfnWndProc = WndProc;
      SetFileTime.lpszClassName = L"D2D";

      RegisterClass(&SetFileTime);
      
      CreateWindow(
      	L"D2D",
      	L"Direct2D",
      	WS\_OVERLAPPEDWINDOW|WS\_VISIBLE,
      	100, 100, 800, 600,
      	NULL,
      	NULL,
      	hCurrInstance,
      	NULL);
      
      MSG WindowMessages = { };
      while (GetMessage(&WindowMessages, NULL, 0, 0))
      {
          TranslateMessage(&WindowMessages);
          DispatchMessage(&WindowMessages);
      }
      

      }

      LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
      {
      switch (msg)
      {
      case WM_CREATE:
      {
      ID2D1Factory* pD2DFactory = NULL;
      D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);

      		RECT rc;
      		GetClientRect(hwnd, &rc);
      
      		ID2D1HwndRenderTarget\* pRT = NULL;
      		pD2DFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), 
      			D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU( rc.right - rc.left, rc.bottom - rc.top)),
      			&pRT);
      
      
      
      		ID2D1SolidColorBrush\* m\_pLightSlateGrayBrush = NULL;
      		pRT->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::LightSlateGray), &m\_pLightSlateGrayBrush);
      		
      
      		D2D\_RECT\_F OriginalRect;
      		OriginalRect.left = 10.f;
      		OriginalRect.top = 10.f;
      		OriginalRect.bottom = 100.f;
      		OriginalRect.right = 100.f;
      
      		D2D1\_ROUNDED\_RECT RoundedRect;
      		RoundedRect.rect = OriginalRect;
      		RoundedRect.radiusX = 1.0f;
      		RoundedRect.radiusY = 1.0f;
      
      		ID2D1RoundedRectangleGeometry\* Card = NULL;
      		pD2DFactory->CreateRoundedRectangleGeometry(RoundedRect, &Card);
      
      		pRT->BeginDraw();
      		pRT->FillRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush);
      		pRT->DrawRoundedRectangle(&RoundedRect, m\_pLightSlateGrayBrush);
      		pRT->EndDraw();
      
      		return 0;
      	}
      	case WM\_CLOSE:
      	{
      		DestroyWindow(hwnd);
      		return 0;
      	}
      	case WM\_DESTROY:
      	{
      		PostQuitMessage(0);
      		return 0;
      	}
      	default:
      		return DefWindowProc(hwnd, msg, wParam, lParam);
      }
      

      }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You don't handle the WM_PAINT message, in which all the drawing code should be put. You have put everything in WM_CREATE, which is called when the window is being created. I suggest you read the following article[^] to get a basic understanding of how it works.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups