连接图片(二),实现根据鼠标方向,平滑移动

/*----------------------------------------
BRICKS1.C -- LoadBitmap Demonstration
(c) Charles Petzold, 1998
----------------------------------------*/

#include <windows.h>
#include <TCHAR.H>
#include <stdio.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName [] = TEXT ("Bricks1") ;
	HWND         hwnd ;
	MSG          msg ;
	WNDCLASS     wndclass ;

	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = WndProc ;
	wndclass.cbClsExtra    = 0 ;
	wndclass.cbWndExtra    = 0 ;
	wndclass.hInstance     = hInstance ;
	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
	//wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
	wndclass.hbrBackground = NULL ;
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName = szAppName ;

	if (!RegisterClass (&wndclass))
	{
		MessageBox (NULL, TEXT ("This program requires Windows NT!"),
			szAppName, MB_ICONERROR) ;
		return 0 ;
	}

	hwnd = CreateWindow (szAppName, 
		TEXT ("LoadBitmap Demo"), 
		WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, 
		NULL,
		hInstance, 
		NULL
		) ;

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
	}
	return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HBITMAP hBitmap,hBitmap2,memBM;
	static int     cxClient, cyClient, cxSource, cySource,cxSource2, cySource2 ,cxSourceresult;
	BITMAP         bitmap,bitmap2 ,bitmap3;
	static HDC            hdc, hdcMem = NULL,hdcMem2 ,hdcMem3;
	static HINSTANCE      hInstance ;

	static long            lDistance = -10,lSrcLocation = 0;
	static int            xMouseLocationfirst ;
	static int            xMouseLocationsecond;
	PAINTSTRUCT    ps ;
	POINT          MousePointFirst;
	POINT          MousePointSecond;
	RECT           ClientRect;
	static BOOL    IsInitialize = TRUE;
    static BOOL    IsRight = TRUE; 
	static BOOL    IsShowResultPage = FALSE;
	switch (message)
	{
	case WM_CREATE:
		hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
		hBitmap = LoadBitmap (hInstance, TEXT ("IDB_BITMAP5")) ;  //载入图片资源(注意,应该先将图片添加到工程里)
		hBitmap2 = LoadBitmap (hInstance,TEXT("IDB_BITMAP3"));
		return 0 ;

	case WM_SIZE:
		cxClient = LOWORD (lParam) ; //获得客户区大小
		cyClient = HIWORD (lParam) ;		
		return 0 ;

	case WM_PAINT:
		hdc = BeginPaint (hwnd, &ps) ;

		//判断是否要显示最终的页面
		if(IsShowResultPage)
		{
			//创建兼容DC
			hdcMem = CreateCompatibleDC (hdc) ;  
			hdcMem2 = CreateCompatibleDC (hdc) ;
			hdcMem3 = CreateCompatibleDC (hdc) ;
			SelectObject(hdcMem,hBitmap);
			SelectObject(hdcMem2,hBitmap2);

            //获得图片对象,从而获得图片的 长 宽(x,y)
			GetObject (hBitmap, sizeof (BITMAP), &bitmap) ; 
			GetObject (hBitmap2, sizeof (BITMAP), &bitmap2) ;
			cxSource = bitmap.bmWidth ;
			cySource = bitmap.bmHeight ;
			cxSource2 = bitmap2.bmWidth ;
			cySource2 = bitmap2.bmHeight ;

			//创建与指定的设备环境相关的设备兼容的位图,注意x,应该为2张图片的x的和,获得兼容位图的x,y
			memBM   = CreateCompatibleBitmap(hdc,cxSource+cxSource2,cySource); 
			SelectObject(hdcMem3,memBM);
			GetObject (memBM, sizeof (BITMAP), &bitmap3);
			cxSourceresult = bitmap3.bmWidth;

			//将要连接的2张图片,分别依次加入到兼容位图 memBM 中去,注意BitBlt 参数的设置(x)
			BitBlt(hdcMem3,0,0,cxSource,cySource,hdcMem,0,0,SRCCOPY);
			DeleteDC(hdcMem);
			BitBlt(hdcMem3,cxSource,0,cxSource2,cySource2,hdcMem2,0,0,SRCCOPY);
			DeleteDC(hdcMem2);

			//if(lSrcLocation >= cxSource/2)
			if(!IsRight)
			{
				while(lSrcLocation <= cxSource )
				{
					BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,lSrcLocation,0,SRCCOPY);
					lSrcLocation = lSrcLocation + 10;
					Sleep(1);
					
				}
				IsRight = TRUE;


			}
			else
			{
				while((lSrcLocation >= 0)/* && (lSrcLocation <= cxSource/2 )*/)
				{
					BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,lSrcLocation,0,SRCCOPY);
					lSrcLocation = lSrcLocation - 10;
					Sleep(1);					
				}
				IsRight = FALSE;
			}

			DeleteObject(memBM);
			IsShowResultPage = FALSE;

		}

		else

		{

			//判断是否是刚启动程序,初始化客户区
			if(IsInitialize == TRUE)
			{
				hdcMem = CreateCompatibleDC (hdc) ;
				hdcMem2 = CreateCompatibleDC (hdc) ;
				hdcMem3 = CreateCompatibleDC (hdc) ;
				SelectObject(hdcMem,hBitmap);
				SelectObject(hdcMem2,hBitmap2);


				GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
				GetObject (hBitmap2, sizeof (BITMAP), &bitmap2) ;

				cxSource = bitmap.bmWidth ;
				cySource = bitmap.bmHeight ;

				cxSource2 = bitmap2.bmWidth ;
				cySource2 = bitmap2.bmHeight ;

				memBM   = CreateCompatibleBitmap(hdc,cxSource+cxSource2,cySource);
				SelectObject(hdcMem3,memBM);

				GetObject (memBM, sizeof (BITMAP), &bitmap3);

				cxSourceresult = bitmap3.bmWidth;

				BitBlt(hdcMem3,0,0,cxSource,cySource,hdcMem,0,0,SRCCOPY);
				DeleteDC(hdcMem);

				BitBlt(hdcMem3,cxSource,0,cxSource2,cySource2,hdcMem2,0,0,SRCCOPY);
				DeleteDC(hdcMem2);


				BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,0,0,SRCCOPY);

				DeleteObject(memBM);
			}
			else
			{	
				//TCHAR szBuf[MAX_PATH] = {0};
				lSrcLocation = lSrcLocation+lDistance;

				//wsprintf(szBuf,L"xRE = %d",lSrcLocation);
				//OutputDebugString(szBuf);

				hdcMem = CreateCompatibleDC (hdc) ;
				hdcMem2 = CreateCompatibleDC (hdc) ;
				hdcMem3 = CreateCompatibleDC (hdc) ;
				SelectObject(hdcMem,hBitmap);
				SelectObject(hdcMem2,hBitmap2);


				GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
				GetObject (hBitmap2, sizeof (BITMAP), &bitmap2) ;

				cxSource = bitmap.bmWidth ;
				cySource = bitmap.bmHeight ;

				cxSource2 = bitmap2.bmWidth ;
				cySource2 = bitmap2.bmHeight ;

				memBM   = CreateCompatibleBitmap(hdc,cxSource+cxSource2,cySource);
				SelectObject(hdcMem3,memBM);



				BitBlt(hdcMem3,0,0,cxSource,cySource,hdcMem,0,0,SRCCOPY);
				DeleteDC(hdcMem);

				BitBlt(hdcMem3,cxSource,0,cxSource2,cySource2,hdcMem2,0,0,SRCCOPY);
				DeleteDC(hdcMem2);

				if(lSrcLocation >=cxSource )
				{			
					BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,cxSource,0,SRCCOPY);
					lSrcLocation = cxSource ;
					lDistance = 0;
				}
				else if(lSrcLocation <= 0)
				{
					BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,0,0,SRCCOPY);
					lSrcLocation = 0;
					lDistance = 0;
				}
				else
				{
					BitBlt(hdc,0,0,cxSource+cxSource2,cySource,hdcMem3,lSrcLocation,0,SRCCOPY);

				}

				DeleteObject(memBM);
			}
		}



		DeleteDC (hdcMem3) ;
		EndPaint (hwnd, &ps) ;
		return 0 ;


	case WM_MOUSEMOVE:

		//当鼠标移动并且按下左键的时候,通过对比前后2次,鼠标位置的x值,来判断鼠标的移动方向
		if(wParam & MK_LBUTTON)
		{
			IsInitialize = FALSE;

			xMouseLocationfirst = xMouseLocationsecond;  		   
			GetCursorPos(&MousePointSecond);
			xMouseLocationsecond = MousePointSecond.x;
			if(xMouseLocationfirst >= xMouseLocationsecond)
			{
				IsRight = FALSE;

				lDistance = +50;
			}
			else
			{
				IsRight = TRUE;
				lDistance = -50;
			}

			InvalidateRect(hwnd,NULL,TRUE);
		}	
		return 0;

	case WM_LBUTTONUP:

		IsShowResultPage = TRUE;
		InvalidateRect(hwnd,NULL,TRUE);
		return 0;


	case WM_DESTROY:
		DeleteObject (hBitmap) ;
		PostQuitMessage (0) ;
		return 0 ;
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值