<?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[HWNd HDC bei Konsolenprogramm?]]></title><description><![CDATA[<p>Hallo, ich möchte ein BMP berarbeiten und habe Code, der in einem Programm mit richtigem Fenster funktioniert.</p>
<p>Da ich die *.bmp nur lade, Daten manipulieren und wieder speichern möchte und es auch nicht anzeigen möchte, wollte ich es in einer Konsolenanwendung schreiben.</p>
<pre><code class="language-cpp">void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) 
 { 
    HANDLE hf;							//file handle 
    BITMAPFILEHEADER hdr;				//bitmap file-header 
    PBITMAPINFOHEADER pbih;				//bitmap info-header 
    LPBYTE lpBits;						//Speicher pointer 
    DWORD dwTotal;						//totale Anzahl von bytes
    DWORD cb;							//Zusätzliche Anzahl von Bytes
    BYTE *hp;							//Byte pointer 
    DWORD dwTmp; 

    pbih = (PBITMAPINFOHEADER) pbi; 
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih-&gt;biSizeImage);

    if (!lpBits) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Holen der color table (RGBQUAD Array) und der Bits 
										//(array von palette indices) von der &quot;DIB&quot;. 
    if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih-&gt;biHeight, lpBits, pbi, 
        DIB_RGB_COLORS)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Erzeuge die .BMP Datei. 
    hf = CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
    if (hf == INVALID_HANDLE_VALUE) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
    hdr.bfType = 0x4d42;				//0x42 = &quot;B&quot; 0x4d = &quot;M&quot; 
										//berechne die Größe von der gesamten Datei. 
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                 pbih-&gt;biSize + pbih-&gt;biClrUsed 
                 * sizeof(RGBQUAD) + pbih-&gt;biSizeImage); 
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

										// Berechne den Offset des Arrays von Farb indices. 
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                    pbih-&gt;biSize + pbih-&gt;biClrUsed 
                    * sizeof (RGBQUAD); 

										//Kopiere die BITMAPFILEHEADER in die .BMP Datei. 
    if (!WriteFile(hf, (LPVOID) &amp;hdr, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &amp;dwTmp,  NULL)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
										//Kopiere die BITMAPINFOHEADER und RGBQUAD Arrays in die datei. 
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                  + pbih-&gt;biClrUsed * sizeof (RGBQUAD), 
                  (LPDWORD) &amp;dwTmp, ( NULL)))
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    dwTotal = cb = pbih-&gt;biSizeImage;	//kopiere das Array der color indices in die .BMP Datei.
    hp = lpBits; 
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &amp;dwTmp,NULL)) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

	if (!CloseHandle(hf))				//Schließe die .BMP Datei. 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    GlobalFree((HGLOBAL)lpBits);		//gib den Speicher frei. 
}
</code></pre>
<p>Nun braucht aber die Methode (oben zu sehen) HWND und HDC.<br />
Wie bekomme ich diese in einem Konsolemprogramm?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/194781/hwnd-hdc-bei-konsolenprogramm</link><generator>RSS for Node</generator><lastBuildDate>Tue, 30 Jun 2026 06:40:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/194781.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Oct 2007 18:21:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HWNd HDC bei Konsolenprogramm? on Wed, 10 Oct 2007 18:21:34 GMT]]></title><description><![CDATA[<p>Hallo, ich möchte ein BMP berarbeiten und habe Code, der in einem Programm mit richtigem Fenster funktioniert.</p>
<p>Da ich die *.bmp nur lade, Daten manipulieren und wieder speichern möchte und es auch nicht anzeigen möchte, wollte ich es in einer Konsolenanwendung schreiben.</p>
<pre><code class="language-cpp">void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) 
 { 
    HANDLE hf;							//file handle 
    BITMAPFILEHEADER hdr;				//bitmap file-header 
    PBITMAPINFOHEADER pbih;				//bitmap info-header 
    LPBYTE lpBits;						//Speicher pointer 
    DWORD dwTotal;						//totale Anzahl von bytes
    DWORD cb;							//Zusätzliche Anzahl von Bytes
    BYTE *hp;							//Byte pointer 
    DWORD dwTmp; 

    pbih = (PBITMAPINFOHEADER) pbi; 
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih-&gt;biSizeImage);

    if (!lpBits) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Holen der color table (RGBQUAD Array) und der Bits 
										//(array von palette indices) von der &quot;DIB&quot;. 
    if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih-&gt;biHeight, lpBits, pbi, 
        DIB_RGB_COLORS)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Erzeuge die .BMP Datei. 
    hf = CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
    if (hf == INVALID_HANDLE_VALUE) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
    hdr.bfType = 0x4d42;				//0x42 = &quot;B&quot; 0x4d = &quot;M&quot; 
										//berechne die Größe von der gesamten Datei. 
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                 pbih-&gt;biSize + pbih-&gt;biClrUsed 
                 * sizeof(RGBQUAD) + pbih-&gt;biSizeImage); 
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

										// Berechne den Offset des Arrays von Farb indices. 
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                    pbih-&gt;biSize + pbih-&gt;biClrUsed 
                    * sizeof (RGBQUAD); 

										//Kopiere die BITMAPFILEHEADER in die .BMP Datei. 
    if (!WriteFile(hf, (LPVOID) &amp;hdr, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &amp;dwTmp,  NULL)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
										//Kopiere die BITMAPINFOHEADER und RGBQUAD Arrays in die datei. 
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                  + pbih-&gt;biClrUsed * sizeof (RGBQUAD), 
                  (LPDWORD) &amp;dwTmp, ( NULL)))
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    dwTotal = cb = pbih-&gt;biSizeImage;	//kopiere das Array der color indices in die .BMP Datei.
    hp = lpBits; 
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &amp;dwTmp,NULL)) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

	if (!CloseHandle(hf))				//Schließe die .BMP Datei. 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    GlobalFree((HGLOBAL)lpBits);		//gib den Speicher frei. 
}
</code></pre>
<p>Nun braucht aber die Methode (oben zu sehen) HWND und HDC.<br />
Wie bekomme ich diese in einem Konsolemprogramm?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1382318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1382318</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 10 Oct 2007 18:21:34 GMT</pubDate></item><item><title><![CDATA[Reply to HWNd HDC bei Konsolenprogramm? on Wed, 10 Oct 2007 18:47:17 GMT]]></title><description><![CDATA[<p>hwnd wird überhaupt nicht verwendet?<br />
Für hdc kannste vermutlich das Screen-DC nehmen, welches du über GetDC(NULL) bekommst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1382333</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1382333</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Wed, 10 Oct 2007 18:47:17 GMT</pubDate></item><item><title><![CDATA[Reply to HWNd HDC bei Konsolenprogramm? on Wed, 10 Oct 2007 19:00:05 GMT]]></title><description><![CDATA[<p><em>GetConsoleWindow()</em> --&gt; HWND (To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later.)<br />
<em>GetDC()</em> --&gt; HDC</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1382343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1382343</guid><dc:creator><![CDATA[gosha16]]></dc:creator><pubDate>Wed, 10 Oct 2007 19:00:05 GMT</pubDate></item><item><title><![CDATA[Reply to HWNd HDC bei Konsolenprogramm? on Thu, 11 Oct 2007 08:38:56 GMT]]></title><description><![CDATA[<p>Danke, es funktioniert jetzt.</p>
<p>Das Auslesen der Bilddaten und das Speichern der Datei scheinen mir in meiner Version doch extrem umständlich. Ich benutze folgende 3 Methoden aus meiner &quot;Bilder&quot; Klasse. (Ich habe mir die Methoden größtenteils aus der MSDN zusammengesucht)</p>
<p>Gibt es eine einfachere Möglichkeit als diese:</p>
<pre><code class="language-cpp">PBITMAPINFO Bilder::CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
{ 
    BITMAP bmp; 
    PBITMAPINFO pbmi; 
    WORD    cClrBits; 

																	//Hole das Bitmap color (farb) format, Breite, und höhe. 
    if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&amp;bmp)) 
	{}

																	// Convertiere das color (farb) Format zu einer Anzahl von Bits. 
    cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
    if (cClrBits == 1) 
        cClrBits = 1; 
    else if (cClrBits &lt;= 4) 
        cClrBits = 4; 
    else if (cClrBits &lt;= 8) 
        cClrBits = 8; 
    else if (cClrBits &lt;= 16) 
        cClrBits = 16; 
    else if (cClrBits &lt;= 24) 
        cClrBits = 24; 
    else cClrBits = 32; 

																	//Belege Speicher für die BITMAPINFO Structur. (Diese Struktur 
																	//enthält eine BITMAPINFOHEADER Struktur und ein Array von RGB QUAD 
																	//Daten Structuren.) 

     if (cClrBits != 24) 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER) + 
                    sizeof(RGBQUAD) * (1&lt;&lt; cClrBits)); 

																	// Es ist kein RGBQUAD Array für das 24-bit-per-pixel Format vorhanden 

     else 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER)); 

																	// Initialisiere die Felder der BITMAPINFO Struktur. 

    pbmi-&gt;bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pbmi-&gt;bmiHeader.biWidth = bmp.bmWidth; 
    pbmi-&gt;bmiHeader.biHeight = bmp.bmHeight; 
    pbmi-&gt;bmiHeader.biPlanes = bmp.bmPlanes; 
    pbmi-&gt;bmiHeader.biBitCount = bmp.bmBitsPixel; 
    if (cClrBits &lt; 24) 
        pbmi-&gt;bmiHeader.biClrUsed = (1&lt;&lt;cClrBits); 

																	//Wenn die Bitmap nicht komprimiert ist, setze das BI_RGB flag. 
    pbmi-&gt;bmiHeader.biCompression = BI_RGB; 

																	//Berechne die Anzahl von Bytes in dem Color indices Array 
																	//und speichere das Ergebnis in biSizeImage. 
    pbmi-&gt;bmiHeader.biSizeImage = ((pbmi-&gt;bmiHeader.biWidth * cClrBits +31) &amp; ~31) /8
    * pbmi-&gt;bmiHeader.biHeight; 
																	// Sete biClrImportant auf 0, um anzuzeigen, dass alle device farben wichtig sind

     pbmi-&gt;bmiHeader.biClrImportant = 0; 
     return pbmi; 
 } 

