// The code below is from the wizard but it has been customized for this 
// application. There is a custom code to handle WM_SIZE, WM_GESTURENOTIFY
// and WM_GESTURE messages.
//
// Processes messages for the main window:
//      WM_COMMAND        - process the application menu
//      WM_SIZE           - process resizing of client window
//      WM_PAINT          - paint the main window
//      WM_DESTROY        - post a quit message and return
//      WM_GESTURENOTIFY  - process a gesture notification message 
//      WM_GESTURE        - process the gesture command
// in:
//      hWnd        window handle
//      message     message code
//      wParam      message parameter (message-specific)
//      lParam      message parameter (message-specific)
// returns:
//      the result of the message processing and depends on the message sent
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_GESTURENOTIFY:
        {
            // This is the right place to define the list of gestures that this
            // application will support. By populating GESTURECONFIG structure 
            // and calling SetGestureConfig function. We can choose gestures 
            // that we want to handle in our application. In this app we
            // decide to handle all gestures.
            GESTURECONFIG gc = {
                0,              // gesture ID
                GC_ALLGESTURES, // settings related to gesture ID that are to be 
                                // turned on
                0               // settings related to gesture ID that are to be 
                                // turned off
            };

            BOOL bResult = SetGestureConfig(
                hWnd,                 // window for which configuration is specified  
                0,                    // reserved, must be 0
                1,                    // count of GESTURECONFIG structures
                &gc,                  // array of GESTURECONFIG structures, dwIDs will be processed in the
                                      // order specified and repeated occurances will overwrite previous ones
                sizeof(GESTURECONFIG) // sizeof(GESTURECONFIG)
            );                        
            
            if (!bResult)
            {
                ASSERT(L"Error in execution of SetGestureConfig" && 0);
            }
        }
        break;

    case WM_GESTURE:
        // The gesture processing code is implemented in the CGestureEngine 
        // class.
        return g_cGestureEngine.WndProc(hWnd,wParam,lParam);
        break;

    case WM_SIZE:
        // resize rectangle and place it in the middle of the new client area
        g_cRect.ResetObject(LOWORD(lParam),HIWORD(lParam));
        break;

    case WM_COMMAND:
        wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;

    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);       

        // Full redraw: background + rectangle
        g_cRect.Paint(hdc);

        EndPaint(hWnd, &ps);
        break;

    case WM_DESTROY:

        PostQuitMessage(0);

        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
