Windows/MFC

MSFlexGrid Example For VS2005

aucd29 2013. 10. 2. 17:59
Query
QueryDlg.cpp

[code]
#include "stdafx.h"
#include "QueryDlg.h"

// Type information for the event handler
_ATL_FUNC_INFO OnCurrentViewChangedInfo = {CC_STDCALL, VT_I4, 1, {VT_DISPATCH}};

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg - Event handlers

HRESULT __stdcall CQueryDlg::OnCurrentViewChanged(IDispatch* pNewCurrentView)
{
    bool bEnabled;

    // Set the current tool
    esriARViewType arViewType;
    m_ipARControl->get_CurrentViewType(&arViewType);
    if(esriARToolNoneSelected != arViewType)
    bEnabled = false;
    else
    {
    bEnabled = true;
    // Update the current tool if necessary
    esriARTool arCurrentTool;
    m_ipARControl->get_CurrentARTool(&arCurrentTool);
    if(arCurrentTool != m_ARTool)
     m_ipARControl->put_CurrentARTool(m_ARTool);
    }

    m_btnZoomIn.Enable(bEnabled);
    m_btnZoomOut.Enable(bEnabled);
    m_btnPan.Enable(bEnabled);
    m_btnFullExtent.Enable(bEnabled);
    EnableButtons(true, bEnabled);

    return S_OK;
}

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg - Message handlers

LRESULT CQueryDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    // Get the control interfaces
    GetDlgControl(IDC_AR, IID_IARControl, (void **) &m_ipARControl);
    GetDlgControl(IDC_MSFLEXGRID, IID_IMSFlexGrid, (void **) &m_ipFlexGrid);

    // Set the window icon
    SetIcon(::LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_ICON_APP)));

    // Setup the buttons
    m_btnBrowse.Load(GetDlgItem(IDC_BUTTON_LOADPMF), IDB_BITMAP_BROWSE, IDS_OPEN, true);
    m_btnZoomIn.Load(GetDlgItem(IDC_BUTTON_ZOOMIN), IDB_BITMAP_ZOOMIN, IDS_ZOOMIN, false);
    m_btnZoomOut.Load(GetDlgItem(IDC_BUTTON_ZOOMOUT), IDB_BITMAP_ZOOMOUT, IDS_ZOOMOUT, false);
    m_btnPan.Load(GetDlgItem(IDC_BUTTON_PAN), IDB_BITMAP_PAN, IDS_PAN, false);
    m_btnFullExtent.Load(GetDlgItem(IDC_BUTTON_FULLEXTENT), IDB_BITMAP_FULLEXTENT, IDS_FULLEXTENT, false);

    // Create the custom font
    LOGFONT lf;
    ::GetObject((HFONT)::SendMessage(m_hWnd, WM_GETFONT, 0, 0), sizeof(LOGFONT), &lf);
    CComBSTR strFont;
    strFont.LoadString(IDS_FONT);
    _tcscpy(lf.lfFaceName, strFont);
    m_customFont = ::CreateFontIndirect(&lf);

    // Get this for formatting the flexgrid
    TEXTMETRIC tm;
    ::GetTextMetrics(::GetDC(m_hWnd), &tm);
    m_MaxCharWidth = tm.tmMaxCharWidth;

    // Connect to the AR Control
    AtlAdviseSinkMap(this, true);

    return 0;
}

LRESULT CQueryDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    AtlAdviseSinkMap(this, false);
    ::DeleteObject(m_customFont);
    return 0;
}

LRESULT CQueryDlg::OnClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    EndDialog(0);
    return 0;
}

