www.pudn.com > Geotest.rar > SearchDlg.cpp
// SearchDlg.cpp : implementation file
//
#include "stdafx.h"
#include "geotest.h"
#include "SearchDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// SearchDlg dialog
SearchDlg::SearchDlg(CWnd* pParent /*=NULL*/)
: CDialog(SearchDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(SearchDlg)
m_keyword = _T("");
m_Keyword2 = _T("");
//}}AFX_DATA_INIT
m_pMapX=NULL;
}
SearchDlg::SearchDlg(CMapX *pMapX,CWnd* pParent /*=NULL*/)
: CDialog(SearchDlg::IDD, pParent)
{
m_pMapX=pMapX;
}
void SearchDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(SearchDlg)
DDX_Control(pDX, IDC_REFINEFIELD, m_refineField);
DDX_Control(pDX, IDC_REFINELAYER, m_refineLayer);
DDX_Control(pDX, IDC_WORD, m_ctrKeyword);
DDX_Control(pDX, IDC_FINDFIELD, m_findField);
DDX_Control(pDX, IDC_SEARCHLAYERNAME, m_ctrLayerName);
DDX_Text(pDX, IDC_KEYWORD, m_keyword);
DDX_CBString(pDX, IDC_WORD, m_Keyword2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(SearchDlg, CDialog)
//{{AFX_MSG_MAP(SearchDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnOK2)
ON_BN_CLICKED(IDC_BUTTON2, OnCancel2)
ON_CBN_CLOSEUP(IDC_SEARCHLAYERNAME, OnCloseupSearchlayername)
ON_CBN_CLOSEUP(IDC_FINDFIELD, OnCloseupFindfield)
ON_CBN_CLOSEUP(IDC_REFINELAYER, OnCloseupRefinelayer)
ON_CBN_KILLFOCUS(IDC_WORD, OnKillfocusWord)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// SearchDlg message handlers
void SearchDlg::OnOK() //在所以图层中搜索图元
{
try{
UpdateData(TRUE);
CMapXFindFeature FoundObject;
for(long j = 1;j<=m_pMapX->GetLayers().GetCount();j++)
{
FoundObject = m_pMapX->GetLayers().
Item(j).GetFind().Search(m_keyword);//在指定图层搜索指定的图元
if(FoundObject.GetFindRC() %10 == 1) {
m_pMapX->SetZoom(100);
m_pMapX->SetCenterX(FoundObject.GetCenterX());
m_pMapX->SetCenterY(FoundObject.GetCenterY());
break;
}
}
if(FoundObject.GetFindRC() %10 !=1)
AfxMessageBox("No exact match found.");
} catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
} catch (COleException *e) {
e->ReportError();
e->Delete();
}
//CDialog::OnOK();
}
void SearchDlg::OnOK2() // 高级搜索图元
{
UpdateData(TRUE); //!!交换输入的数据
CString cs, //图层名
str, //图元名
field, //字段名
refinelayer, //精确搜索图层名
refinefield; //精确搜索字段名
m_ctrLayerName.GetLBText(m_ctrLayerName.GetCurSel(),cs);
m_ctrKeyword.GetLBText(m_ctrKeyword.GetCurSel(),str);
m_findField.GetLBText(m_findField.GetCurSel(),field);
m_refineLayer.GetLBText(m_findField.GetCurSel(),refinelayer);
m_refineField.GetLBText(m_findField.GetCurSel(),refinefield);
try {
CMapXFindFeature FoundObj;
CMapXLayer Layer;
CMapXDataset Findds; //字段数据集
CMapXLayerFind layerFind;
Layer= m_pMapX->GetLayers().Item(cs);// 得到要查找的图层
layerFind=Layer.GetFind();
layerFind.SetFindDataset(m_pMapX->GetDatasets().Item(cs)); //设置匹配的数据集字段
//设置相匹配的数据集字段,和FindDataset一起使用
layerFind.SetFindField(m_pMapX->GetDatasets().Item(cs).GetFields().Item(field));
//layerFind.SetClosestAddr(TRUE);//是否使用在最精确处查到到的匹配
if(refinelayer != "不选")
layerFind.SetRefineLayer(m_pMapX->GetLayers().Item(refinelayer)); //设置用于精确搜索的图层
if(refinefield != "不选")
layerFind.SetRefineField(m_pMapX->GetDatasets().Item(refinelayer).GetFields().Item(refinefield)); //设置精确搜索的字段
FoundObj = m_pMapX->GetLayers().Item(cs).GetFind().Search(m_Keyword2); //搜索
if(FoundObj.GetFindRC() % 10 == 1) {
m_pMapX->SetZoom(500);
m_pMapX->SetCenterX(FoundObj.GetCenterX());
m_pMapX->SetCenterY(FoundObj.GetCenterY());
} else
AfxMessageBox("No exact match found !");
} catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
} catch (COleException *e) {
e->ReportError();
e->Delete();
}
CDialog::OnOK();
}
BOOL SearchDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_keyword = "Houston";
m_Keyword2 = "请输入或选中图元";
//设置初始图层列表项
m_ctrLayerName.ResetContent();
for(long t=1;t<=m_pMapX->GetLayers().GetCount();t++)
{
m_ctrLayerName.AddString(m_pMapX->GetLayers().Item(t).GetName());
}
m_ctrLayerName.SetCurSel(0);
CString str,cs;
//设置初始字段选项
m_findField.ResetContent();
long getSel = m_ctrLayerName.GetCurSel();
m_ctrLayerName.GetLBText(getSel,str); //把选中的项的字符串拷贝给STR
for(long j = 1;j<=m_pMapX->GetDatasets().Item(str).GetFields().GetCount();j++)
{
if(m_pMapX->GetDatasets().Item(str).GetFields().Item(j).GetType()==miTypeString)
m_findField.AddString(m_pMapX->GetDatasets().Item(str).GetFields().Item(j).GetName());
else
continue;
}
m_findField.SetCurSel(0);
//设置初始精确搜索图层选项
m_refineLayer.ResetContent();
for(t=1;t<=m_pMapX->GetLayers().GetCount();t++)
{
if(m_pMapX->GetLayers().Item(str) != m_pMapX->GetLayers().Item(t))
m_refineLayer.AddString(m_pMapX->GetLayers().Item(t).GetName());
}
m_refineLayer.InsertString(0,"不选");
m_refineLayer.SetCurSel(0);
//设置初始精确搜索字段选项
m_refineField.ResetContent();
m_refineField.AddString("不选");
m_refineField.SetCurSel(0);
//设置初始图元选项
OnSelFeature();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void SearchDlg::OnCancel2()
{
CDialog :: OnCancel();
}
void SearchDlg::OnCloseupSearchlayername()
{
CString str;
m_ctrLayerName.GetLBText(m_ctrLayerName.GetCurSel(),str);
//动态设置字段选项
m_findField.ResetContent();
for(long t=1;t<=m_pMapX->GetDatasets().Item(str).GetFields().GetCount();t++)
{
if(m_pMapX->GetDatasets().Item(str).GetFields().Item(t).GetType()==miTypeString)
m_findField.AddString(m_pMapX->GetDatasets().Item(str).GetFields().Item(t).GetName());
else
continue;
}
m_findField.SetCurSel(0);
//动态设置精确搜索图层选项
m_refineLayer.ResetContent();
for(t=1;t<=m_pMapX->GetLayers().GetCount();t++)
{
if(m_pMapX->GetLayers().Item(str) != m_pMapX->GetLayers().Item(t))
m_refineLayer.AddString(m_pMapX->GetLayers().Item(t).GetName());
}
m_refineLayer.InsertString(0,"不选");
m_refineLayer.SetCurSel(0);
//动态设置图元选项
OnSelFeature();
UpdateData(FALSE);
}
//动态设置图元选项
void SearchDlg::OnSelFeature()
{
CString str, //图层名
cs; //字段名
m_ctrLayerName.GetLBText(m_ctrLayerName.GetCurSel(),str);
m_findField.GetLBText(m_findField.GetCurSel(),cs);
for(long j = 1;j<=m_pMapX->GetDatasets().Item(str).GetFields().GetCount();j++)
{
if(m_pMapX->GetDatasets().Item(str).GetFields().Item(j) ==
m_pMapX->GetDatasets().Item(str).GetFields().Item(cs))
break;
}
//动态设置字段选项
m_ctrKeyword.ResetContent();
for(long t = 1;t<=m_pMapX->GetDatasets().Item(str).GetRowCount()&&t<200;t++)
{//刚才就是这里错了,得到行数的方法竟然写成了得到列数。不应该呀,以后要注意,这样很浪费时间
COleVariant variant = m_pMapX->GetDatasets().Item(str).GetValue(t,j); //得到t行,J列的图元
variant.ChangeType(VT_BSTR);
CString buffer = variant.bstrVal;
SysFreeString(variant.bstrVal);
m_ctrKeyword.AddString(buffer);
}
m_ctrKeyword.SetCurSel(0);
}
void SearchDlg::OnCloseupFindfield()
{
OnSelFeature();
UpdateData(FALSE);
}
void SearchDlg::OnCloseupRefinelayer()
{
CString str;
m_refineLayer.GetLBText(m_refineLayer.GetCurSel(),str);
if(str=="不选")
{
m_refineField.ResetContent();
m_refineField.AddString("不选");
m_refineField.SetCurSel(0);
return;
}
m_refineField.ResetContent();
for(long i = 1;i<=m_pMapX->GetDatasets().Item(str).GetFields().GetCount();i++)
{
m_refineField.AddString(m_pMapX->GetDatasets().Item(str).GetFields().Item(i).GetName());
}
m_refineField.SetCurSel(0);
}
//用户手动输入结束后的映射
void SearchDlg::OnKillfocusWord()
{
UpdateData(TRUE);//一定要有这个
m_ctrKeyword.InsertString(0,m_Keyword2);
UpdateData(FALSE);
}