Graphics editor: drawing and changing shapes (Windows GDI) - winapi

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.

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.

Related

How do I force a repaint of only the background so a control doesn't repaint over it again?

This question was generated by a response to another question: Common Controls on a Transparent Window?.
Apparently, there is a way to only paint the background without a control painting itself again This would solve the problem of having common control buttons on a transparent background.
So my question is, how do I paint only the background around a common control after the common control has painted itself?
About how to redraw visible borders.
The rounded rectangle drawn by RoundRect is used as representative
The FrameRgn function draws a border around the specified region by using the specified brush.
Simple code demonstration:
HRGN hRegion = ::CreateRoundRectRgn (0, 0, ClientWidth, ClientHeight,12,12);
Canvas->Brush->Style = bsSolid;
Canvas->Brush->Color = RGB(96, 96, 96);
::FrameRgn(Canvas->Handle, hRegion, Canvas->Brush->Handle, 2, 2);
::DeleteObject(hRegion); // Don't leak a GDI object
Link you need: Redraw Border

Draw themed combobox on windows

I try to emulate the look of a themed noneditable combobox (CBS_DROPDOWNLIST) using DrawThemeBackground. I supply the part CP_READONLY, which apparently draws the background of a themed combobox:
DrawThemeBackground(theme, dc, CP_READONLY, CBRO_NORMAL, &rectangle, nullptr);
However, it does not contain the dropdown arrow. So, I tried to paint the arrow myself the following way:
rectangle.left = rectangle.right - 20;
DrawThemeBackground(theme, dc, CP_DROPDOWNBUTTONRIGHT, CBXSR_NORMAL, &rectangle, nullptr);
But the above draws the arrow centered within the rectangle on a combobox background including the border, so I cannot use this without having a border within the combobox (which itself already has a border). I used theme-explorer to verify that the arrow is always on a background with borders.
In essence, my question is: How can I draw the background and the arrow at the appropriate position to emulate the look of a plain windows combobox?
What I have found out so far:
I can specify a clipping rectangle to clip away the aforementioned borders. But this poses the question of determining the exact position rectangle and the clipping rectangle: It seems that I can use GetThemeMargins to determine the margins, but that does not tell me how large the arrow is as a whole.
GetThemeBitmap might be useful in determining the exact size of the arrow, but as I read here and confirmed on my machine, using it with TMT_GLYPHDIBDATA does not work as advertised, and I would like to go without any workarounds, if possible.

Xcode - CGContext - Eraser painting app "transparant" eraser for background

I have a small question.
I am making a painting/drawing app for the iPhone. In my app you can draw on backgrounds and photo's. But i can't find the solution for a eraser. Because most of the eraser samples are white colored paint. So on a non-white background it is just making white "lines". Any solutions for a (sort of) transparant eraser that will erase only draw "lines" and not the background?
Thnx for your help!
Instead of drawing using a blend mode of kCGBlendModeNormal with a white coloured paint, you will want to draw using a kCGBlendModeDestinationOut when the eraser is on. (This assumes the background is in a layer behind the drawing layer.)
For example:
<pre>
if (eraser == TRUE) // Erase to show background
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDestinationOut);
}
else // Draw with color
{
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
}
</pre>
You might also like to view this CGBlendMode reference that shows the effect of different blend types including explanations, originally posted at: http://ericasadun.com/CookbookTest/Blending.pdf
Alternative link to document: http://www.docdroid.net/xt0b/blending-kcgblendmode.pdf.html

Draw inside a win32 HRGN object

I am implementing a border control in win32, and I have implemented various styles for that border. I am using simple MoveTo() and LineTo() commands for drawing the border.
My problem is, when i select some new style for my border, it starts painting over the currently drawn border. I understand i need to refresh/repaint my window inorder to have a fresh canvas. I am using InvalidateRect() for now to achieve this purpose. But I am concerned, If i have other windows associated with my border control window (as child windows), how will this effect the child windows?i will need to repaint everything on this window, correct?
Secondly, Is there anyway I can draw lines inside a GDI Region (HRGN)? So far, all i have come across is how to fill up that HRGN with some fill color. Is there anyway i can retrieve HDC associated with that particular HRGN object??
This is not a concern, anything you draw is automatically clipped by the child window rectangles. No extra code is required. The underlying window style flag is WS_CLIPCHILDREN.
You cannot draw lines in a region nor are they associated with a device context. Other than by drawing the region and then drawing the lines in your paint message handler. You are probably interested in paths. The MSDN docs start here.

Cocoa Different text color in the same letter

What i need is a bit difficult to explain. Let's try...
I need to write a text in my app, and that text will gradually change color from left to right.
I think i could say it's a similar effect to what you see in a karaoke screen.
For instance. I draw a text line, in blue color, and gradually, using a timer, the color from left to right changes to red. But i don't want it to change letter by letter, but gradually. That would require that at some moment, the same letter can have a part in red and a part in blue.
I've been reading about core drawing, and maybe it's the solution, but i really don't know how to start. I was thinking about using a background layer and in top of it draw a transparent text (only with border), but i am not very sure how to do it.
any ideas would be appreciated.
You can probably do the job with a CGLayer.
Best of all you should be able to use your existing code to draw the characters (or anything else) in to the CGLayer.
Here's some rough code:
// first, make a CGLayer...
CGLayerRef yourDrawingPad =
CGLayerCreateWithContext(UIGraphicsGetCurrentContext(),etc..
CGContextRef yourRef = CGLayerGetContext(yourDrawingPad);
// now, draw to yourRef .. draw your various typography (or anything)
// use your current drawing code.
// all done drawing, now make an image mask...
UIGraphicsBeginImageContext(CGLayerGetSize(yourDrawingPad));
CGContextRef temp = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(temp, CGPointZero, yourDrawingPad);
CGImageRef anAlphaMask = CGBitmapContextCreateImage(temp);
UIGraphicsEndImageContext();
You now have a nice mask you can use:
CGContextSaveGState(UIGraphicsGetCurrentContext()) ...
CGContextClipToMask(UIGraphicsGetCurrentContext(), self.frame, anAlphaMask);
So just mask it in to an image of a gradient, or whatever works.
(Conceivably: perhaps you will have to construct a bitmap image offscreen, and then use that as the mask?)
Footnote: Don't forget it is very likely you will have to paint upside down! Fortunately that is simple to deal with. Where it says "now, draw to yourRef" before drawing save your state and add the two lines of code:
CGContextTranslateCTM(refForMask, 0, self.frame.size.height);
CGContextScaleCTM(refForMask, 1.0, -1.0);
Hope it helps
I have an idea of how this could be done, but it is kind of complex. You could create an NSBezierPath and add the glyphs for each character. Then, set that as the clip and draw a NSGradient. Then you would just have to change the offset of the colors in the NSGradient to move the color along.

Resources