I'm trying to learn MFC from book: MV C++ Windows Application by Example(2008). There is example app. where I can draw rings filled witch chosen color:
void CRingView::OnDraw(CDC* pDC)
{
CRingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
PointArray& pointArray = pDoc->GetPointArray();
ColorArray& colorArray = pDoc->GetColorArray();
int iSize = static_cast<int>(pointArray.GetSize());
for (int iIndex = 0; iIndex < iSize; iIndex++)
{
CPoint point = pointArray[iIndex];
COLORREF color = colorArray[iIndex];
CPen pen(PS_SOLID, 0, BLACK);
CBrush brush(color);
pDC->Ellipse(point.x - RADIUS, point.y - RADIUS, point.x + RADIUS, point.y + RADIUS);
CPen* pOldPen = pDC->SelectObject(&pen);
CBrush* pOldBrush = pDC->SelectObject(&brush);
}
}
but there is no color change(always white like bg) even if I do:
CBrush brush(BLACK);
So question is: What I'm doing wrong? I'm using Visual Studio 2013 but with new project there shouldn't be any compatibility errors.
And BLACK is:
static const COLORREF BLACK = RGB(0, 0, 0);
The DC draws with whatever brush, pen, font, etc. objects are currently selected. So SelectObject of the pen and the brush should happen before doing the drawing.
Related
Ok suppose I have a brush,
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0));
And I want to change it's color.
Not calling CreateSolidBrush and DeleteObject on it over and over again.
Like in this example,
#define INFINITY UINT64_MAX // You get the point. I am just calling it many times.
RECT rect = { 0 };
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0)); // Same brush as the one above.
for(uint64_t i = 0; i < INFINITY; i++){
SetRect(&rect, 0, i, i, i + 1); // Right angle triangle btw.
// How would I change the color of the brush?
FillRect(hdc, &rect, brush);
}
As shown above, the reason I don't want to use CreateSolidBrush and DeleteObject again and again, is that it is slow and I need to be able to change the color of the brush quickly.
I have found SetDCBrushColor. Which can change the color of the selected brush? But doesn't seem to change my brush even after selecting it to the context.
That's why I'm wondering if there is any alternative to SetDCBrushColor.
So that I can use my brush in FillRect.
Any help is greatly appreciated. Thanks in advance.
Actually, I am so sorry for asking this question. I found the answer.
Here it is:
HBRUSH dcbrush = (HBRUSH)::GetStockObject(DC_BRUSH); // Returns the DC brush.
COLORREF randomColor = RGB(69, 69, 69);
SetDCBrushColor(hdc, randomColor); // Changing the DC brush's color.
In the above snippet;
Calling GetStockObject(DC_BRUSH) returns the DC brush.
After receiving the brush, I can change it's color with the above mentioned.
SetDCBrushColor
I would also suggest saving the color like,
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, randomColor);
SetDCBrushColor(hdc, holdPreviousBrushColor);
So that you set the DC brush back to it's original color.
So now the code snippet in the question would look like,
#define INFINITY UINT64_MAX // You get the point. I am just calling it many times.
RECT rect = { 0 };
HBRUSH brush = (HBRUSH)::GetStockObject(DC_BRUSH);
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, RGB(0, 0, 0));
for(uint64_t i = 0; i < INFINITY; i++){
SetRect(&rect, 0, i, i, i + 1); // Right angle triangle btw.
SetDCBrushColor(hdc, /* Any color you want. */);
FillRect(hdc, &rect, brush);
}
SetDCBrushColor(hdc, holdPreviousBrushColor); // Setting the DC brush's color back to its original color
In order to make buttons, I create and render texture that way:
typedef struct{
SDL_Rect pos;
SDL_Texture* texture;
int hovered;
} button;
button getButton(int x, int y, char * label, TTF_Font* font, SDL_Color color){
button btn;
btn.hovered = false;
btn.pos.x = x;
btn.pos.y = y;
SDL_Surface* surface = TTF_RenderText_Solid(font, label, color);
btn.pos.w = surface->w;
btn.pos.h = surface->h;
btn.texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return btn;
}
void drawButton(button btn){
SDL_RenderCopyEx( renderer, btn.texture, NULL, &btn.pos, 0, NULL, SDL_FLIP_NONE);
if(btn.hovered){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0x00);
SDL_RenderDrawRect(renderer, &btn.pos);
}
The problem is that I get texture which size equals one of label. How can I increase texture pixel size without stretching it i.e. add blank spaces to the side of it?
Something like
void drawButton(button btn){
SDL_RenderCopyEx( renderer, btn.texture, NULL, &btn.pos, 0, NULL, SDL_FLIP_NONE);
if(btn.hovered){
int padding = 10;
SDL_Rect pos = {btn.pos.x - padding, btn.pos.y - padding,
btn.pos.w + 2*padding, btn.pos.h + 2*padding };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0x00);
SDL_RenderDrawRect(renderer, &pos);
}
}
That way only the size of the rectangle changes, obviously I just pulled 10 for the size of padding out of thin air, you'll want to pick something appropriate yourself.
Found a way to do it. To enlarge the texture you create surface representing background of button, then combine them:
button getButton(int x, int y, char * label, TTF_Font* font, SDL_Color color){
button btn;
btn.hovered = false;
btn.pos.x = x;
btn.pos.y = y;
SDL_Surface* surface = TTF_RenderText_Solid(font, label, color);
SDL_Surface* back = SDL_CreateRGBSurface(0, surface->w+10, surface->h+10,
32, 0, 0, 0, 0);// create a black background
SDL_Rect t = {5, 5, back->w, back->w}; // place in a background to place label
SDL_BlitSurface(surface, NULL, back, &t); // combining surfaces
btn.pos.w = back->w;
btn.pos.h = back->h;
btn.texture = SDL_CreateTextureFromSurface(renderer, back);
SDL_FreeSurface(surface);
return btn;
}
I've written a simple MFC application to draw a polygon in a dialog, the complete code being here.
The code opens a dialog, with default standard grey background, defines
std::vector<CPoint> m_LeftPolygon;
then does as following:
void DrawPitons(std::vector<CPoint> points, COLORREF rgbColor, HDC hDC)
{
static unsigned short XDiff[7] = { 5,4,4,3,2,1,0 },
YDiff[7] = { 0,1,2,3,4,4,5 };
for (unsigned pnt = 0; pnt < points.size(); pnt++)
{
SetPixel(hDC, points[pnt].x, points[pnt].y, rgbColor);
for (unsigned short i = 0; i < 7; i++)
{
SetPixel(hDC, points[pnt].x + XDiff[i], points[pnt].y + YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x + XDiff[i], points[pnt].y - YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x - XDiff[i], points[pnt].y + YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x - XDiff[i], points[pnt].y - YDiff[i], rgbColor);
}
}
}
void CROIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // device context for painting
HDC hDC = dc.GetSafeHdc();
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)),
oldPen = (HPEN)SelectObject(hDC, hPen);
SetROP2(hDC, R2_COPYPEN);
// Draw the polygon
if (m_LeftPolygon.size() > 1)
{
BOOL stat = MoveToEx(hDC, (int)m_LeftPolygon[0].x, (int)m_LeftPolygon[0].y, NULL);
for (size_t index = 1; index < m_LeftPolygon.size(); index++)
stat = LineTo(hDC, (int)m_LeftPolygon[index].x, (int)m_LeftPolygon[index].y);
stat = LineTo(hDC, (int)m_LeftPolygon[0].x, (int)m_LeftPolygon[0].y);
}
// Draw the control points
DrawPitons(m_LeftPolygon, RGB(255, 0, 0), hDC);
SelectObject(hDC, oldPen);
DeleteObject(hPen);
}
}
void CROIDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
m_LeftPolygon.push_back(point);
SendMessage(WM_PAINT);
}
For some reason, nothing is drawn, though I would expect the pitons and connecting lines to be drawn in red over the grey background.
What am I missing?
In C# and XNA, you can create a 1x1 texture like this:
Texture2D white_pixel;
white_pixel = new Texture2D(GraphicsDevice, 1, 1);
white_pixel.SetData<Color[]>(new Color{ Color.White });
// Sorry if I got the syntax wrong, it's been a while
Then later on, you can arbitrarily draw the pixel to any size and color by doing this:
spriteBatch.Begin();
spriteBatch.Draw(white_pixel, new Rectangle(0, 0, width, height), Color.Whatever);
spriteBatch.End();
What is the equivalent in SDL?
SDL_Texture *tex = nullptr;
SDL_CreateTexture(renderer,
Uint32 format, // What do I put here
int access, // and here
1
1);
// Not sure if this is correct
SDL_SetTextureColorMod(tex,
255,
255,
255)
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 10;
rect.h = 10;
SDL_RenderCopy(renderer, tex, nullptr, &rect);
SDL_PIXELFORMAT_RGB24/SDL_PIXELFORMAT_BGR24 for format and SDL_TEXTUREACCESS_STATIC for access would be a good start.
Or you could just draw a colored rectangle directly via SDL_SetRenderDrawColor() and SDL_RenderFillRect().
I have a window created with the WS_EX_LAYERED window style. I am currently drawing onto a memory bitmap using GDI+, and using UpdateLayeredWindow to update the graphical content of my layered window.
Here's a snippet of my code:
void Redraw(HWND hWnd, int width, int height) {
static bool floppy = true;
floppy = !floppy;
HDC hScreenDC = GetDC(HWND_DESKTOP);
HDC hMemDC = CreateCompatibleDC(hScreenDC);
HBITMAP hBmp = CreateCompatibleBitmap(hScreenDC, width, height);
HGDIOBJ hObj = SelectObject(hMemDC, hBmp);
Graphics gfx(hMemDC);
SolidBrush b(Color(254, (floppy ? 255 : 0), (floppy ? 0 : 255), 0));
gfx.FillRectangle(&b, Rect(0, 0, width, height));
BLENDFUNCTION blend;
blend.BlendOp = AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT src = { 0, 0 };
SIZE size;
size.cx = width;
size.cy = height;
Assert(UpdateLayeredWindow(
hWnd,
hScreenDC,
NULL,
&size,
hMemDC,
&src,
RGB(0, 0, 0),
&blend,
ULW_ALPHA
));
SelectObject(hMemDC, hObj);
DeleteObject(hBmp);
DeleteDC(hMemDC);
ReleaseDC(HWND_DESKTOP, hScreenDC);
}
When creating my SolidBrush, I specified the value of 254 for the alpha component. This results in a 99.6% opaque fill, which is not what I want.
When I specify 255 as the alpha component, there appears to be no fill; my window becomes completely transparent. This is an issue because I wish to draw shapes that are 100% opaque, but I also wish to draw some that aren't.
There seems to be some qwerks with FillRectangle. This becomes apparent when we observe that using FillEllipse with a SolidBrush whose alpha component is 255, results in the shape being rendered perfectly (opaque).
Here are two work-arounds that I came up with, which each solve the issue for me:
Call FillRectangle twice
SolidBrush b(Color(254, 255, 0, 0));
gfx.FillRectangle(&b, Rect(0, 0, width, height));
gfx.FillRectangle(&b, Rect(0, 0, width, height));
Since the same area is being filled twice, they will blend and create RGB(255, 0, 0) regardless of the content behind the window (it's now 100% opaque). I do not prefer this method, as it requires every rectangle to be drawn twice.
Use FillPolygon instead
Just as with FillEllipse, FillPolygon doesn't seem to have the colour issue, unless you call it like so:
SolidBrush b(Color(255, 255, 0, 0));
Point points[4];
points[0] = Point(0, 0);
points[1] = Point(width, 0);
points[2] = Point(width, height);
points[4] = Point(0, height);
gfx.FillPolygon(&b, points, 4); //don't copy and paste - this won't work
The above code will result in a 100% transparent window. I am guessing that this is either due to some form of optimisation that passes the call to FillRectangle instead. Or - most likely - there is some problem with FillPolygon, which is called by FillRectangle. However, if you add an extra Point to the array, you can get around it:
SolidBrush b(Color(255, 255, 0, 0));
Point points[5];
points[0] = Point(0, 0);
points[1] = Point(0, 0); //<-
points[2] = Point(width, 0);
points[3] = Point(width, height);
points[4] = Point(0, height);
gfx.FillPolygon(&b, points, 5);
The above code will indeed draw a 100% opaque shape, which fixes my problem.
UpdateLayeredWindow() requires a bitmap with pre-multiplied alpha:
Note that the APIs use premultiplied alpha, which means that the red,
green and blue channel values in the bitmap must be premultiplied with
the alpha channel value. For example, if the alpha channel value is x,
the red, green and blue channels must be multiplied by x and divided
by 0xff prior to the call.
You can use Bitmap::ConvertFormat() to convert a bitmap to pre-multiplied (the format is PixelFormat32bppPARGB).