Bild bewegen lassen

DuffCola

Mitglied
Wie kann ich, in meinem Programm vom Bild paint1 beim Druck auf Paint die Position wechdeln lassen.
Bei mier scheitern alle Versuche.
Das Bild behält immer die zuerst festgelegte Position...

Code:
//==================================================== MAIN_CPP ====================================================//
#include "StdAfx.h"
#include "functions.h"

//==================================================== Variablen ====================================================//
//==================================================== Var Main ====================================================//
int				iMainWindow_width			= GetSystemMetrics(SM_CXSCREEN);
int				iMainWindow_height		    = GetSystemMetrics(SM_CYSCREEN);
int				iMainWindow_hor				= 0;
int				iMainWindow_ver				= 0;
const LPCWSTR   szMainWindow_ClassName	    = (LPCWSTR)"WndClassEx";
LPCWSTR			szMainWindow_Title  		= (LPCWSTR)"Note Viewer";
WNDCLASSEX		MainWindow_WndClassEx;
HWND			MainWindow_hWnd;

//==================================================== Var Global ====================================================//
PAINTSTRUCT 	ps;
HDC 			hdc;

paint paint1;
int x= 0;
int y= 0;
LPCWSTR x2;

paint paint2;
paint paint3;

//==================================================== Funktionen ====================================================//
MSG	Global_msg;
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
bool InitWndClassEx( WNDCLASSEX *WndClassEx, HINSTANCE hInstance, const char* szClassName  );

//==================================================== Win Main ====================================================//
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{

//==================================================== REGISTRIERUNG ====================================================//

MainWindow_WndClassEx.cbSize		 = sizeof( WNDCLASSEX );
MainWindow_WndClassEx.style         = NULL;
MainWindow_WndClassEx.lpfnWndProc   = WndProc; 
MainWindow_WndClassEx.cbClsExtra    = NULL; 
MainWindow_WndClassEx.cbWndExtra    = NULL; 
MainWindow_WndClassEx.hInstance     = hInstance; 
MainWindow_WndClassEx.hIcon         = LoadIcon (NULL, IDI_WINLOGO); 
MainWindow_WndClassEx.hCursor       = LoadCursor (NULL, IDC_ARROW); 
MainWindow_WndClassEx.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); 
MainWindow_WndClassEx.lpszMenuName  = NULL; 
MainWindow_WndClassEx.lpszClassName = szMainWindow_ClassName;	  
MainWindow_WndClassEx.hIconSm		 = LoadIcon (NULL, IDI_WINLOGO);

if( !RegisterClassEx( &MainWindow_WndClassEx ) )
{
	MessageBox( NULL, L"MainWindow_WndClassEx failed.", L"Error", MB_OK | MB_ICONERROR );
	return false;
}

//==================================================== FENSTER ERSTELLEN ====================================================//
//==================================================== MainWindow_hWnd ====================================================//
MainWindow_hWnd = CreateWindowEx( WS_EX_TOPMOST,
				szMainWindow_ClassName,
				szMainWindow_Title,
				WS_POPUP | WS_VISIBLE,
				iMainWindow_hor,	
				iMainWindow_ver,    
				iMainWindow_width,	
				iMainWindow_height,	
				NULL,
				NULL,
				hInstance,
				NULL );
if( MainWindow_hWnd == NULL )
{
	MessageBox( NULL, L"Create MainWindow_hWnd failed.", L"Error", MB_OK );
	return 0;
}

while( GetMessage( &Global_msg, MainWindow_hWnd, NULL, NULL ) > NULL )
{
	TranslateMessage( &Global_msg );	 
	DispatchMessage( &Global_msg );	  
}//while
return 0;
}//WinMain

LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){	  
switch( msg )
{
//==================================================== Fenster Schließen ====================================================//
case WM_DESTROY: 
	PostQuitMessage(0);
break;
//==================================================== Tastenabfrage ====================================================//
case WM_KEYDOWN:
    switch(wParam)
    {
//==================================================== Escape ====================================================//
    case VK_ESCAPE:
	PostMessage(MainWindow_hWnd,WM_CLOSE,0,0);
    break;
//==================================================== Space ====================================================//
	case VK_SPACE:
	PostMessage(MainWindow_hWnd,WM_CLOSE,0,0);
	break;
    }//wParam
    break;
//==================================================== WM_CREATE ====================================================//
case WM_CREATE:	
	paint1.loadBmp(L"test.bmp", 400, 400);
	paint2.loadBmp(L"test2.bmp", 800, 400);
break;
//==================================================== Malen ====================================================//
case WM_PAINT:
	hdc = BeginPaint(hWnd, &ps);

	paint1.loadBmp(L"test.bmp", 400, 400);
	paint1.drawBmp(hWnd, ps, hdc);
	paint2.drawBmp(hWnd, ps, hdc);

    EndPaint(hWnd, &ps);
break;
//==================================================== Default ====================================================//
default : 
	return DefWindowProc( hWnd, msg, wParam, lParam );

}//switch(msg)
	return 0;
}//LRESULT








#include "StdAfx.h"
#include "functions.h"

