⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bxfiledialog.cpp

📁 ResOrg 图形化管理Vc项目的资源ID的工具的源代码。 ResOrg - Manage and Renumber Resource Symbol IDs Introduction The
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	// Get this window's area
	CRect rectSubDialog;
	GetWindowRect(&rectSubDialog);
	
	// Now rectWndToCenterOn contains the screen rectangle of the window
	// .. pointed to by pWndToCenterOn.  Next, we apply the same centering
	// .. algorithm as does CenterWindow()
	
	// find the upper left of where we should center to
	int xLeft = (rectToCenterOn.left + rectToCenterOn.right) / 2 -
		rectSubDialog.Width() / 2;
	int yTop = (rectToCenterOn.top + rectToCenterOn.bottom) / 2 -
		rectSubDialog.Height() / 2;
	
	// Move the window to the correct coordinates with SetWindowPos()
	GetParent()->SetWindowPos(NULL, xLeft, yTop, -1, -1,
		SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

/************************************************************************/
/*  ResolveShortcut														*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      Anatoly Danekin										*/
/*  Description: Resolves the passed shortcut							*/
/*	Revisions:	 														*/
/************************************************************************/
BOOL BXFileDialog::ResolveShortcut(CString& strFile)
{
	HRESULT         hres;
	IShellLink*     psl;
	char            szGotPath[MAX_PATH];
	WIN32_FIND_DATA wfd;
	
	// Get a pointer to the IShellLink interface.
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
		IID_IShellLink, (void**)&psl);
	
	if (SUCCEEDED(hres))
	{
		IPersistFile* ppf;
		
		// Get a pointer to the IPersistFile interface.
		hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
		
		if (SUCCEEDED(hres))
		{
			WORD wsz[MAX_PATH];
			
			// Ensure that the string is Unicode.
			MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strFile, -1, wsz,
				MAX_PATH);
			
			// Load the shortcut.
			hres = ppf->Load(wsz, STGM_READ);
			if (SUCCEEDED(hres))
			{
				// Resolve the link.
				hres = psl->Resolve(GetParent()->m_hWnd, SLR_ANY_MATCH);
				if (SUCCEEDED(hres))
				{
					// Get the path to the link target.
					hres = psl->GetPath(szGotPath, MAX_PATH,
						(WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH );
					if (SUCCEEDED(hres))
						strFile = szGotPath;
				}
			}
			// Release the pointer to the IPersistFile interface.
			ppf->Release();
		}
		
		// Release the pointer to the IShellLink interface.
		psl->Release();
	}
	
	return SUCCEEDED(hres);
}

// a couple of helper functions:
int GetBiggest(int n1, int n2)
{
	if (n1 > n2)	return n1;
	if (n2 > n1)	return n2;
	return -1; // equal
}

float GetSmallestF(float f1, float f2)
{
	if (f1 < f2)	return f1;
	if (f2 < f1)	return f2;
	return -1.0f; // equal
}

/************************************************************************/
/*  DrawImage															*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      Wes Rogers, David Wulff								*/
/*  Description: Draws the specified image in the specified rectangle	*/
/*	Revisions:	 27:7:2001 - David Wulff, image now resizes properly and*/
/*				 .. centers												*/
/************************************************************************/
BOOL BXFileDialog::DrawImage(CDC* pDC, CString csFile, CPoint ptCenter,
							 HWND hWnd, CRect rectImage)
{
    if (pDC == NULL || csFile.IsEmpty() || hWnd == NULL)
        return FALSE;
	
    LPPICTURE gpPicture = NULL;
	
    if (LoadPictureFile((LPCTSTR)csFile, &gpPicture))
    {
        //Get width and height of picture
        long hmWidth  = 0;
        long hmHeight = 0;
		
        gpPicture->get_Width(&hmWidth);
        gpPicture->get_Height(&hmHeight);
		
		// [DW - 27:7:2001]
		CSize szImg(hmWidth, hmHeight);
		pDC->HIMETRICtoLP(&szImg);
		
		rectImage.DeflateRect(0, 0, 1, 1);
		
		int nLeft, nTop, nRight, nBottom;
		CRect rcDest = rectImage, rcSrc = CRect(0, 0, szImg.cx, szImg.cy);
		
		// if the image wont fit without being shrunk...
		if (GetBiggest(rcDest.Width(), rcSrc.Width()) == rcSrc.Width() ||
			GetBiggest(rcDest.Height(), rcSrc.Height()) == rcSrc.Height())
		{
			float nDX = rcDest.Width(), nDY = rcDest.Height(),
				nSX = rcSrc.Width(), nSY = rcSrc.Height();
			float nModX = nDX / nSX;
			float nModY = nDY / nSY;
			float nMod = GetSmallestF(nModX, nModY);
			
			// float -> int conversion
			nRight = rcSrc.Width() * nMod;
			nBottom = -(rcSrc.Height() * nMod);
			nLeft = rcDest.left + (rcDest.Width() / 2) - (nRight / 2);
			nTop = rcDest.top + (rcDest.Height() / 2) - (nBottom / 2);
		}
		else
		{
			// center the image
			nLeft = rcDest.left + (rcDest.Width() / 2) - (szImg.cx / 2);
			nTop = rcDest.top + (rcDest.Height() / 2) - (szImg.cy / 2) + szImg.cy;
			nRight = szImg.cx;
			nBottom = -(szImg.cy);
		}
		// [/DW - 27:7:2001]
		
        HRESULT hrP = gpPicture->Render(pDC->m_hDC, nLeft, nTop, nRight, nBottom,
			0, 0, hmWidth, hmHeight, NULL);
		
        gpPicture->Release();
		
        if (SUCCEEDED(hrP))
            return TRUE;
    }
	
    return FALSE;
}