void Bilder::CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) 
 { 
    HANDLE hf;							//file handle 
    BITMAPFILEHEADER hdr;				//bitmap file-header 
    PBITMAPINFOHEADER pbih;				//bitmap info-header 
    LPBYTE lpBits;						//Speicher pointer 
    DWORD dwTotal;						//totale Anzahl von bytes
    DWORD cb;							//Zusätzliche Anzahl von Bytes
    BYTE *hp;							//Byte pointer 
    DWORD dwTmp; 

    pbih = (PBITMAPINFOHEADER) pbi; 
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih-&gt;biSizeImage);

    if (!lpBits) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Holen der color table (RGBQUAD Array) und der Bits 
										//(array von palette indices) von der &quot;DIB&quot;. 
    if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih-&gt;biHeight, lpBits, pbi, 
        DIB_RGB_COLORS)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

										//Erzeuge die .BMP Datei. 
    hf = CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
    if (hf == INVALID_HANDLE_VALUE) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
    hdr.bfType = 0x4d42;				//0x42 = &quot;B&quot; 0x4d = &quot;M&quot; 
										//berechne die Größe von der gesamten Datei. 
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                 pbih-&gt;biSize + pbih-&gt;biClrUsed 
                 * sizeof(RGBQUAD) + pbih-&gt;biSizeImage); 
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

										// Berechne den Offset des Arrays von Farb indices. 
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                    pbih-&gt;biSize + pbih-&gt;biClrUsed 
                    * sizeof (RGBQUAD); 

										//Kopiere die BITMAPFILEHEADER in die .BMP Datei. 
    if (!WriteFile(hf, (LPVOID) &amp;hdr, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &amp;dwTmp,  NULL)) 
    {}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //
										//Kopiere die BITMAPINFOHEADER und RGBQUAD Arrays in die datei. 
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                  + pbih-&gt;biClrUsed * sizeof (RGBQUAD), 
                  (LPDWORD) &amp;dwTmp, ( NULL)))
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    dwTotal = cb = pbih-&gt;biSizeImage;	//kopiere das Array der color indices in die .BMP Datei.
    hp = lpBits; 
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &amp;dwTmp,NULL)) 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

	if (!CloseHandle(hf))				//Schließe die .BMP Datei. 
	{}									//// Hier fehlt noch eine Fehlermeldung /Fehlerbehandlung //

    GlobalFree((HGLOBAL)lpBits);		//gib den Speicher frei. 
}