示例#2
0
void CTinyCadView::OnDraw(CDC* pDC)
{
	//CTinyCadDoc* pDoc = GetCurrentDocument();
	CDC BitmapDC;
	CBitmap *old_bitmap = NULL;

	int selected;

	CRect client;
	if (pDC->IsKindOf(RUNTIME_CLASS(CPaintDC)))
	{
		client = static_cast<CPaintDC*> (pDC)->m_ps.rcPaint;
	}
	else
	{
		GetClientRect(&client);
	}

	// Are we going to use off-screen drawing?
	BOOL osb = !pDC->IsPrinting() && m_use_offscreen_drawing && CreateBitmap(*pDC, client.Width(), client.Height());

	if (osb)
	{
		BitmapDC.CreateCompatibleDC(pDC);
		old_bitmap = BitmapDC.SelectObject(&m_bitmap);
	}

	{
		CContext dc(osb ? &BitmapDC : pDC, GetTransform(), this);

		CDPoint origin = GetTransform().GetOrigin();

		if (osb)
		{
			CPoint point = CPoint(-client.left, -client.top);
			dc.SetPixelOffset(point);
		}

		if (pDC->IsPrinting())
		{
			dc.SetBlack(CTinyCadRegistry::GetPrintBandW());
		}

		CDPoint Start, End;
		CRect rect;
		GetClientRect(&rect);
		TransformSnap snap;
		snap.SetGridSnap(FALSE);
		Start = GetTransform().DeScale(snap, CPoint(rect.left, rect.top));
		End = GetTransform().DeScale(snap, CPoint(rect.right, rect.bottom));

		// Is any of this region in the off-page area?
		if (!pDC->IsPrinting())
		{

			// Paint the region white
			if (pDC->IsPrinting())
			{
				dc.SelectBrush(cWHITE);
				dc.SelectPen(PS_SOLID, 1, cWHITE);
			}
			else
			{
				COLORREF col = GetCurrentDocument()->GetOptions()->GetUserColor().Get(CUserColor::BACKGROUND);
				dc.SelectBrush(col, 0);
				dc.SelectPen(PS_SOLID, 1, col);
			}
			dc.Rectangle(CDRect(Start.x - 2, Start.y - 2, End.x + 2, End.y + 2));

			dc.SelectBrush(cOFFPAGE);
			dc.SelectPen(PS_SOLID, 1, cOFFPAGE);

			if (End.x > GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x)
			{
				CDPoint a = CDPoint(GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x, 0);
				dc.Rectangle(CDRect(a.x, a.y, End.x, End.y));
			}
			if (End.y > GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y)
			{
				CDPoint a = CDPoint(Start.x, GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y);
				dc.Rectangle(CDRect(a.x, a.y, End.x, End.y));
			}
			if (Start.x < 0) dc.Rectangle(CDRect(0, Start.y, Start.x, End.y));
			if (Start.y < 0) dc.Rectangle(CDRect(Start.x, 0, End.x, Start.y));

			// Fill this region with a grid
			double grid = GetCurrentDocument()->m_snap.GetGrid();
			double SGrid = dc.GetTransform().doubleScale(grid);
			if (GetCurrentDocument()->GetOptions()->ShowGrid() && SGrid > 10)
			{
				double x = dc.GetTransform().GetOrigin().x;
				double y = dc.GetTransform().GetOrigin().y;

				TransformSnap s = GetCurrentDocument()->m_snap;
				s.SetGridSnap(TRUE);

				x = s.Snap(x);
				y = s.Snap(y);

				for (double xp = x >= 0 ? x : 0; xp < End.x && xp < GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x; xp += grid)
				{
					for (double yp = y >= 0 ? y : 0; yp < End.y && yp < GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y; yp += grid)
					{
						dc.SetPixel(CDPoint(xp, yp), 0);
					}
				}
			}
		}

		Start -= CDPoint(10, 10);
		End += CDPoint(10, 10);

		GetCurrentDocument()->GetSelectBegin();

		drawingIterator it = GetCurrentDocument()->GetDrawingBegin();
		while (it != GetCurrentDocument()->GetDrawingEnd())
		{
			CDrawingObject *obj = *it;

			selected = !pDC->IsPrinting() && GetCurrentDocument()->IsSelected(obj);
			paint_options options = selected ? draw_selected : draw_normal;

			if (!pDC->IsPrinting() || !obj->IsConstruction())
			{
				if (pDC->IsPrinting() || obj->IsInside(Start.x, End.x, Start.y, End.y))
				{
					obj->Paint(dc, options);
				}
			}

			++it;
		}

		// Now draw the selectable object, so it stands out...
		CDrawingObject *obj = GetCurrentDocument()->GetSelectable();
		if (obj != NULL && !GetCurrentDocument()->IsSelected(obj))
		{
			paint_options options = draw_selectable;
			GetCurrentDocument()->GetSelectable()->Paint(dc, options);
		}

		// If only one item is selected then just draw its handles now
		if (GetCurrentDocument()->IsSingleItemSelected())
		{
			GetCurrentDocument()->GetSingleSelectedItem()->PaintHandles(dc);
		}

		// if necessary turn back on the current object to be edited
		if (GetCurrentDocument()->GetEdit() != NULL) {
			//ATLTRACE2("TinyCadView::GetCurrentDocument->GetEdit->Paint(dc, draw_selected=%d)\n",draw_selected);
			GetCurrentDocument()->GetEdit()->Paint(dc, draw_selected);
		}

		// Draw the design details
		GetCurrentDocument()->Display(dc);
	}

	if (osb)
	{
		pDC->BitBlt(client.left, client.top, client.Width(), client.Height(), &BitmapDC, 0, 0, SRCCOPY);
		BitmapDC.SelectObject(old_bitmap);
	}
}