窗體置頂透明移動(dòng)且為不規(guī)則的代碼_第1頁
窗體置頂透明移動(dòng)且為不規(guī)則的代碼_第2頁
窗體置頂透明移動(dòng)且為不規(guī)則的代碼_第3頁
窗體置頂透明移動(dòng)且為不規(guī)則的代碼_第4頁
窗體置頂透明移動(dòng)且為不規(guī)則的代碼_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、前幾天做了一些代碼的整合。將幾個(gè)功能都做到了一起,做成了一個(gè)項(xiàng)目。下面的代碼主要實(shí)現(xiàn)了一個(gè)不規(guī)則的窗體,且是透明置頂而且可以移動(dòng)的窗體。class CSetWindowRgnDlg : public CDialog/ Constructionpublic:CSetWindowRgnDlg(CWnd* pParent = NULL); / standard constructorHRGN BitmapToRegion(HBITMAP hBmp, COLORREF cTransparentColor, COLORREF cTolerance=NULL);/ Dialog Data/ ClassWi

2、zard generated virtual function overrides /AFX_VIRTUAL(CSetWindowRgnDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / /AFX_DATA(CSetWindowRgnDlg) enum IDD = IDD_SETWINDOWRGN_DIALOG ; / NOTE: the ClassWizard will add data members here /AFX_DATADDX/DDV support/ Implementationprotected

3、:;BOOL CSetWindowRgnDlg:OnInitDialog() CDialog:OnInitDialog(); / Generated message map functions /AFX_MSG(CSetWindowRgnDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnLButtonDown(UINT nFla

4、gs, CPoint point); afx_msg void OnTimer(UINT nIDEvent); /AFX_MSG DECLARE_MESSAGE_MAP() HICON m_hIcon; /AFX_VIRTUALCMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty() pSysMenu->AppendMenu(MF_SEPARATO

5、R); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); / Add "About." menu item to system menu. strAboutMenu);/ Set the icon for this dialog. The framework

6、 does this automatically/ when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); / TODO: Add extra initialization here CBitmap bmp; if(bmp.LoadBitmap(IDB_BITMAP1)/加載位圖 HRGN rgn; rgn = BitmapToRegion(HBITMAP)bmp, RGB(255, 255, 255); SetWindowRgn(rgn, T

7、RUE); bmp.DeleteObject(); / Set big icon / Set small icon /設(shè)備為半透明窗體 SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE)0X80000);HINSTANCE hlnst = LoadLibrary("User32.DLL"); if(hlnst) typedef BOOL (WINAPI*MYFUNC)(HWND, COLORREF, BYTE,DWORD);

8、MYFUNC fun = NULL; fun = (MYFUNC)GetProcAddress(hlnst, "SetLayeredWindowAttributes");if(fun)fun(this->GetSafeHwnd(), 0, 129, 2);FreeLibrary(hlnst);/設(shè)置窗口的層次SetWindowPos(&wndTopMost, 0, 0, 0,SWP_NOSIZE);CenterWindow();CRect rect;GetWindowRect(rect);rect.OffsetRect(0, 0);MoveWindow(rec

9、t);/設(shè)定時(shí)間SetTimer(0, 200, NULL);0, SWP_NOSIZE |/設(shè)置窗體的的初始化位置 RECT rect1; GetClientRect(&rect1); MoveWindow(1366-rect.right,0,rect.right,rect.bottom); return TRUE; / return TRUE unless you set the focus to a controlvoidlParam)if (nID & 0xFFF0) = IDM_ABOUTBOX) else CDialog:OnSysCommand(nID, lPar

10、am); CAboutDlg dlgAbout; dlgAbout.DoModal(); CSetWindowRgnDlg:OnSysCommand(UINT nID, LPARAM/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CSet

11、WindowRgnDlg:OnPaint()if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND,dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.W

12、idth() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2; (WPARAM) else CClientDC dc(this); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); CBitmap *pOldBmp; pOldBmp = memDC.SelectObject(&bmp); BITMAP bm; bmp.GetBitmap(&bm); dc.BitBlt(0, 0, bm

13、.bmWidth, bm.bmHeight, &memDC, 0, 0, / Draw the icon dc.DrawIcon(x, y, m_hIcon); NOTSRCCOPY);memDC.SelectObject(pOldBmp); bmp.DeleteObject(); CDialog:OnPaint(); /系統(tǒng)調(diào)用這個(gè)獲取光標(biāo)顯示當(dāng)用戶拖動(dòng)/最小化窗口HCURSOR CSetWindowRgnDlg:OnQueryDragIcon()HRGN CSetWindowRgnDlg:BitmapToRegion(HBITMAP hBmp, return (HCURSOR) m

14、_hIcon; COLORREF cTransparentColor, COLORREF cTolerance)HRGN hRgn = NULL; if (hBmp) HDC hMemDC = CreateCompatibleDC(NULL); if (hMemDC) BITMAP bm; GetObject(hBmp, sizeof(bm), &bm); /創(chuàng)建一個(gè)32位色的位圖,并選進(jìn)內(nèi)存設(shè)備環(huán)境寸BITMAPINFOHEADER RGB32BITSBITMAPINFO = sizeof(BITMAPINFOHEADER),/ 大小,尺bm.bmWidth, bm.bmHeight

