dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Inverting axes in the back buffer

  1. #1
    Join Date
    Jan 2019
    Posts
    7

    Inverting axes in the back buffer

    It's me again. The last time you explained to me well with my problem, I hope you will not refuse this time.
    I want to invert the standard axes as in the traditional coordinate system .First I draw everything in the "back buffer" and then display it. But the text is mirrored, which is inconvenient to read.
    Code:
    void Draw()
    {
    	HDC hdc = GetDC(m_hwnd);
    	
    	RECT rect;
    	GetClientRect(m_hwnd, &rect);
    	
    	int width = rect.right - rect.left;
    	int height = rect.bottom - rect.top;
    	
    	HDC hMemDC = CreateCompatibleDC(hdc);
    	HBITMAP hMemBitmap = CreateCompatibleBitmap(hdc, 500, 200);
    	HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hMemBitmap);
    	HBRUSH MemBrush = CreateSolidBrush(RGB(217, 171, 171));
    	SelectObject(hMemDC, MemBrush);
    	PatBlt(hMemDC, 0, 0, 500, 200, PATCOPY);
    
    	SetMapMode(hdc, MM_ISOTROPIC);
    	SetWindowExtEx(hdc, width, height, NULL);
    	SetViewportExtEx(hdc, width, -height, NULL);
    	SetViewportOrgEx(hdc, 0, height, NULL);
    	
    	LineTo(hMemDC, 100, 100);
    	TextOut(hMemDC, 20, 30, "Hello world", 12);
    	BitBlt(hdc, 0, 0, width, height, hMemDC, 0, 0, SRCCOPY);
    
    	SelectObject(hdc, hOldBitmap);
    	ReleaseDC(m_hwnd, hdc);
    }
    If the position of the axes is changed for the memory device context, then everything is displayed incorrectly and I do not understand why.
    Code:
    void Draw2()
    {
    	HDC hdc = GetDC(m_hwnd);
    	
    	RECT rect;
    	GetClientRect(m_hwnd, &rect);
    	
    	int width = rect.right - rect.left;
    	int height = rect.bottom - rect.top;
    	
    	HDC hMemDC = CreateCompatibleDC(hdc);
    	HBITMAP hMemBitmap = CreateCompatibleBitmap(hdc, 500, 200);
    	HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hMemBitmap);
    	HBRUSH MemBrush = CreateSolidBrush(RGB(217, 171, 171));
    	SelectObject(hMemDC, MemBrush);
    	PatBlt(hMemDC, 0, 0, 500, 200, PATCOPY);
    
    	SetMapMode(hMemDC, MM_ISOTROPIC);
    	SetWindowExtEx(hMemDC, 500, 200, NULL);
    	SetViewportExtEx(hMemDC, 500, -200, NULL);
    	SetViewportOrgEx(hMemDC, 0, 200, NULL);
    	
    	LineTo(hMemDC, 100, 100);
    	TextOut(hMemDC, 20, 30, "Hello world", 12);
    
    	BitBlt(hdc, 0, 0, width, height, hMemDC, 0, 0, SRCCOPY);
    
    	SelectObject(hdc, hOldBitmap);
    	ReleaseDC(m_hwnd, hdc);
    }
    In the device context, everything is displayed as planned.
    Code:
    void Draw3()
    {
    	HDC hdc = GetDC(m_hwnd);
    	
    	RECT rect;
    	GetClientRect(m_hwnd, &rect);
    	
    	int width = rect.right - rect.left;
    	int height = rect.bottom - rect.top;
    
    	SetMapMode(hdc, MM_ISOTROPIC);
    	SetWindowExtEx(hdc, 500, 200, NULL);
    	SetViewportExtEx(hdc, 500, -200, NULL);
    	SetViewportOrgEx(hdc, 0, 200, NULL);
    	
    	LineTo(hdc, 100, 100);
    	TextOut(hdc, 20, 30, "Hello world", 12);
    
    	ReleaseDC(m_hwnd, hdc);
    }
    How to make axes for the back buffer so that the image is displayed as in the function Draw3?
    And help me understand. I change (0; 0) in the Draw function to (0; height) for the device context. Why is everything displayed in the upper left corner, if it should in the lower left?

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,136

    Re: Inverting axes in the back buffer

    Bitmaps in Win32 are usually up-side down.
    See this article: https://docs.microsoft.com/en-us/win...bottom-up-dibs
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jan 2019
    Posts
    7

    Re: Inverting axes in the back buffer

    Quote Originally Posted by Marc G View Post
    Bitmaps in Win32 are usually up-side down.
    See this article: https://docs.microsoft.com/en-us/win...bottom-up-dibs
    Thank you very much for the article. However, it's still not clear how to fix the axes in the back buffer.
    It is also not clear what the SetViewportOrgEx function does with the memory buffer. I mean this case.
    Code:
    	SetMapMode(hMemDC, MM_ISOTROPIC);
    	SetWindowExtEx(hMemDC, 500, 200, NULL);
    	SetViewportExtEx(hMemDC, 500, -200, NULL);
    	SetViewportOrgEx(hMemDC, 0, 200, NULL);
    The description says that it shifts the point (0; 0), but it doesn’t appear at all.

  4. #4
    Join Date
    Jul 2006
    Posts
    71

    Re: Inverting axes in the back buffer

    I am not programming with win GDI for a long time so i can't be 100% sure.
    The function TextOut(hMemDC, 20, 30, "Hello world", 12); expects the origin (0,0) to be at the top left corner of the window.
    As you are changing the origin before drawing the text you see wrong view.
    When i was using OpenGL i faced with the same situation. glPush/glPop matrix function helped to solve that problem.
    In your situation you can set origin back before drawing the text and set another after if you want to work in another axis set.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)