투명 윈도우 만들기

31433 
Created at 2006-09-28 04:09:09 
290   0   0   0  
You should be able to use this demo as a skeleton application for your own project.

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 윈도우즈 Share on Facebook Share on X

◀ PREVIOUS
마우스로 윈도우 드래그하여 이동시키는 비기
▶ NEXT
GDI+ Programming 방법
  Comments 0
Login for comment
SIMILAR POSTS

GDI+ Programming 방법 (created at 2006-09-28)

CFileDialog - File Open Example (created at 2006-09-28)

윈도우 옮기는 API - SetWindowPos (created at 2006-09-28)

마우스로 윈도우 드래그하여 이동시키는 비기 (created at 2006-09-28)

불투명 윈도우 만들기 (created at 2006-09-28)

WinCE에서 Key Hooking하는 방법 (created at 2006-09-28)

비트맵을 배경으로 뿌리고 그 위에 컨트롤 올리는 방법 (created at 2006-09-28)

IE Control을 사용하여 만든 어플리케이션에서 javascript로 어플리케이션에 정의된 함수 호출하는 방법 (created at 2006-09-28)

Broadcast를 이용한 Application 종료 (created at 2006-09-28)

DirectShow - NULL Rendering Example (created at 2006-09-28)

모달리스 다이얼로그의 종료 버튼을 클릭했을 때 종료가 안될때... (created at 2006-09-28)

ActiveX에서 바이너리 데이터 파라메터로 안깨지게 받는법 (created at 2006-09-28)

ListCtrl에서 아이템 추가하기 예제 (created at 2006-09-28)

BITMAP 오브젝트에서 가로,세로 크기 알아내기 (created at 2006-09-28)

비트맵을 파일에서 로딩하는 비기 (created at 2006-09-28)

듀얼 모니터에서 Focused Monitor의 Rect 알아내기 (created at 2006-09-28)

프로그램에서 버젼(version) 정보 읽어주는 소스 (created at 2006-09-28)

드라이브 문자 알아내는 소스 (created at 2006-09-28)

ActiveX에 다이얼로그 붙이기 (created at 2006-09-28)

웹브라우져가 떠서 웹페이지 보이게 하는 소스 (created at 2006-09-28)

Toolbar에서 Icon 없애기 (created at 2006-09-28)

RDP(Remote Desktop Protocol) Port 값 가져오고/바꿔주기 (created at 2006-09-28)

String Find Function (StrFnd) (created at 2006-09-28)

작업관리자에 프로그램 안뜨게 하기 (created at 2006-09-28)

파일에서 한줄만 읽어다 return 해주는 소스 (created at 2006-09-28)

HTML 긁어오는 프로그램 소스 (created at 2006-09-28)

IE 패치에 따른 object, embed, applet 대처 방안 (created at 2006-09-28)

빈폴더 찾아내기 (created at 2006-09-29)

프로세스명으로 프로세스 죽이는 함수 (created at 2006-09-29)

자기자신 IP알아내기(로칼컴퓨터) (created at 2006-09-29)

OTHER POSTS IN THE SAME CATEGORY

작업관리자에 프로그램 안뜨게 하기 (created at 2006-09-28)

String Find Function (StrFnd) (created at 2006-09-28)

Toolbar에서 Icon 없애기 (created at 2006-09-28)

웹브라우져가 떠서 웹페이지 보이게 하는 소스 (created at 2006-09-28)

ActiveX에 다이얼로그 붙이기 (created at 2006-09-28)

드라이브 문자 알아내는 소스 (created at 2006-09-28)

ListCtrl에서 아이템 추가하기 예제 (created at 2006-09-28)

ActiveX에서 바이너리 데이터 파라메터로 안깨지게 받는법 (created at 2006-09-28)

모달리스 다이얼로그의 종료 버튼을 클릭했을 때 종료가 안될때... (created at 2006-09-28)

DirectShow - NULL Rendering Example (created at 2006-09-28)

Broadcast를 이용한 Application 종료 (created at 2006-09-28)

IE Control을 사용하여 만든 어플리케이션에서 javascript로 어플리케이션에 정의된 함수 호출하는 방법 (created at 2006-09-28)

윈도우 옮기는 API - SetWindowPos (created at 2006-09-28)