LRESULT CQueryDlg::OnClickedLoadPMF(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    // Open a file dialog for selecting map documents
    MSComDlg::ICommonDialogPtr pCommonDialog;
    GetDlgControl(IDC_COMMONDIALOG, MSComDlg::IID_ICommonDialog, (void **) &pCommonDialog);
    pCommonDialog->ShowOpen();

    // Exit if no map document is selected
    CComBSTR bstrFileName;
    pCommonDialog->get_FileName(&bstrFileName);
    if(0 == _tcslen(bstrFileName)) return 0;

    // Load the specified pmf
    VARIANT_BOOL bIsOK;
    m_ipARControl->CheckDocument(bstrFileName, &bIsOK);
    if(VARIANT_TRUE == bIsOK)
        m_ipARControl->LoadDocument(bstrFileName, NULL);
    else
    {
        CComBSTR strCaption, strError;
        strCaption.LoadString(IDS_CAPTION);
        strError.LoadString(IDS_LOAD_ERROR);
        MessageBox(strError, strCaption, MB_OK);
        return 0;
    }

    // Determine whether permission to search layers and query field values
    m_ipARControl->HasDocumentPermission(esriARDocumentPermissionsQueryFeatures, &m_bQueryFeatures);
    m_ipARControl->HasDocumentPermission(esriARDocumentPermissionsQueryValues, &m_bQueryValues);

    EnableButtons();

    return 0;
}

LRESULT CQueryDlg::OnClickedZoomIn(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    m_ipARControl->put_CurrentARTool(esriARToolMapZoomIn);
    // Remember the current tool
    m_ARTool = esriARToolMapZoomIn;
    return 0;
}

LRESULT CQueryDlg::OnClickedZoomOut(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    m_ipARControl->put_CurrentARTool(esriARToolMapZoomOut);
    m_ARTool = esriARToolMapZoomOut;
    return 0;
}

LRESULT CQueryDlg::OnClickedPan(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    m_ipARControl->put_CurrentARTool(esriARToolMapPan);
    m_ARTool = esriARToolMapPan;
    return 0;
}

LRESULT CQueryDlg::OnClickedFullExtent(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    // Set extent to full data extent
    double xMin, yMin, xMax, yMax;
    IARPageLayoutPtr ipARPageLayout;
    m_ipARControl->get_ARPageLayout(&ipARPageLayout);
    IARMapPtr ipARFocusMap;
    ipARPageLayout->get_FocusARMap(&ipARFocusMap);
    ipARFocusMap->GetFullExtent(&xMin, &yMin, &xMax, &yMax);
    ipARFocusMap->SetExtent(xMin, yMin, xMax, yMax);
    // Refresh the display
    ipARFocusMap->Refresh(VARIANT_TRUE);
    return 0;
}

LRESULT CQueryDlg::OnClickedQuery(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    // Determine whether permission to search layers
    if(!m_bQueryFeatures)
    {
        CComBSTR strCaption, strError;
        strCaption.LoadString(IDS_CAPTION);
        strError.LoadString(IDS_PERMISSION_ERROR);
        MessageBox(strError, strCaption, MB_OK);
        return 0;
    }

    // Get IARQueryDef interface
    IARSearchDefPtr pSearchDef(CLSID_ArcReaderSearchDef);
    // Set the spatial searching to intersects
    pSearchDef->put_SpatialRelationship(esriARSpatialRelationshipIntersects);

    // Get the layer count
    IARPageLayoutPtr ipARPageLayout;
    m_ipARControl->get_ARPageLayout(&ipARPageLayout);
    IARMapPtr ipARFocusMap;
    ipARPageLayout->get_FocusARMap(&ipARFocusMap);
    long lLayerCnt;
    ipARFocusMap->get_ARLayerCount(&lLayerCnt);

    // Get the coordinates of the current extent
    double dXmin, dYmin, dXmax, dYmax;
    ipARFocusMap->GetExtent(&dXmin, &dYmin, &dXmax, &dYmax);
    // Set the envelope coordinates as the search shape
    pSearchDef->SetEnvelopeShape(dXmin, dYmin, dXmax, dYmax, 0);

    // Get IARFeatureSet interface
    ipARFocusMap->QueryARFeatures(pSearchDef, &m_ipFeatureSet);
    // Reset the featureset
    m_ipFeatureSet->Reset();
    // Get the IARFeature interface
    m_ipFeatureSet->Next(&m_ipFeature);
    // Display attribute values
    m_lRecord = 0;

    UpdateValueDisplay();
    EnableButtons(false);
    return 0;
}

LRESULT CQueryDlg::OnClickedNextFtr(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    // Get the next feature
    m_ipFeatureSet->get_ARFeature(++m_lRecord, &m_ipFeature);
    UpdateValueDisplay();
    return 0;
}

LRESULT CQueryDlg::OnClickedPreviousFtr(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    // Get the previous feature
    m_ipFeatureSet->get_ARFeature(--m_lRecord, &m_ipFeature);
    UpdateValueDisplay();
    return 0;
}

LRESULT CQueryDlg::OnClickedZoomTo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    return m_ipFeature->ZoomTo();
}

LRESULT CQueryDlg::OnClickedCenterAt(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    return m_ipFeature->CenterAt();
}

LRESULT CQueryDlg::OnClickedFlash(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    return m_ipFeature->Flash();
}

LRESULT CQueryDlg::OnClickedFlicker(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    return m_ipFeature->Flicker();
}

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg - Custom Draw message handlers

// Draw the information text in blue
LRESULT CQueryDlg::OnCtlColorEdit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    if((HWND)lParam == GetDlgItem(IDC_STATIC_FTRCOUNT)) return 0;

    HDC hdc = (HDC)wParam;
    ::SetTextColor(hdc, ::GetSysColor(COLOR_HIGHLIGHT));
    ::SetBkMode(hdc, TRANSPARENT);
    ::SelectObject(hdc, m_customFont);
    return (LRESULT)::GetStockObject(HOLLOW_BRUSH);
}

// Draws the button in the appropriate mode
LRESULT CQueryDlg::OnDrawItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    LPDRAWITEMSTRUCT drawItem = (LPDRAWITEMSTRUCT)lParam;

    if(GetDlgItem(IDC_BUTTON_LOADPMF) == drawItem->hwndItem)
        m_btnBrowse.Draw(drawItem);
    else if(GetDlgItem(IDC_BUTTON_ZOOMIN) == drawItem->hwndItem)
        m_btnZoomIn.Draw(drawItem);
    else if(GetDlgItem(IDC_BUTTON_ZOOMOUT) == drawItem->hwndItem)
        m_btnZoomOut.Draw(drawItem);
    else if(GetDlgItem(IDC_BUTTON_PAN) == drawItem->hwndItem)
        m_btnPan.Draw(drawItem);
    else if(GetDlgItem(IDC_BUTTON_FULLEXTENT) == drawItem->hwndItem)
        m_btnFullExtent.Draw(drawItem);

    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg - Helpers

