透明效果图

// a3.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; HBITMAP bg,bmp,girl; HDC mdc,hdc; const int xstart=180; const int ystart=70; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text // Foward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_A3, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_A3); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage is only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_A3); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_A3; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; HDC bufdc; // HBITMAP bmp; BITMAP bm1,bm2; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } MoveWindow(hWnd,0,0,600,400,true); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); //获取当前的显示设备上下文 bg = (HBITMAP)LoadImage(NULL,"bg2.bmp",IMAGE_BITMAP,600,400,LR_LOADFROMFILE); bmp=(HBITMAP)LoadImage(NULL,"girl.bmp",IMAGE_BITMAP,300,200,LR_LOADFROMFILE); GetObject(bg,sizeof(BITMAP),&bm1); if(bm1.bmBitsPixel!=32&&bm1.bmBitsPixel !=24) { MessageBox(NULL,"此程序","警告",0); return FALSE; } hdc = GetDC(hWnd); mdc= CreateCompatibleDC(hdc);//创建存放图象的显示缓冲 bufdc= CreateCompatibleDC(hdc);//创建存放图象的显示缓冲 girl=CreateCompatibleBitmap(hdc,300,200);//创建存放图象的显示缓冲 SelectObject(mdc,girl); //将位图资源装入显示缓冲 //在mdc进得透明处理 SelectObject(bufdc,bg); //将位图资源装入显示缓冲 BitBlt(mdc,0,0,300,200,bufdc,xstart,ystart,SRCCOPY); SelectObject(bufdc,bmp); //将位图资源装入显示缓冲 BitBlt(mdc,0,0,300,200,bufdc,300,0,SRCAND); BitBlt(mdc,0,0,300,200,bufdc,0,0,SRCPAINT); unsigned char *px1,*px2; //处理背景图 px1=new unsigned char [bg,bm1.bmHeight *bm1.bmWidthBytes ]; GetBitmapBits(bg,bm1.bmHeight *bm1.bmWidthBytes,px1); //处理前景图 GetObject(girl,sizeof(BITMAP),&bm2); px2=new unsigned char [girl,bm2.bmHeight *bm2.bmWidthBytes ]; GetBitmapBits(girl,bm2.bmHeight *bm2.bmWidthBytes,px2); int xend,yend; int x,y,i; int rgb_b; int pxBytes=bm1.bmBitsPixel/8; xend=xstart+300; yend=ystart+200; for(y=ystart;y<yend;y++) { for(x=xstart;x<xend;x++) { rgb_b=y*bm1.bmWidthBytes +x*pxBytes; px1[rgb_b]=px1[rgb_b]*0.7; px1[rgb_b+1]=px1[rgb_b+1]*0.7; px1[rgb_b+2]=px1[rgb_b+2]*0.7; } } for(y=0;y<bm2.bmHeight ;y++) { for(x=0;x<bm2.bmWidth ;x++) { rgb_b=y*bm2.bmWidthBytes +x*pxBytes; i=(ystart+y)*bm1.bmWidthBytes +(xstart+x)*pxBytes; px2[rgb_b]=px2[rgb_b]*0.3+px1[i]; px2[rgb_b+1]=px2[rgb_b+1]*0.3+px1[i+1]; px2[rgb_b+2]=px2[rgb_b+2]*0.3+px1[i+2]; } } /* */ SetBitmapBits(girl,bm2.bmHeight *bm2.bmWidthBytes ,px2); MyPaint(hdc); ReleaseDC(hWnd,hdc); delete(px1); delete(px2); return TRUE; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Mesage handler for about box. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } void MyPaint(HDC hdc) { SelectObject(mdc,bg); //将位图资源装入显示缓冲 BitBlt(hdc,0,0,600,400,mdc,0,0,SRCCOPY); SelectObject(mdc,girl); //将位图资源装入显示缓冲 BitBlt(hdc,xstart,ystart,300,200,mdc,0,0,SRCCOPY); // BitBlt(hdc,100,200,150,100,mdc,0,0,SRCPAINT); }  

你可能感兴趣的:(透明效果图)