NO

Author Topic: Color inside the "Ellipse"  (Read 8067 times)

Offline alderman2

  • Member
  • *
  • Posts: 6
Color inside the "Ellipse"
« on: December 17, 2022, 10:29:07 PM »
I need to use the "Ellipse" function in a program I'm working on.  I can color the edges around but not inside the circle.  I've tried everything I can find on how to but nothing has worked yet.  The others I've seen do this use add-on libraries, but I don't want to do that.
 Anyone know the easiest way to color the circle surface with any color?

This is what it looks like when I can color the border around the circle, but how do you paint in the circle:

HPEN hpen,hpenOld;

   PAINTSTRUCT    ps ;
   BeginPaint(hwnd, &ps);



    hpen=CreatePen(PS_SOLID,0,color);
    hpenOld=SelectObject(hDC,hpen);
 
      Ellipse( hDC, 400, 300,200, 200);

    SelectObject(hDC,hpenOld);
    DeleteObject(hpen);

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Color inside the "Ellipse"
« Reply #1 on: December 17, 2022, 10:43:23 PM »
May the source be with you

Offline alderman2

  • Member
  • *
  • Posts: 6
Re: Color inside the "Ellipse"
« Reply #2 on: December 17, 2022, 11:14:18 PM »
Using Filled Shapes

I solved it!!!  After two weeks of trying, and when I finally post the question here, I found the solution!

This is how it works:
Code: [Select]
HDC hDC;
HBRUSH hBrush;

hDC = GetDC(hwnd);

hBrush = CreateSolidBrush (RGB(0, 0, 255));

SelectObject (hDC, hBrush);


Ellipse( hDC, 300, 300, 100, 100);


Is there something missing in the code?

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Color inside the "Ellipse"
« Reply #3 on: December 18, 2022, 12:42:59 AM »
Is there something missing in the code?

Impossible to say because you only shared a fragment of code.
The function below was copied from the BCX runtime library.
It might provide clues that you can use.

Code: [Select]
  int BCX_Circle (HWND Wnd, int X, int Y, int R, int Pen, int Fill, HDC DrawHDC)
        {
          int a,b=0;
          if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1;
          HPEN   hNPen=CreatePen(PS_SOLID,1,Pen);
          HPEN   hOPen=(HPEN)SelectObject(DrawHDC,hNPen);
          HBRUSH hOldBrush;
          HBRUSH hNewBrush;
          if (Fill)
          {
            hNewBrush=CreateSolidBrush(Pen);
            hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
          }
          else
          {
            hNewBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
            hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
          }
          a = Ellipse(DrawHDC,X-R,Y+R,X+R,Y-R);
          DeleteObject(SelectObject(DrawHDC,hOPen));
          DeleteObject(SelectObject(DrawHDC,hOldBrush));
          if (b) ReleaseDC(Wnd,DrawHDC);
          return a;




Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Color inside the "Ellipse"
« Reply #4 on: December 18, 2022, 12:27:20 PM »
Hi MrBcx,

Code: [Select]
if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1;

This code snippet works? I've never seen this syntax, I surmise if no HDC it will get one, then it will set b equal to 1
as a flag to later allow the release of the DC;  but I've never run across this use of a comma..... I would have had to write
Code: [Select]
if (!DrawHDC)
  { DrawHDC=GetDC(Wnd) ; b=1;}

Are multiple commas allowed too? As in
 if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1,c=2,d=3;

John Z

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Color inside the "Ellipse"
« Reply #5 on: December 18, 2022, 01:33:46 PM »
Are multiple commas allowed too? As in
 if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1,c=2,d=3;

Short answer: Yes.

From The C standard:
Quote
§6.5.17 Comma operator
Syntax

expression:
    assignment-expression
    expression , assignment-expression


Semantics
The left operand of a comma operator is evaluated as a void expression; there is a sequence point
between its evaluation and that of the right operand. Then the right operand is evaluated; the result
has its type and value.)

EXAMPLE As indicated by the syntax, the comma operator (as described in this subclause) cannot appear in contexts where
a comma is used to separate items in a list (such as arguments to functions or lists of initializers). On the other hand, it can be
used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the
function call
 
Code: [Select]
   f(a, (t=3, t+2), c)
the function has three arguments, the second of which has the value 5.

Basically it acts as a separator making void the former expression so the last result is the last expression of the serie.
But the very important part is that it creates a sequence point between expressions. A sequence point is a delimiter for instruction sequencing. To make the long story short the compiler must follow the sequence of instructions between sequencing points, and cannot optimize by changing execution order.

This code snippet works? I've never seen this syntax, I surmise if no HDC it will get one, then it will set b equal to 1
as a flag to later allow the release of the DC;  but I've never run across this use of a comma..... I would have had to write
Code: [Select]
    if (!DrawHDC)
        { DrawHDC=GetDC(Wnd) ; b=1;}
Yes this is the equivalent form.

If you want to know more about sequence points refer to to "§5.1.2.3 Program execution" of the C standard.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Color inside the "Ellipse"
« Reply #6 on: December 18, 2022, 01:41:47 PM »
Thanks frankie!

What a great day, learned something new (to me at least)!  :)

Thanks for the reference I'll read up on it.  I had done an internet
search before posting but came up with nothing, probably because
I didn't phrase it correctly.

Much appreciated,
John Z

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Color inside the "Ellipse"
« Reply #7 on: December 18, 2022, 02:20:56 PM »
John,

Sorry for the delay ... I got busy yesterday, so I'm happy that Frankie provided a helpful reply.
I discovered the comma operator several years ago and thought to myself, "That's gonna be useful!"
and it has been, including being incorporated into BCX in several locations, as you've observed.
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Color inside the "Ellipse"
« Reply #8 on: December 19, 2022, 02:21:28 PM »
No worries MrBcx!  Great to see an interesting post  :)

Everyone can't be on every day.

John Z

Offline alderman2

  • Member
  • *
  • Posts: 6
Re: Color inside the "Ellipse"
« Reply #9 on: December 23, 2022, 05:06:50 PM »
Is there something missing in the code?

Impossible to say because you only shared a fragment of code.
The function below was copied from the BCX runtime library.
It might provide clues that you can use.

Code: [Select]
  int BCX_Circle (HWND Wnd, int X, int Y, int R, int Pen, int Fill, HDC DrawHDC)
        {
          int a,b=0;
          if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1;
          HPEN   hNPen=CreatePen(PS_SOLID,1,Pen);
          HPEN   hOPen=(HPEN)SelectObject(DrawHDC,hNPen);
          HBRUSH hOldBrush;
          HBRUSH hNewBrush;
          if (Fill)
          {
            hNewBrush=CreateSolidBrush(Pen);
            hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
          }
          else
          {
            hNewBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
            hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
          }
          a = Ellipse(DrawHDC,X-R,Y+R,X+R,Y-R);
          DeleteObject(SelectObject(DrawHDC,hOPen));
          DeleteObject(SelectObject(DrawHDC,hOldBrush));
          if (b) ReleaseDC(Wnd,DrawHDC);
          return a;
BCX seems like an interesting code library!
 Where to find it?

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Color inside the "Ellipse"
« Reply #10 on: December 23, 2022, 05:21:04 PM »
it is part of the "Basic to C Translator" program that MrBCX promotes and maintains at https://www.bcxbasiccoders.com/
jump over and check it out....

I don't think the code library is separate but I could be mistaken.

John Z

Offline Marco

  • Member
  • *
  • Posts: 45
Re: Color inside the "Ellipse"
« Reply #11 on: December 24, 2022, 10:29:03 AM »
Just a side note: it's thanks to Kevin Diggins and its excellent BCX project that I started using Pelles C (year 2006).

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Color inside the "Ellipse"
« Reply #12 on: December 26, 2022, 05:33:35 PM »
Just a side note: it's thanks to Kevin Diggins and its excellent BCX project that I started using Pelles C (year 2006).

Thank you for the nod Marco.

BCX development continues to this day.
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Color inside the "Ellipse"
« Reply #13 on: December 27, 2022, 06:24:14 AM »
Just a side note: it's thanks to Kevin Diggins and its excellent BCX project that I started using Pelles C (year 2006).
  Well Marco I guess we share something in common.  I came by Pelles C the same way back in 2006!  I learned a lot from BCX generated code back then.
No one cares how much you know,
until they know how much you care.

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Color inside the "Ellipse"
« Reply #14 on: December 28, 2022, 05:39:07 AM »
Just a side note: it's thanks to Kevin Diggins and its excellent BCX project that I started using Pelles C (year 2006).
  Well Marco I guess we share something in common.  I came by Pelles C the same way back in 2006!  I learned a lot from BCX generated code back then.

Sharing code and creating apps that get used by others brings a unique joy that only programmers ever get to experience.

Be fearless, share some code, share an app, ask a question.  You never know what new ideas might spring forth.

Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com