/************************************************************************/
/*  LoadPictureFile														*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      Wes Rogers												*/
/*  Description: Loads the specified picture into the passed LPPICTURE	*/
/*	Revisions:	 														*/
/************************************************************************/
BOOL BXFileDialog::LoadPictureFile(LPCTSTR szFile, LPPICTURE* pgpPicture)
{
    // open file
    HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING,
		0, NULL);
	
    if (hFile == INVALID_HANDLE_VALUE)
        return FALSE;
	
    // get file size
    DWORD dwFileSize = GetFileSize(hFile, NULL);
	
    if (dwFileSize == (DWORD)-1)
    {
        CloseHandle(hFile);
        return FALSE;
    }
	
    LPVOID pvData = NULL;
	
    // alloc memory based on file size
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
	
    if (hGlobal == NULL)
    {
        CloseHandle(hFile);
        return FALSE;
    }
	
    pvData = GlobalLock(hGlobal);
	
    if (pvData == NULL)
    {
        GlobalUnlock(hGlobal);
        CloseHandle(hFile);
        return FALSE;
    }
	
    DWORD dwBytesRead = 0;
	
    // read file and store in global memory
    BOOL bRead = ReadFile(hFile,
		pvData,
		dwFileSize,
		&dwBytesRead,
		NULL);
	
    GlobalUnlock(hGlobal);
    CloseHandle(hFile);
	
    if (!bRead)
        return FALSE;
	
    LPSTREAM pstm = NULL;
	
    // create IStream* from global memory
    HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
	
    if (!(SUCCEEDED(hr)))
    {
        if (pstm != NULL)
            pstm->Release();
		
        return FALSE;
    }
	
    else if (pstm == NULL)
        return FALSE;
	
	// Create IPicture from image file
	if (*pgpPicture)
		(*pgpPicture)->Release();
	
    hr = ::OleLoadPicture(pstm,
		dwFileSize,
		FALSE,
		IID_IPicture,
		(LPVOID *)&(*pgpPicture));
	
    if (!(SUCCEEDED(hr)))
    {
		pstm->Release();
        return FALSE;
    }
	
    else if (*pgpPicture == NULL)
    {
		pstm->Release();
        return FALSE;
    }
	
    pstm->Release();
	
    return TRUE;
}