15、, 1, 32,/ 寬/ 高 / 平面 / 計(jì)數(shù)/ 壓縮BI_RGB, 0, 0, 0, 0, 0/ 圖片尺寸 / X坐標(biāo) / Y坐標(biāo) / biClrUsed; /biClrImportant; ;VOID * pbits32; /創(chuàng)建一個(gè)獨(dú)立的位圖 HBITMAPhbm32=CreateDIBSection(hMemDC,(BITMAPINFO DIB_RGB_COLORS, &pbits32, NULL, 0);if (hbm32) *)&RGB32BITSBITMAPINFO,HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC,

16、hbm32);/創(chuàng)建一個(gè)直流只是拷貝位圖到內(nèi)存DC HDC hDC = CreateCompatibleDC(hMemDC); if (hDC) /得到多少字節(jié)/行我們已經(jīng)為位圖位(四舍五入到32位)BITMAP bm32; GetObject(hbm32, sizeof(bm32), &bm32); while (bm32.bmWidthBytes % 4) /位圖拷貝到內(nèi)存中直流 HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, bm32.bmWidthBytes+; hBmp);BitBlt(hMemDC, 0, 0, bm.bmWidth

17、, bm.bmHeight, hDC, 0, 0, SRCCOPY);/為了更好的表演,我們將使用ExtCreateRegion()函數(shù)來創(chuàng)建/地區(qū)。這個(gè)函數(shù)采取一個(gè)RGNDATA結(jié)構(gòu)對條目。我們將添加的矩形/數(shù)量的ALLOC_UNIT號(hào)碼在這個(gè)結(jié)構(gòu)。 #define ALLOC_UNIT 100DWORD maxRects = ALLOC_UNIT; HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects);RGNDATA *pData = (RGNDATA *)Gl

18、obalLock(hData);pData->rdh.dwSize = sizeof(RGNDATAHEADER); pData->rdh.iType = RDH_RECTANGLES; pData->rdh.nCount = pData->rdh.nRgnSize = 0; SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);/保留在手邊的最高和最低值為“透明”的像素 BYTE lr = GetRValue(cTransparentColor); BYTE lg = GetGValue(cTransp

19、arentColor); BYTE lb = GetBValue(cTransparentColor); BYTE hr = min(0xff, lr + GetRValue(cTolerance); BYTE hg = min(0xff, lg + GetGValue(cTolerance); BYTE hb = min(0xff, lb + GetBValue(cTolerance);垂直)/掃描每一位圖行從底部向頂部(位圖是反向的 BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;for (

20、int y = 0; y < bm.bmHeight; y+) /掃描每個(gè)像素的位圖從左到右 for (int x = 0; x < bm.bmWidth; x+) /搜索一個(gè)連續(xù)范圍的“非透明像素” int x0 = x; LONG *p = (LONG *)p32 + x; while (x < bm.bmWidth) BYTE b = GetRValue(*p); if (b >= lr && b <= hr) b = GetGValue(*p); if (b >= lg && b <= hg) b = GetBV

21、alue(*p);的矩形區(qū)域sizeof(RGNDATAHEADER) GMEM_MOVEABLE);*)GlobalLock(hData); if (b >= lb && b <= hb) /該像素是“透明的” break; p+; x+; if (x > x0) /添加像素(x0,y)(x,y + 1)作為一個(gè)新 if (pData->rdh.nCount >= maxRects) GlobalUnlock(hData); maxRects += ALLOC_UNIT; hData = GlobalReAlloc(hData, + (sizeo

22、f(RECT) * maxRects), pData = (RGNDATA RECT *pr = (RECT *)&pData->Buffer; SetRect(&prpData->rdh.nCount, x0, y, x, y+1);if (x0 < pData->rdh.rcBound.left) pData->rdh.rcBound.left = x0; if (y < pData->rdh.rcBound.top) pData->rdh.rcBound.top = y; if (x > pData->rdh.r

23、cBound.right) pData->rdh.rcBound.right = x; if (y+1 > pData->rdh.rcBound.bottom) pData->rdh.rcBound.bottom = y+1; pData->rdh.nCount+; / Windows98上,ExtCreateRegion()可能會(huì)失敗的數(shù)量是否矩形太/ 大(即:> 4000)。因此,我們必須創(chuàng)建該地區(qū)的多個(gè)步驟。if (pData->rdh.nCount = 2000) HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);if (hRgn) CombineRgn(hRgn, hRgn, h, RGN_OR); else

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論