📄 addressbar.h
字号:
if (strText != strLB) {
T* pT = static_cast<T*>(this);
// change icon on edit control
// COMBOBOXEXITEM item;
// pT->OnGetItem(strLB, item);
// item.mask = CBEIF_IMAGE|CBEIF_SELECTEDIMAGE;// only image
// item.iItem = -1;
// MTLVERIFY(SetItem(&item));
pT->OnItemSelected(strLB);
_MoveToTopAddressBox(strLB);
}
}
LRESULT OnCbenGetDispInfo(LPNMHDR lpnmhdr)
{
PNMCOMBOBOXEX pDispInfo = (PNMCOMBOBOXEX)lpnmhdr;
T* pT = static_cast<T*>(this);
pT->OnGetDispInfo(pDispInfo->ceItem);
return 0;
}
LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
RECT rect;
GetClientRect(&rect);
::FillRect((HDC)wParam, &rect, (HBRUSH)LongToPtr(COLOR_3DFACE + 1));
return 1; // don't do the default erase
}
LRESULT OnComboEraseBackground(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
// fill only thumb
// const int cxyBorder = 2;
// CRect rc;
// m_wndCombo.GetClientRect(&rc);
// rc.left = rc.right - ::GetSystemMetrics(SM_CXHTHUMB) - cxyBorder;
// rc.top += cxyBorder;
// rc.bottom -= cxyBorder;
// ::FillRect((HDC)wParam, &rc, (HBRUSH)LongToPtr(COLOR_3DFACE + 1));
return 1; // don't do the default erase
}
LRESULT OnComboWindowPosChanging(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
abrTRACE(_T("CAddressBarCtrlImpl::OnComboWindowPosChanging\n"));
LRESULT lRet = m_wndCombo.DefWindowProc(uMsg, wParam, lParam);
LPWINDOWPOS lpWP = (LPWINDOWPOS)lParam;
lpWP->cx = m_cx - _GetGoBtnWidth();
// CRect rc; m_wndCombo.GetClientRect(&rc);
// rc.right = lpWP->cx - ::GetSystemMetrics(SM_CXHTHUMB) - 2;
// CEdit edit = GetEditCtrl();
// m_wndCombo.ClientToScreen(&rc);
// edit.ScreenToClient(&rc);
// edit.ValidateRect(&rc);
return lRet;
}
LRESULT OnEditEraseBackground(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
return 1; // don't do the default erase
}
LRESULT OnEditLButtonDblClk(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{// fixed for Win2000 by DOGSTORE
m_wndEdit.SetSel(0, -1);
return 0;
}
void OnEditKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// if not dropped, eat VK_DOWN
if (!GetDroppedState() && !_AutoCompleteWindowVisible() && (nChar == VK_DOWN || nChar == VK_UP)) {
SetMsgHandled(TRUE);
}
else if (nChar == VK_RETURN) {
_OnEnterKeyDown();
SetMsgHandled(TRUE);
}
else {
SetMsgHandled(FALSE);
}
}
LRESULT OnToolTipText(int idCtrl, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
LPNMTTDISPINFO pDispInfo = (LPNMTTDISPINFO)pnmh;
pDispInfo->szText[0] = 0;
if((idCtrl != 0) && !(pDispInfo->uFlags & TTF_IDISHWND))
{
CString strHeader = _T('\"');
CString strHelp = _T("\" 傊堏摦");
TCHAR szBuff[80];
CString strSrc = MtlGetWindowText(GetEditCtrl());
if (strSrc.IsEmpty())
return 0;
AtlCompactPathFixed(szBuff, strSrc, 80 - strHelp.GetLength() - strHeader.GetLength());
strHeader += szBuff;
strHeader += strHelp;
::lstrcpyn(pDispInfo->szText, strHeader, 80);
}
return 0;
}
// Implementation
void _GetProfile()
{
CIniSection pr;
CString strSection = _T("AddressBar#");
strSection.Append(GetDlgCtrlID());
pr.Open(_szIniFileName, strSection);
pr.QueryValue(m_dwAddressBarExtendedStyle, _T("ExtendedStyle"));
pr.Close();
}
void _WriteProfile()
{
CIniSection pr;
CString strSection = _T("AddressBar#");
strSection.Append(GetDlgCtrlID());
pr.Open(_szIniFileName, strSection);
pr.SetValue(m_dwAddressBarExtendedStyle, _T("ExtendedStyle"));
pr.Close();
}
void _MoveToTopAddressBox(const CString& strURL)
{
T* pT = static_cast<T*>(this);
COMBOBOXEXITEM item;
pT->OnGetItem(strURL, item);
item.iItem = 0;// add to top
item.pszText = (LPTSTR)(LPCTSTR)strURL;
// search the same string
int nCount = GetCount();
for (int n = 0; n < nCount; ++n) {
CString str;
MtlGetLBTextFixed(m_hWnd, n, str);
if (strURL == str) {
DeleteItem(n);
InsertItem(&item);
break;
}
}
}
void _OnEnterKeyDown()
{
CEdit edit = GetEditCtrl();
CString str = MtlGetWindowText(edit);//some comboboxex control impl often not support GetWindowText
if (!str.IsEmpty()) {
if (_AutoCompleteWindowVisible())// I have to clean auto complete window, but escape means undo...
::SendMessage(GetEditCtrl(), WM_KEYDOWN, (WPARAM)VK_ESCAPE, 0);
T* pT = static_cast<T*>(this);
pT->OnItemSelected(str);
_AddToAddressBoxUnique(str);
}
}
bool _AutoCompleteWindowVisible() const
{
CRect rc; m_wndEdit.GetWindowRect(&rc);
CPoint pt(rc.left + 1, rc.bottom + 5);
HWND hWndDropDown = ::WindowFromPoint(pt);
if (MtlIsFamily(m_hWnd, hWndDropDown))
return false;
else
return true;
// HWND hWnd = ::FindWindow(_T("Auto-Suggest Dropdown"), NULL);
// return ::IsWindowVisible(hWnd) == TRUE;
}
void _AddToAddressBoxUnique(const CString& strURL)
{
T* pT = static_cast<T*>(this);
COMBOBOXEXITEM item;
pT->OnGetItem(strURL, item);
item.iItem = 0;// ado to top
item.pszText = (LPTSTR)(LPCTSTR)strURL;
// search the same string
int nCount = GetCount();
for (int n = 0; n < nCount; ++n) {
CString str;
MtlGetLBTextFixed(m_hWnd, n, str);
if (strURL == str) {
DeleteItem(n);
break;
}
}
InsertItem(&item);
}
void _ShowGoButton(bool bShow)
{
if (bShow)
m_wndGo.ShowWindow(SW_SHOWNORMAL);
else
m_wndGo.ShowWindow(SW_HIDE);
// SendMessage(WM_SIZE, 0, 0);// update layout of go button
// generate WM_WINDOWPOSCHANGING, no other way
CRect rect;
GetWindowRect(rect);
CWindow(GetParent()).ScreenToClient(rect);
SetWindowPos(NULL, rect.left, rect.top,
rect.right - rect.left - 1, rect.bottom - rect.top,
SWP_NOZORDER|SWP_NOREDRAW);// | SWP_NOACTIVATE);
SetWindowPos(NULL, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER);// | SWP_NOACTIVATE);
}
int _GetGoBtnWidth()
{
abrTRACE(_T("CAddressBarCtrlImpl::_GetGoBtnWidth\n"));
// if (!m_wndGo.m_hWnd || !m_wndGo.IsWindowVisible())
// return 0;
if (!m_wndGo.m_hWnd || !_check_flag(m_dwAddressBarExtendedStyle, ABR_EX_GOBTNVISIBLE)) {
abrTRACE(_T(" return 0\n"));
return 0;
}
/* TBBUTTONINFO bi;
memset(&bi, 0, sizeof(bi));
bi.cbSize = sizeof(TBBUTTONINFO);
bi.dwMask = TBIF_SIZE;
if (m_wndGo.GetButtonInfo(m_nGoBtnCmdID, &bi) == -1) {
abrTRACE(_T(" failed\n"));
return 0;
}
abrTRACE(_T(" width(%d)\n"), bi.cx);*/
return m_cxGoBtn + s_kcxGap;
}
// Strangly GetButtonInfo lies...
int _CalcBtnWidth(int cxIcon, const CString& strText)
{
const int cxGap = 3;
if (strText.IsEmpty())
return cxGap*2 + cxIcon;
int nWidth = 0;
nWidth += cxGap*2;
nWidth += cxIcon;
nWidth += cxGap*2 - 1;
nWidth += MtlComputeWidthOfText(strText, m_wndGo.GetFont());
// nWidth += cxGap*2;
return nWidth;
}
int _CalcBtnHeight(int cyIcon)
{
const int cyGap = 3;
CLogFont lf; CFontHandle(m_wndGo.GetFont()).GetLogFont(&lf);
int cy = lf.lfHeight;
if(cy < 0)
cy = -cy;
return MtlMax(cy, cyIcon + cyGap*2);
}
bool _LoadTypedURLs()
{
CRegKey rk;
LONG lRet = rk.Open(HKEY_CURRENT_USER,
_T("Software\\Microsoft\\Internet Explorer\\TypedURLs"));
if(lRet != ERROR_SUCCESS)
return false;
std::list<CString> urls;
if (!MtlGetProfileString(rk, std::back_inserter(urls), _T("url"), 1, 25))// get only 25 items
return false;
// std::for_each(urls.begin(), urls.end(), __Mtl_Function_AddToComboBoxEx(hWndComboBoxEx));
T* pT = static_cast<T*>(this);
std::list<CString>::iterator it;
for (it = urls.begin(); it != urls.end(); ++it) {
CString strUrl = *it;
COMBOBOXEXITEM item;
pT->OnGetItem(strUrl, item);
item.iItem = -1;// add to tail
item.pszText = (LPTSTR)(LPCTSTR)strUrl;
InsertItem(&item);
}
return true;
}
HWND _CreateGoButton(UINT nImageBmpID, UINT nHotImageBmpID,
int cx, int cy, COLORREF clrMask, UINT nFlags = ILC_COLOR8)
{
CImageList imgs;
MTLVERIFY(imgs.Create(cx, cy, nFlags | ILC_MASK, 1, 1));
CBitmap bmp;
MTLVERIFY(bmp.LoadBitmap(nImageBmpID));
imgs.Add(bmp, clrMask);
CImageList imgsHot;
MTLVERIFY(imgsHot.Create(cx, cy, nFlags | ILC_MASK, 1, 1));
CBitmap bmpHot;
MTLVERIFY(bmpHot.LoadBitmap(nHotImageBmpID));
imgsHot.Add(bmpHot, clrMask);
m_wndGo = ::CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
ATL_SIMPLE_TOOLBAR_PANE_STYLE | TBSTYLE_LIST | CCS_TOP, 0,0,100,100,
m_hWnd, (HMENU)12, _Module.GetModuleInstance(), NULL);
m_wndGo.SetButtonStructSize(sizeof(TBBUTTON));
// init button size
m_cxGoBtn = _CalcBtnWidth(cx, _T("堏摦"));
m_wndGo.SetButtonSize(CSize(m_cxGoBtn, _CalcBtnHeight(cy)));
m_wndGo.SetImageList(imgs);
m_wndGo.SetHotImageList(imgsHot);
// one button
TBBUTTON btn = { 0, m_nGoBtnCmdID, TBSTATE_ENABLED, TBSTYLE_BUTTON/* | TBSTYLE_AUTOSIZE*/, 0, 0 };
MTLVERIFY(m_wndGo.InsertButton(-1, &btn));
TBBUTTONINFO bi;
memset(&bi, 0, sizeof(bi));
bi.cbSize = sizeof(TBBUTTONINFO);
bi.dwMask = TBIF_TEXT;
bi.pszText = _T("堏摦");
MTLVERIFY(m_wndGo.SetButtonInfo(m_nGoBtnCmdID, &bi));
m_wndGo.AddStrings(_T("NS\0")); // for proper item height
return m_wndGo.m_hWnd;
}
// IDropSource
// Overridables
HRESULT OnGetAddressBarCtrlDataObject(IDataObject** ppDataObject)
{
ATLASSERT(ppDataObject != NULL);
*ppDataObject = NULL;
return E_NOTIMPL;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if (wParam & MK_CONTROL) {
bHandled = FALSE;
return 0;
}
if (_HitTest(pt)) {
_DoDragDrop(pt, (UINT)wParam, true);
}
else
bHandled = FALSE;
return 0;
}
LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if (wParam & MK_CONTROL) {
bHandled = FALSE;
return 0;
}
if (_HitTest(pt)) {
_DoDragDrop(pt, (UINT)wParam, true);
}
else
bHandled = FALSE;
return 0;
}
bool _HitTest(CPoint pt)
{
CRect rc;
m_wndCombo.GetClientRect(rc);
CRect rcEdit;
m_wndEdit.GetWindowRect(&rcEdit);
m_wndCombo.ScreenToClient(&rcEdit);
// CImageList imgs = GetImageList();
// CSize size(0, 0); imgs.GetIconSize(size);
rc.right = rcEdit.left;
if (rc.PtInRect(pt))
return true;
else
return false;
}
void _DoDragDrop(CPoint pt, UINT nFlags, bool bLeftButton)
{
// CRect rc; GetItemRect(nIndex, rc);
// ClientToScreen(rc);
if (PreDoDragDrop(m_hWnd, NULL, false)) {// now dragging
CComPtr<IDataObject> spDataObject;
T* pT = static_cast<T*>(this);
HRESULT hr = pT->OnGetAddressBarCtrlDataObject(&spDataObject);
if (SUCCEEDED(hr)) {
m_bDragFromItself = true;
DROPEFFECT dropEffect = DoDragDrop(spDataObject, DROPEFFECT_MOVE|DROPEFFECT_COPY|DROPEFFECT_LINK);
m_bDragFromItself = false;
}
}
else {// canceled
if (bLeftButton) {
}
else {
m_wndGo.SendMessage(WM_RBUTTONUP, (WPARAM)nFlags, MAKELPARAM(pt.x, pt.y));
}
}
}
};
class CAddressBarCtrl : public CAddressBarCtrlImpl<CAddressBarCtrl>
{
public:
DECLARE_WND_SUPERCLASS(_T("MTL_AddressBar"), GetWndClassName())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -