连接图片(二),实现根据鼠标方向,平滑移动
2011-11-22 15:31
106 查看
/*---------------------------------------- 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) ; }
相关文章推荐
- css3和js实现鼠标滑过图片微移动 平滑过渡
- css实现鼠标移动图片居中放大效果
- 图片移动、鼠标画线----实现GIS地图操作
- JavaScript简单实现鼠标移动切换图片的方法
- C#实现根据图片的EXIF自动调整图片方向
- Js中实现图片随鼠标移动——ClientX,ClientY,Position
- Js实现图片轮播,以及鼠标移动选择指定图片
- 1.纯css实现鼠标移动图片切换的效果
- C#实现根据图片的EXIF自动调整图片方向
- 鼠标移动到一张图片另几张堆叠的实现
- css3实现小米商城鼠标移动图片上浮阴影效果
- CSS3实现鼠标移动到图片上图片变大(缓慢变大,有过渡效果,放大的过程是有动画过渡的,这个过渡的时间可以自定义)
- WM_LBUTTONDOW实现地图不断往鼠标方向移动位置
- js实现鼠标移动到图片产生遮罩效果
- XNA实践鼠标篇--根据点击坐标移动图片(四)
- CSS实现鼠标移动到图片上图片变大
- JS实现图片跟随鼠标移动
- js实现跟随鼠标移动且带关闭功能的图片广告实例
- js根据鼠标移动速度背景图片自动旋转的方法
- CSS3实现鼠标移动到图片上图片变大(缓慢变大,有过渡效果,放大的过程是有动画过渡的,这个过渡的时间可以自定义)