/************************************************************************/
/*  ReSize																*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      David Wulff, Tak^Shoran								*/
/*  Description: Called by the WM_SIZING override to reposition the		*/
/*				 .. dialog's controls.									*/
/*	Revisions:	 														*/
/************************************************************************/
void BXFileDialog::ReSize(int x, int y, int cx, int cy)
{
	// note: the listctrl is automatically resized by the underlying class
	CWnd* pWnd;
	CRect rcDlgCl, rcWin, rc1, rc2, rcLoc;
	
	GetClientRect(&rcDlgCl);
	GetWindowRect(&rcWin);
	
	int eRight = rcDlgCl.right - CONTROL_GAP;
	int eLeft = rcDlgCl.left + CONTROL_GAP;
	int eTop = rcDlgCl.top + CONTROL_GAP;
	int eBot = rcDlgCl.bottom - CONTROL_GAP;
	
	// Move preview box
	if (m_bShowPreview)
	{
		CWnd* pListCtrl = GetParent()->GetDlgItem(lst1);
		CRect rcList;
		pListCtrl->GetClientRect(&rcList);
		
		checkBox.SetWindowPos(0, eRight - 98,
			eBot - (CONTROL_GAP * 4) - (22 * 4), 90, 22, SWP_NOZORDER);
		
		PreviewRect.bottom = eBot - (CONTROL_GAP * 5) - (22 * 4);
		PreviewRect.right = eRight - CONTROL_GAP - 3;
		PreviewRect.left = PreviewRect.right - m_nPreviewBoxWidth;
	}
	
	// Move button
	m_btn.GetWindowRect(&rc1);
	ScreenToClient(&rc1);
	
	pWnd = GetParent()->GetDlgItem(IDCANCEL);
	pWnd->GetWindowRect(&rc2);
	ScreenToClient(&rc2);
	
	rc1.left  = rc2.left;
	rc1.right = rc2.right;
	
	m_btn.SetWindowPos(0, rc1.left, rc1.top, rc1.Width(), rc1.Height(),
		SWP_NOZORDER);
	
	// Move outbar
	CWnd* pWndOutLookBar = GetDlgItem(IDC_OUTLOOK_BAR_PLACEHOLDER);
	pWndOutLookBar->GetWindowRect(&rc1);
	ScreenToClient(&rc1);
	
	rc1.bottom = rcDlgCl.Height() - rc1.top;
	
	pWndOutLookBar->SetWindowPos(0, rc1.left, rc1.top, rc1.Width(),
		rc1.Height(), SWP_NOZORDER);
	
	Invalidate();
}

/************************************************************************/
/*  SetAppearance														*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      David Wulff											*/
/*  Description: Pass in the eAppearance to use	when drawing the dialog	*/
/*				 .. and buttons, etc.									*/
/*	Revisions:	 														*/
/************************************************************************/
void BXFileDialog::SetAppearance(eAppearance eApp)
{
	m_eAppearance = eApp;
	bDotNetUI = (m_eAppearance == eAppearance::APPEARANCE_VSDOTNET);
}

/************************************************************************/
/*  OnFileTypeChange													*/
/*																		*/
/*  Created:     27:7:2001												*/
/*  Author:      David Wulff											*/
/*  Description: Handler for the CDN_TYPECHANGE message					*/
/*	Revisions:	 														*/
/************************************************************************/
void BXFileDialog::OnFileTypeChange(DWORD dwNewFilterIndex)
{
	// you can override this function to provide custom control over
	// .. the OK button's drop-down menu, depending on the file type
	// .. selected.
	
	// E.G:
#if 0
	// destroy the current menu (remove all items)
	m_btn.m_menu.DestroyMenu();
	
	// recreate the popup menu
	m_btn.m_menu.CreatePopupMenu();
	
	// add the new items, depending on the file type selected
	switch(dwNewFilterIndex)
	{
	case 0:
		m_btn.AddMenuItem(ID_START,"&Open",0);
		m_btn.AddMenuItem(ID_START+1,"Open with &Filter",0);
		break;
	case 1:
		m_btn.AddMenuItem(ID_START,"&Open",0);
		break;
	case 2:
		m_btn.AddMenuItem(ID_START,"&Open",0);
		m_btn.AddMenuItem(ID_START+1,"Open As &HTML",0);
		break;
	default:
		break; // do nothing
	}
#endif
}

//////////////////////////////////////////////////////////////////////////
// BXFileEdit (public CEdit)
//////////////////////////////////////////////////////////////////////////

/************************************************************************/
/*  BXFileEdit															*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      David Wulff											*/
/*  Description: Class constructor										*/
/*	Revisions:	 														*/
/************************************************************************/
BXFileEdit::BXFileEdit()
{
}


/************************************************************************/
/*  ~BXFileEdit															*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      David Wulff											*/
/*  Description: Class destructor										*/
/*	Revisions:	 														*/
/************************************************************************/
BXFileEdit::~BXFileEdit()
{
}

BEGIN_MESSAGE_MAP(BXFileEdit, CEdit)
//{{AFX_MSG_MAP(BXFileEdit)
ON_WM_KEYUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/************************************************************************/
/*  OnKeyUp																*/
/*																		*/
/*  Created:     31:3:2001												*/
/*  Author:      David Wulff											*/
/*  Description: WM_KEYUP handler for the new edit control				*/
/*	Revisions:	 														*/
/************************************************************************/
void BXFileEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (BXFileDialog::m_bClear)
	{
		SetWindowText("");
		BXFileDialog::m_bClear = FALSE;
		SetSel(0,-1);
	}
	
	CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

#pragma warning ( default : 4244 ) // float -> int conversion warnings

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -