투명 윈도우 만들기 | |||
| |||
A standard CDialog MFC EXE application was created with the wizard and apart from the standard supplied code, the following functions were overridden. void OnClose() Used to clean up some resources on exit. void OnPaint() Used to paint the bimap into the region. void OnSize(UINT nType, int cx, int cy) Used to create the region and position and resize the dialog. void OnLButtonDown(UINT nFlags, CPoint point) To allow the dialog to be dragged by clicking anywhere. A button was created to allow the user to close the dialog. A timer was created to demonstrate auto closing after a period of time. The image is a normal bmp file that is loaded by the app and used to paint the dialog. TRANSPARENTCOLOR is defined in the dialog class header as bright purple (RGB(255, 0, 255), but this could be changed to any colour you like. The dialog window is auto sized to the size of the loaded bitmap then a region created based on the transparent colour. The dialog is then positioned in the centre of the current screen. This was done as an example for those who want to use this application as a splash windows. #define TRANSPARENTCOLOR RGB(255, 0, 255); BITMAP m_Bitmap; // Struct to hold info about the bitmap HBITMAP m_hBitmap; // Handle of the bitmap void CTransparentDialogDlg::OnClose() { // TODO: Add your message handler code here and/or call default DeleteObject(m_hBitmap); //not really need but what the heck. CDialog::OnClose(); } void CTransparentDialogDlg::OnPaint() { // device context for painting CPaintDC dc(this); //Create a memory DC HDC hMemDC = ::CreateCompatibleDC(NULL); //Select the bitmap in the memory dc. SelectObject(hMemDC, m_hBitmap); //Copy the memory dc into the screen dc ::BitBlt(dc.m_hDC, 0, 0, m_Bitmap.bmWidth, m_Bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY); //Delete the memory DC and the bitmap ::DeleteDC(hMemDC); CDialog::OnPaint(); } void CTransparentDialogDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // Load the image m_hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), "Image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (m_hBitmap == NULL) { MessageBox("Error loading bitmap"); return; } //Get information about the bitmap.. GetObject(m_hBitmap, sizeof(m_Bitmap), &m_Bitmap); // Get info about the bitmap // Put the bitmap into a memory device context CPaintDC dc(this); //get a memory dc object CDC dcMem; //create a compatible dc dcMem.CreateCompatibleDC(&dc); // Select the bitmap into the in-memory DC //Select the bitmap into the dc CBitmap* pOldBitmap = dcMem.SelectObject(CBitmap::FromHandle(m_hBitmap)); //Create a couple of region objects. CRgn crRgn, crRgnTmp; //create an empty region crRgn.CreateRectRgn(0, 0, 0, 0); //Create a region from a bitmap with transparency colour of Purple COLORREF crTransparent = TRANSPARENTCOLOR; int iX = 0; for (int iY = 0; iY < m_Bitmap.bmHeight; iY++) { do { //skip over transparent pixels at start of lines. while (iX <= m_Bitmap.bmWidth && dcMem.GetPixel(iX, iY) == crTransparent) iX++; //remember this pixel int iLeftX = iX; //now find first non transparent pixel while (iX <= m_Bitmap.bmWidth && dcMem.GetPixel(iX, iY) != crTransparent) ++iX; //create a temp region on this info crRgnTmp.CreateRectRgn(iLeftX, iY, iX, iY+1); //combine into main region. crRgn.CombineRgn(&crRgn, &crRgnTmp, RGN_OR); //delete the temp region for next pass (otherwise you'll get an ASSERT) crRgnTmp.DeleteObject(); }while(iX < m_Bitmap.bmWidth); iX = 0; } //Centre it on current desktop SetWindowRgn(crRgn, TRUE); iX = (GetSystemMetrics(SM_CXSCREEN)) / 2 - (m_Bitmap.bmWidth / 2); iY = (GetSystemMetrics(SM_CYSCREEN)) / 2 - (m_Bitmap.bmHeight / 2); SetWindowPos(&wndTopMost, iX, iY, m_Bitmap.bmWidth, m_Bitmap.bmHeight, NULL); // Free resources. dcMem.SelectObject(pOldBitmap); // Put the original bitmap back (prevents memory leaks) dcMem.DeleteDC(); crRgn.DeleteObject(); } void CTransparentDialogDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y)); CDialog::OnLButtonDown(nFlags, point); } Tags: MFC 윈도우즈 | |||
| |||
| |||
로그인을 하시면 댓글을 등록 할 수 있습니다. |