void paint::loadBmp(LPCWSTR bmpPath, int hor, int ver)
{
	this->bmpHor=		hor;
	this->bmpVer =		ver;
	this->bmpPath =		bmpPath;

	this->hBitmap = (HBITMAP)LoadImage(this->hInstancePicture, bmpPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}

void paint::drawBmp(HWND hWnd, PAINTSTRUCT ps, HDC hdc)
{
this->hdcMem    = CreateCompatibleDC(hdc);
this->oldBitmap = SelectObject( this->hdcMem, this->hBitmap);
GetObject		(this->hBitmap, sizeof(this->bitmap), &this->bitmap);
BitBlt			(	   hdc,     this->bmpHor, this->bmpVer, this->bitmap.bmWidth, this->bitmap.bmHeight, this->hdcMem,0, -0, SRCCOPY);
SelectObject	(this->hdcMem,  this->oldBitmap);
DeleteDC		(this->hdcMem);



}






#include "StdAfx.h"

class paint
{
private:
HBITMAP			hBitmap;
BITMAP 			bitmap;
HDC 			hdcMem;
HGDIOBJ 		oldBitmap;
HINSTANCE		hInstancePicture;
LPCWSTR			bmpPath;
int				bmpHor;
int				bmpVer;
public:
void loadBmp(LPCWSTR bmpPath, int hor, int ver);
void drawBmp(HWND hWnd, PAINTSTRUCT ps, HDC hdc);
};
 
Hallo,

Ich hab kurz gegoogelt und folgendes bezüglich Bitmap laden gefunden: Microsoft Knowledge Base: Wie Sie LoadImage() verwenden, um eine BMP-Datei lesen

Das hier hab ich daraus gehackt:

C++:
BOOL LoadBitmapFromBMPFile(LPTSTR szFileName, HBITMAP *phBitmap,
		HPALETTE *phPalette)
{
	
	BITMAP bm;
	
	*phBitmap = NULL;
	*phPalette = NULL;
	
	// Use LoadImage() to get the image loaded into a DIBSection
	*phBitmap = (HBITMAP) LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
			LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
	if (*phBitmap == NULL) return FALSE;
	
	// Get the color depth of the DIBSection
	GetObject(*phBitmap, sizeof(BITMAP), &bm);
	// If the DIBSection is 256 color or less, it has a color table
	if ((bm.bmBitsPixel * bm.bmPlanes) <= 8)
	{
		HDC hMemDC;
		HBITMAP hOldBitmap;
		RGBQUAD rgb[256];
		LPLOGPALETTE pLogPal;
		WORD i;
		
		// Create a memory DC and select the DIBSection into it
		hMemDC = CreateCompatibleDC(NULL);
		hOldBitmap = (HBITMAP) SelectObject(hMemDC, *phBitmap);
		// Get the DIBSection's color table
		GetDIBColorTable(hMemDC, 0, 256, rgb);
		// Create a palette from the color tabl
		pLogPal = (LOGPALETTE *) malloc(
				sizeof(LOGPALETTE) + (256 * sizeof(PALETTEENTRY)));
		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = 256;
		for (i = 0; i < 256; i++)
		{
			pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
			pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
			pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
		}
		*phPalette = CreatePalette(pLogPal);
		// Clean up
		free(pLogPal);
		SelectObject(hMemDC, hOldBitmap);
		DeleteDC(hMemDC);
	}
	else   // It has no color table, so use a halftone palette
	{
		HDC hRefDC;
		
		hRefDC = GetDC(NULL);
		*phPalette = CreateHalftonePalette(hRefDC);
		ReleaseDC(NULL, hRefDC);
	}
	return TRUE;
	
}
//
//  FUNKTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  ZWECK:  Verarbeitet Meldungen vom Hauptfenster.
//
//  WM_COMMAND	- Verarbeiten des Anwendungsmenüs
//  WM_PAINT	- Zeichnen des Hauptfensters
//  WM_DESTROY	- Beenden-Meldung anzeigen und zurückgeben
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	static HBITMAP bmp;
	HDC hdcMem;
	BITMAP bitmap;
	HGDIOBJ oldBitmap;
	
	static int posx, posy;
	
	switch (message)
	{
		case WM_KEYDOWN:
			switch (wParam)
			{
				case VK_UP:
					posy--;
					InvalidateRect(hWnd, NULL, true);
					break;
				case VK_DOWN:
					posy++;
					InvalidateRect(hWnd, NULL, true);
					break;
				case VK_RIGHT:
					posx++;
					InvalidateRect(hWnd, NULL, true);
					break;
				case VK_LEFT:
					posx--;
					InvalidateRect(hWnd, NULL, true);
					break;
			}
			break;
			
		case WM_COMMAND:
			wmId = LOWORD(wParam);
			wmEvent = HIWORD(wParam);
			// Menüauswahl bearbeiten:
			switch (wmId)
			{
				case IDM_ABOUT:
					DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
					break;
				case IDM_EXIT:
					DestroyWindow(hWnd);
					break;
				default:
					return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			break;
		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HBITMAP hBitmap, hOldBitmap;
			HPALETTE hPalette, hOldPalette;
			HDC hDC, hMemDC;
			BITMAP bm;
			
			hDC = BeginPaint(hWnd, &ps);
			
			if (LoadBitmapFromBMPFile(L"C:\\Users\\Phil\\Desktop\\img\\npc.bmp",
					&hBitmap, &hPalette))
			{
				GetObject(hBitmap, sizeof(BITMAP), &bm);
				hMemDC = CreateCompatibleDC(hDC);
				hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
				hOldPalette = SelectPalette(hDC, hPalette, FALSE);
				RealizePalette(hDC);
				
				BitBlt(hDC, posx, posy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
						SRCCOPY);
				
				SelectObject(hMemDC, hOldBitmap);
				DeleteObject(hBitmap);
				SelectPalette(hDC, hOldPalette, FALSE);
				DeleteObject(hPalette);
			}
			EndPaint(hWnd, &ps);
			
		}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

Den Dateiname für das Bild musst du noch ändern!

mfg. Poseidon

PS.: Via OpenGL kann man wesentlich eleganter Bilder bewegen lassen =)
 
Zuletzt bearbeitet von einem Moderator:
Zurück