repainting a drawing canvas

This is a discussion on repainting a drawing canvas within the Windows Programming forums, part of the Platform Specific Boards category; I'm working on a multi-user paint style program. The obvious problem of repainting comes up when the canvas is minimized ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    repainting a drawing canvas

    I'm working on a multi-user paint style program. The obvious problem of repainting comes up when the canvas is minimized or covered, etc. I've attempted to fix the problem by having a backup HDC handy and drawing to it as well, but it apparently does nothing. I've used examples from books and from the forums but it fails to do anything. Here is the relevant code:

    Global variables
    Code:
    HDC hdcBackup, hdcCanvas;
    HBITMAP hbitBackup;
    HBRUSH hbBackup;
    PAINTSTRUCT ps;
    The repaint
    Code:
    LRESULT WINAPI CanvasDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    	switch(uMsg)
    	{
    	case WM_CREATE:
    		{
    		int maxX = GetSystemMetrics(SM_CXSCREEN);
    		int maxY = GetSystemMetrics(SM_CYSCREEN);
    
    		hdcCanvas = GetDC(hDlgCanvas);
    		hdcBackup = CreateCompatibleDC(hdcCanvas);
    		hbitBackup = CreateCompatibleBitmap(hdcCanvas, maxX, maxY);
    		SelectObject(hdcBackup, hbitBackup);
    		hbBackup = (HBRUSH)GetStockObject(WHITE_BRUSH);
    		SelectObject(hdcBackup, hbBackup);
    		PatBlt(hdcBackup, 0, 0, maxX, maxY, PATCOPY);
    		ReleaseDC(hDlgCanvas, hdcCanvas);
    		}
    		break;
    	case WM_PAINT:
    		hdcCanvas = BeginPaint(hDlgCanvas, &ps);
    		BitBlt(hdcCanvas,
    			ps.rcPaint.left, ps.rcPaint.top,
    			ps.rcPaint.right - ps.rcPaint.left, ps.rcPaint.bottom - ps.rcPaint.top,
    			hdcBackup,
    			ps.rcPaint.left, ps.rcPaint.top,
    			SRCCOPY);
    		EndPaint(hDlgCanvas, &ps);
    		break;
    All of the drawing takes place in this function, which I've doubled to draw on the backup hdc as well.
    Code:
    void DrawObj(CPaintTool crPaintTool)
    {
    	hdcCanvas = GetDC(hDlgCanvas);
    	COLORREF crColor = RGB(crPaintTool.cRed, crPaintTool.cGreen, crPaintTool.cBlue);
    	SelectObject(hdcCanvas, (HGDIOBJ)CreatePen(PS_SOLID, crPaintTool.LineWidth, crColor));
    
    	switch (crPaintTool.ToolID)
    	{
    	case PIXELBRUSH:
    		MoveToEx(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y);
    
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y);
    		break;
    	case LINE:
    		MoveToEx(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcBackup, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		break;
    	case RECTANGLE:
    		MoveToEx(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcBackup, crPaintTool.pStart.x, crPaintTool.pEnd.y);
    		LineTo(hdcBackup, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		LineTo(hdcBackup, crPaintTool.pEnd.x, crPaintTool.pStart.y);
    		LineTo(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y);
    
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pEnd.y);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pStart.y);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y);
    		break;
    	case CIRCLE:
    		Ellipse(hdcBackup, crPaintTool.pStart.x, crPaintTool.pStart.y, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    
    		Ellipse(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		break;
    	default:
    		break;
    	}
    	ReleaseDC(hDlgCanvas, hdcCanvas);
    }

    What am I missing?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    perhaps another call to " ShowWindow (hwnd, nCmdShow);"? to refresh the window.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by kryptkat
    perhaps another call to " ShowWindow (hwnd, nCmdShow);"? to refresh the window.
    I put that before and after EndPaint() in WM_PAINT but it didn't do anything.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing HBITMAP into CWnd, Acquired from "screenshot"
    By DeusAduro in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2009, 03:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Drawing to HDC
    By Ichmael™ in forum Windows Programming
    Replies: 5
    Last Post: 03-09-2006, 09:15 AM
  4. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  5. How to do double buffing with win32 drawing?
    By Josh Kasten in forum Windows Programming
    Replies: 2
    Last Post: 03-26-2004, 11:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21