Fox
Fox

Reputation: 417

Graphics editor: drawing and changing shapes (Windows GDI)

I need to draw, move, change shapes (rectangles, circles an so one) on canvas (represented by standard "Static Control"). All drawing operations are realized by standard GDI functions.

I've realized it like this:

(example for moving shape, other operations use the same principle)

...

// Before any actions set foreground mix mode:

SetROP2(hdc_, R2_NOTXORPEN); 

...

void OnMouseDown(...)
{
  SelectShapeUnderCursor();
}

void OnMouseMove(...)
{
  ... 
  DrawShape(old_points); // Eraise shape at old position (drawing with the same pen's color, see description of R2_NOTXORPEN mode)
  DrawShape(new_points); // Draw shape at new position
  ...
}

void OnMouseUp(...)
{
  DrawShape(new_points); // Final draw shape
}

In this case shapes correctly moving and changing. But great problem is bad colors of shapes. For example, when pen has green color, shape has green color on white background and red on black background. It's normal behavior for R2_NOTXORPEN mix mode.

But I want to shapes have same color as pen. I must refuse of R2_NOTXORPEN mix mode, but how to correctly realize operations like moving, changing shapes? I may use GDI+, if needed.

Upvotes: 0

Views: 740

Answers (1)

Hans Passant
Hans Passant

Reputation: 942348

This is the way it was done back in the Windows 3.x days when all you had was a 386SUX. Now you just update the internal shape list and call InvalidateRect to let the WM_PAINT message handler re-render all shapes. No need for XOR tricks and its fugly side-effects. Double-buffer when it starts to flicker.

Upvotes: 1

Related Questions