<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ugly Function! Kann man das schöner machen?]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Funktion geschrieben die mir ein unausgefülltes Rechteck der größe 32x32 an einer X-Beliebigen X/Y Koordiante malt. Aber dieses viele MoveToEx und LineTo gefällt mir noch nicht so ganz. Gibt es da irgendwie andere Möglichkeiten den selben effekt zu erzielen mit weniger code?</p>
<pre><code class="language-cpp">void drawRect (HDC hdc, int x, int y)
{
    HPEN pen = CreatePen (PS_SOLID,1, RGB(255,0,0));

    SelectObject (hdc, pen);

    MoveToEx (hdc, x,    y,    NULL);
    LineTo   (hdc, x+32, y);

    MoveToEx (hdc, x+32, y,    NULL);
    LineTo   (hdc, x+32, y+32);

    MoveToEx (hdc, x+32, y+32, NULL);
    LineTo   (hdc, x,    y+32);

    MoveToEx (hdc, x,    y+32, NULL);
    LineTo   (hdc, x,    y);

    DeleteObject (pen);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/108746/ugly-function-kann-man-das-schöner-machen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 30 Jun 2026 06:39:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/108746.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 02 May 2005 18:45:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 18:45:04 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Funktion geschrieben die mir ein unausgefülltes Rechteck der größe 32x32 an einer X-Beliebigen X/Y Koordiante malt. Aber dieses viele MoveToEx und LineTo gefällt mir noch nicht so ganz. Gibt es da irgendwie andere Möglichkeiten den selben effekt zu erzielen mit weniger code?</p>
<pre><code class="language-cpp">void drawRect (HDC hdc, int x, int y)
{
    HPEN pen = CreatePen (PS_SOLID,1, RGB(255,0,0));

    SelectObject (hdc, pen);

    MoveToEx (hdc, x,    y,    NULL);
    LineTo   (hdc, x+32, y);

    MoveToEx (hdc, x+32, y,    NULL);
    LineTo   (hdc, x+32, y+32);

    MoveToEx (hdc, x+32, y+32, NULL);
    LineTo   (hdc, x,    y+32);

    MoveToEx (hdc, x,    y+32, NULL);
    LineTo   (hdc, x,    y);

    DeleteObject (pen);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/780412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780412</guid><dc:creator><![CDATA[n()()bi]]></dc:creator><pubDate>Mon, 02 May 2005 18:45:04 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 19:00:20 GMT]]></title><description><![CDATA[<p>Ich glaube du programmierst noch nicht so lange mit WinAPI, ich finde das sieht noch gut aus. Ich habe schon schlimmere Konstruktionen gehabt ....</p>
<p>Nein ich denke da kannst du nichts mehr optimieren, das sind geometrische Zeichenanweisung hintereinader geschrieben. Das ist so, bzw ich wüsste nicht wie man das ändern soll...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/780431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780431</guid><dc:creator><![CDATA[flammenvogel]]></dc:creator><pubDate>Mon, 02 May 2005 19:00:20 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 19:02:18 GMT]]></title><description><![CDATA[<p>flammenvogel schrieb:</p>
<blockquote>
<p>Ich glaube du programmierst noch nicht so lange mit WinAPI, ich finde das sieht noch gut aus. Ich habe schon schlimmere Konstruktionen gehabt ....</p>
</blockquote>
<p>Wie sähe denn eine bessere variante aus? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/780432</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780432</guid><dc:creator><![CDATA[n()()bi]]></dc:creator><pubDate>Mon, 02 May 2005 19:02:18 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 19:03:49 GMT]]></title><description><![CDATA[<p>besser?</p>
<p>Das ist doch gut ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/780436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780436</guid><dc:creator><![CDATA[flammenvogel]]></dc:creator><pubDate>Mon, 02 May 2005 19:03:49 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 19:04:17 GMT]]></title><description><![CDATA[<p>flammenvogel schrieb:</p>
<blockquote>
<p>Ich glaube du programmierst noch nicht so lange mit WinAPI</p>
</blockquote>
<p>du sicher auch nicht.<br />
--&gt; <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/linecurv_10bp.asp" rel="nofollow">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/linecurv_10bp.asp</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/780437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780437</guid><dc:creator><![CDATA[PolyLine]]></dc:creator><pubDate>Mon, 02 May 2005 19:04:17 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 19:18:10 GMT]]></title><description><![CDATA[<p>Oh ne, jetzt geht das los ...</p>
<p>Doch ich progge schon zwei jahre mit WinApi. Zugegben die Lösung mit PolyLine ist natürlich kürzer. Ich halte es aber für besser den alten Code beizubehalten.</p>
<p>Begründung: Ich bin es auch von OpenGl gewohnt Linen wie oben im Code zu definieren (eher Dreiecke in OpenGL). Wie immer führen viele Wege nach Rom ...</p>
<p>Außerdem beschäftige ich mich auch nicht so sehr mit Code Optimierung. Die Ressourcen die heute zur Verfügung stehen sind so größzügig, das man das Optimieren dem Kompiler überlassen sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/780450</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780450</guid><dc:creator><![CDATA[flammenvogel]]></dc:creator><pubDate>Mon, 02 May 2005 19:18:10 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 20:33:33 GMT]]></title><description><![CDATA[<p>Sind nicht zumindest die mittleren MoveToEx überflüssig:</p>
<blockquote>
<p>If LineTo succeeds, the current position is set to the specified ending point.</p>
</blockquote>
<p>Außerdem solltest du den alten Pen sichern und wieder herstellen, nachdem du deinen Pen ausgebraucht hast (man sollte in der Regel nie etwas löschen, was noch in einen DC selektiert ist)</p>
<p>Meine Lösung (wenn du LineTo beibehalten willst) sähe dann so aus:</p>
<pre><code class="language-cpp">void drawRect (HDC hdc, int x, int y)
{
    HPEN hPen    = CreatePen (PS_SOLID,1, RGB(255,0,0));
    HPEN hOldPen = (HPEN) SelectObject (hdc, hPen);

    MoveToEx (hdc, x,    y,    NULL);
    LineTo   (hdc, x+32, y);
    LineTo   (hdc, x+32, y+32);
    LineTo   (hdc, x,    y+32);
    LineTo   (hdc, x,    y);

    SelectObject (hdc, hOldPen);

    DeleteObject (hPen);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/780534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780534</guid><dc:creator><![CDATA[flenders]]></dc:creator><pubDate>Mon, 02 May 2005 20:33:33 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Mon, 02 May 2005 23:15:01 GMT]]></title><description><![CDATA[<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_6kth.asp" rel="nofollow">Rectangle</a> ?</p>
<pre><code class="language-cpp">BOOL Draw32x32Rect(HDC hdc, int x, int y)
{
    return Rectangle(hdc, x, y, x+32, y+32);
}
</code></pre>
<p>bzw. wenn das mit dem pen unbedingt da drinnen stehn soll:</p>
<pre><code class="language-cpp">BOOL Draw32x32Rect(HDC hdc, int x, int y)
{
    HPEN pen = CreatePen (PS_SOLID, 1, RGB(255,0,0));
    HGDIOBJ oldObj = SelectObject (hdc, pen);
    BOOL rc = Rectangle(hdc, x, y, x+32, y+32);
    DeleteObject(SelectObject(hdc,oldObj));
    return rc;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/780626</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780626</guid><dc:creator><![CDATA[k1ro]]></dc:creator><pubDate>Mon, 02 May 2005 23:15:01 GMT</pubDate></item><item><title><![CDATA[Reply to Ugly Function! Kann man das schöner machen? on Tue, 03 May 2005 09:05:22 GMT]]></title><description><![CDATA[<p>Jo, es ist allgemein nicht sehr sinnvoll eine so fest definierte Funktion zu schreiben. Ich meine was wird dann wenn du das nächste mal ein Rechteck der größe 64x64 brauchst? Hast du dann irgendwann:</p>
<p>Draw32x32Rect<br />
Draw64x64Rect<br />
...</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Wie schon erwähnt wurde ist auch das erstellen des Pens in der funktion keine wirklich glanzvolle Idee, und wenn schon, dann musst man wenigstens eine Möglichkeit haben die Farbe mit an die Funktion zu übergeben, sonst kommen wir irgendwann zu:</p>
<p>Draw32x32RectInGreen<br />
Draw32x32RectInBlue<br />
Draw32x32RectInPurple<br />
Draw64x64RectInGreen<br />
Draw64x64RectInBlue<br />
Draw64x64RectInPurple</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/780757</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/780757</guid><dc:creator><![CDATA[template]]></dc:creator><pubDate>Tue, 03 May 2005 09:05:22 GMT</pubDate></item></channel></rss>