CFileDialog - File Open Example (created at 2006-09-28)

GDI+ Programming 방법 (created at 2006-09-28)

마우스로 윈도우 드래그하여 이동시키는 비기 (created at 2006-09-28)

불투명 윈도우 만들기 (created at 2006-09-28)

WinCE에서 Key Hooking하는 방법 (created at 2006-09-28)

비트맵을 배경으로 뿌리고 그 위에 컨트롤 올리는 방법 (created at 2006-09-28)

BITMAP 오브젝트에서 가로,세로 크기 알아내기 (created at 2006-09-28)

비트맵을 파일에서 로딩하는 비기 (created at 2006-09-28)

듀얼 모니터에서 Focused Monitor의 Rect 알아내기 (created at 2006-09-28)

프로그램에서 버젼(version) 정보 읽어주는 소스 (created at 2006-09-28)

RDP(Remote Desktop Protocol) Port 값 가져오고/바꿔주기 (created at 2006-09-28)

UPDATES

글루코사민 vs. 콘드로이친: 무엇이 더 나은 관절 건강 보조제일까? (created at 2024-04-22)

광주 5·18 민주화운동 알린 테리 앤더슨 前 AP 기자 (created at 2024-04-22)

햄과 소세지가 우리 몸에 일으키는 부작용 (updated at 2024-04-22)

콘드로이친의 염증 감소효과 (updated at 2024-04-22)

코사민 DS - 글루코사민+콘드로이친 복합물이 함유된 퇴행성 관절 건강보조제 (updated at 2024-04-22)

삼겹살 먹을때 환상조합 (created at 2024-04-22)

일본 여중생의 특이한 취향 (created at 2024-04-22)

우리가 먹는 약물이 바꿔버린 생태계 (created at 2024-04-21)

일본에서 그린 상상속의 사무직과 현실속의 사무직 (updated at 2024-04-21)

북한 미대생들이 그린 북한 최고존엄 김정은 (created at 2024-04-21)

입사 후 1년도 되지 않은 회사에서 구조조정에 의한 퇴직 불응에 따른 해고 처리시 대응 가능한 방법 (updated at 2024-04-20)

한고은님의 옛날 사진 (updated at 2024-04-20)

소녀대 - Bye Bye Girl (updated at 2024-04-13)

대한민국 날씨 근황 (created at 2024-04-13)

성일종 인재육성 강조하며 이토 히로부미 언급 - 인재 키운 선례? (updated at 2024-04-13)

일제강점기가 더 살기 좋았을지도 모른다는 조수연 국민의힘 후보 - 친일파? (updated at 2024-04-13)

Marshall Ha님의 샤오미 SU7 시승기 - 테슬라의 일론 머스크님이 긴장할만한 느낌 (updated at 2024-04-09)

윙크하는 귀염둥이 반려견들 (created at 2024-04-08)

달콤 살벌한 고백 (created at 2024-04-08)

북한 최정예 공수부대 훈련 모습 (updated at 2024-04-02)

맛있었던 친구 어머니의 주먹밥이 먹고 싶어요 (created at 2024-04-02)

자리 마음에 안든다고 6급 공무원 패는 농협 조합장 (created at 2024-03-26)

85세 딸 짜장면 사주는 102세 어머니 (created at 2024-03-26)

1990년대 감각파 도둑 (created at 2024-03-26)

치매에 걸린 69살의 브루스 윌리스가 전부인 데미무어를 보고 한 말 (updated at 2024-03-22)

경제는 대통령이 살리는 것이 아닙니다 라던 윤석열대통령 - 상황 안좋아지자 여러 전략을 펼쳤지만, 부작용 속출했던 2024년의 봄 (updated at 2024-03-13)

극빈의 생활을 하고 배운것이 없는 사람은 자유가 뭔지도 모를 뿐 아니라 왜 개인에게 필요한지에 대한 필요성을 못느낀다는 윤석열 대통령 (updated at 2024-03-08)

조선일보를 안본다는 사람들이 말하는 그 이유 - 천황폐하, 전두환 각하, 김일성 장군 만세? (created at 2024-03-07)

광폭타이어를 장착하면 성능이 좋아질거라는 착각 (updated at 2024-03-03)

면허시험장에서 면허갱신하면 하루만에 끝나나? (updated at 2024-03-03)