void CQueryDlg::UpdateValueDisplay()
{
    CComVariant varFieldValue;
    CComVariant varFieldName;
    CComBSTR sFieldValue;
    CComBSTR sFieldName;
    SIZE txtSize;
    long lValueWidth = 0;
    long lNameWidth = 0;
    HDC hDC = ::GetDC(m_hWnd);
    long lRow = 0;

    // A grid row for each field
    if(NULL == m_ipFeature)
    m_ipFlexGrid->put_Rows(0);
    else
    {
        long lFldCount;
        m_ipFeature->get_FieldCount(&lFldCount);
        m_ipFlexGrid->put_Rows(lFldCount);
    }

    // For each field that isn't the 'Shape' field
    long lRowCnt;
    m_ipFlexGrid->get_Rows(&lRowCnt);
    for(long i = 0; i < lRowCnt; i++)
    {
        esriARFieldType arFieldType;
        m_ipFeature->get_FieldType(i, &arFieldType);
        if(esriARFieldTypeGeometry != arFieldType &&
             esriARFieldTypeRaster != arFieldType &&
             esriARFieldTypeBlob != arFieldType)
        {
            // Display field names
            m_ipFlexGrid->put_Col(0);
            m_ipFlexGrid->put_Row(lRow);
            m_ipFeature->get_FieldAliasName(i, &sFieldName);
            m_ipFlexGrid->put_Text(sFieldName);
            ::GetTextExtentPoint32(hDC, sFieldName, sFieldName.Length(), &txtSize);
            txtSize.cx *= m_MaxCharWidth;
            if(txtSize.cx > lNameWidth)
                lNameWidth = txtSize.cx;

            // Display field values
            m_ipFlexGrid->put_Col(1);
            m_ipFlexGrid->put_Row(lRow);
            m_ipFlexGrid->put_CellAlignment(flexAlignLeftCenter);
            if(m_bQueryValues)
            {
                m_ipFeature->get_Value(CComVariant(i), &varFieldValue);
                if (varFieldValue.vt != VT_EMPTY)
                {
                    varFieldValue.ChangeType(VT_BSTR);
                    sFieldValue = varFieldValue.bstrVal;
                }
                else
                    sFieldValue = _T("<NULL>");
            }
            else
                sFieldValue.LoadString(IDS_NOPERMISSION);
            m_ipFlexGrid->put_Text(sFieldValue);
            ::GetTextExtentPoint32(hDC, sFieldValue, sFieldValue.Length(), &txtSize);
            txtSize.cx *= m_MaxCharWidth;
            if(txtSize.cx > lValueWidth)
                lValueWidth = txtSize.cx;
            lRow++;
        }
    }

    // Set column widths
    m_ipFlexGrid->put_ColWidth(0, lNameWidth);
    m_ipFlexGrid->put_ColWidth(1, lValueWidth);

    // Enabled/disable controls
    TCHAR tCaption[64];
    BOOL bEnabled;
    long lFtrCount;
    long lCurrFtr;
    m_ipFeatureSet->get_ARFeatureCount(&lFtrCount);
    if(0 == lFtrCount)
    {
        bEnabled = FALSE;
        ::EnableWindow(GetDlgItem(IDC_BUTTON_NEXTFTR), FALSE);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_PREVIOUSFTR), FALSE);
        lCurrFtr = m_lRecord;
    }
    else if(1 == lFtrCount)
    {
        bEnabled = TRUE;
        ::EnableWindow(GetDlgItem(IDC_BUTTON_NEXTFTR), FALSE);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_PREVIOUSFTR), FALSE);
        lCurrFtr = m_lRecord + 1;
    }
    else
    {
        bEnabled = TRUE;
        if(0 == m_lRecord)
            ::EnableWindow(GetDlgItem(IDC_BUTTON_PREVIOUSFTR), FALSE);
        else
            ::EnableWindow(GetDlgItem(IDC_BUTTON_PREVIOUSFTR), TRUE);
        if(lFtrCount == m_lRecord + 1)
            ::EnableWindow(GetDlgItem(IDC_BUTTON_NEXTFTR), FALSE);
        else
            ::EnableWindow(GetDlgItem(IDC_BUTTON_NEXTFTR), TRUE);
        lCurrFtr = m_lRecord + 1;
    }
    _stprintf(tCaption, _T("%ld of %ld\0"), lCurrFtr, lFtrCount);
    ::SetWindowText(GetDlgItem(IDC_STATIC_FTRCOUNT), tCaption);
    ::EnableWindow(GetDlgItem(IDC_BUTTON_ZOOMTO), bEnabled);
    ::EnableWindow(GetDlgItem(IDC_BUTTON_CENTERAT), bEnabled);
    ::EnableWindow(GetDlgItem(IDC_BUTTON_FLASH), bEnabled);
    ::EnableWindow(GetDlgItem(IDC_BUTTON_FLICKER), bEnabled);
}

void CQueryDlg::EnableButtons(bool bMapOnly, bool bEnabled)
{
    BOOL BEnabled = bEnabled ? TRUE : FALSE;

    if(bMapOnly)
    {
        m_btnZoomIn.Enable(bEnabled);
        m_btnZoomOut.Enable(bEnabled);
        m_btnPan.Enable(bEnabled);
        m_btnFullExtent.Enable(bEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_SPATIALQUERY), BEnabled);
    }
    else
    {
        ::EnableWindow(GetDlgItem(IDC_BUTTON_NEXTFTR), BEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_PREVIOUSFTR), BEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_ZOOMTO), BEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_CENTERAT), BEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_FLASH), BEnabled);
        ::EnableWindow(GetDlgItem(IDC_BUTTON_FLICKER), BEnabled);
    }
}
[/code]