C language > Windows questions

Color inside the "Ellipse"

<< < (2/3) > >>

frankie:

--- Quote from: John Z on December 18, 2022, 12:27:20 PM ---Are multiple commas allowed too? As in
 if (!DrawHDC) DrawHDC=GetDC(Wnd),b=1,c=2,d=3;

--- End quote ---

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: ---   f(a, (t=3, t+2), c)

--- End code ---
the function has three arguments, the second of which has the value 5.

--- End quote ---

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.


--- Quote from: John Z on December 18, 2022, 12:27:20 PM ---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: ---    if (!DrawHDC)
        { DrawHDC=GetDC(Wnd) ; b=1;}

--- End code ---

--- End quote ---
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.

John Z:
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

MrBcx:
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.

John Z:
No worries MrBcx!  Great to see an interesting post  :)

Everyone can't be on every day.

John Z

alderman2:

--- Quote from: MrBcx on December 18, 2022, 12:42:59 AM ---
--- Quote from: alderman2 on December 17, 2022, 11:14:18 PM ---Is there something missing in the code?

--- End quote ---

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: ---  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;

--- End code ---

--- End quote ---
BCX seems like an interesting code library!
 Where to find it?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version