HBITMAP Bilder::erzeuge_Manipuliertes_Bild( LPCWSTR file)						{ 
    int width, height;													//Variablen
    HBITMAP bitmap; 
    HBITMAP newBitmap=NULL; 

    if(!file) return NULL;												//Wenn der angegebene Pfad nicht existiert, liefere NULL als HBITMAP zurück

    bitmap= (HBITMAP)LoadImage(0,file,IMAGE_BITMAP, 0, 0,				//Lade das Bitmap in HBITMAP
                 LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_CREATEDIBSECTION);   

    BITMAP bm;															
	GetObject(bitmap, sizeof(BITMAP), &amp;bm);								//Kopiert das BITMAP aus dem HBITMAP

    width=bm.bmWidth;													//Breite und hoehe auslesen
    height=bm.bmHeight; 

    HDC SrcDC=CreateCompatibleDC(NULL); 								//Passenden HDC erzeugen
    SelectObject(SrcDC,bitmap);											//OBjekt selektieren

   DWORD col1; 
    for(int y=0; y&lt;height; y++)											//Durchlaufe alle Pixel
	{
        for(int x=0; x&lt;width; x++) 
        { 
			col1=GetPixel(SrcDC,x,y);									//Pixelfarben auslesen
			byte r,g,b;
			r=GetRValue(col1);
			g=GetGValue(col1);
			b=GetBValue(col1);

			int gesamt = r+g+b;											//Wenn die Summe der Farben eines Pixels kleiner 72 ist
			if(gesamt&lt;72) 
			{
				SetPixel(SrcDC,x,y,RGB(0,255,0));						//setzte ein braunes Pixel
			}
			else 
			{
				SetPixel(SrcDC,x,y,RGB(139,69,19));						//sonst ein rotes
			}
       } 
	}
    DeleteDC(SrcDC);													//HDC freigeben
	return bitmap;														//HBITMAP zurückgeben

}
</code></pre>
<p>Ich rufe dies in der main Methode folgendermaßen auf:</p>
<pre><code class="language-cpp">int main()
{

HWND hWnd=GetConsoleWindow();
HDC hDC=GetDC(NULL); 
Bilder b = Bilder();
HBITMAP h= b.erzeuge_Manipuliertes_Bild(L&quot;1.bmp&quot;);
b.CreateBMPFile(hWnd,L&quot;2.bmp&quot;,b.CreateBitmapInfoStruct(hWnd,h),h,hDC);
return 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1382623</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1382623</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Thu, 11 Oct 2007 08:38:56 GMT</pubDate></item></channel></rss>