click点击一次,执行多次的问题修复(累加绑定)

本文探讨了解决网页中因元素改变而触发多次加载的问题,通过使用jQuery的one方法替代on方法,有效避免了重复加载。同时,强调了上传文件后清空输入框的重要性,以确保change事件正确触发。

首先感谢tigerto博主的问题分享,连接如下:
链接
1、如果出现了点击一次但是加载了多次,不要惊慌,想想是什么原因可能导致的这个问题,然后结合着网上的一些内容冷静分析问题所在。

比如我出现的问题就是改变就会加载uploadImage2changeNew函数,而且设计的内容比较复杂和耦合比较严重。

改变前:
 $("#uploadImage2").on("change", uploadImage2changeNew);
修改后:
 $("#uploadImage2").one("change", uploadImage2changeNew);

** 是不是很简单,不要忘记如果是上传文件的时候每次上传成功或者失败后都要将原来的值清空,类似下面的方法。如果您不清空,那么在上传原文件(文件的大小和占用空间发生了修改但是文件路径和文件名称没有发生修改) 那么还会认为是同一个文件,并没有发生改变(change事件)所以将不会执行里面的函数****

 $("#uploadImage2").val("");

2、获取标签中的value可以类似下面的做法,而且网上有很多其他的的类似方法可以借鉴:
必须要使用text() 进行获取lable中的数据,如果用val()是不能后获取到value值的。

var szd = $("input[name='szd']:checked").val();
var confirmLocationAddrName = $("#szd").text();
进行赋值和val()是一样的
$("#confirmLocationAddrName").text("认定所在地详细地址:");

下面还提供了另外两种方法,但是没有测试哦。

$("label#userid").text();   // 首选,获取label的文本
$("label#userid").html();   // 也可以实现,获取label标签内的所有html标记,一般情况改下label标签内就是文本,所以等效上面的方法
<template> <Teleport to="body"> <div v-if="visible" class="pdf-mask" @mousedown="startDrag"> <div ref="modalRef" class="pdf-modal" :style="modalStyle"> <!-- PDF 头部:标题 + 操作按钮 --> <div class="pdf-header"> <span class="title">报关资料</span> <div class="tools"> <el-button size="small" @click="zoomIn">放大</el-button> <el-button size="small" @click="zoomOut">缩小</el-button> <el-button size="small" @click="close">关闭</el-button> </div> </div> <!-- PDF 标签页 + 内容区域 --> <el-tabs v-model="activeTab" class="pdf-tabs" @tab-click="handleTabClick"> <el-tab-pane v-for="tab in tabs" :key="tab.id" :label="tab.name" :name="tab.id.toString()" <!-- 统一为字符串,避免 el-tabs 类型警告 --> > <!-- 单个 PDF 滚动容器(每个 tab 独立 ref,修复类型断言) --> <div class="pdf-body" :ref="(el) => el && scrollRefMap.set(tab.id, el as HTMLDivElement)" <!-- 显式类型断言 --> @wheel="(e) => onWheel(e, tab.id)" @mousedown="(e) => onPdfMouseDown(e, tab.id)" > <!-- PDF 渲染容器(每个 tab 独立存储,修复类型断言) --> <div :ref="(el) => el && setPdfContainer(tab.id, el as HTMLDivElement)" /> </div> </el-tab-pane> </el-tabs> </div> </div> </Teleport> </template> <script setup lang="ts"> import { ref, reactive, watch, nextTick, onUnmounted } from 'vue' import type { CSSProperties } from 'vue' // 导入 CSS 类型 import * as pdfjsLib from 'pdfjs-dist' import pdfjsWorker from 'pdfjs-dist/build/pdf.worker?worker&url' // 导入 pdfjs 核心类型(解决 PDFLoadingTask/promise 类型错误) import type { PDFLoadingTask, PDFDocumentProxy, PageViewport, RenderTask } from 'pdfjs-dist' // 导入 Element Plus 组件(确保类型正确) import { ElButton, ElTabs, ElTabPane } from 'element-plus' import type { TabsPaneContext } from 'element-plus' // PDF.js 初始化:设置 Worker 路径 pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker /* ---------------- Props & Emits 定义 ---------------- */ const props = defineProps<{ visible: boolean url?: string highlight?: { x0: number; y0: number; x1: number; y1: number } | null }>() const emit = defineEmits<{ (e: 'update:visible', value: boolean): void (e: 'tabschange', tabId: string | number): void }>() /* ---------------- 状态管理(每个 tab 独立数据) ---------------- */ // 标签页数据:id/名称/PDF 地址 const tabs = ref<Array<{ id: string | number; name: string; url: string }>>([]) // 当前激活的标签页(统一为字符串类型,适配 el-tabs) const activeTab = ref<string>('') // 每个 tab 的 PDF 渲染容器(DOM 引用,明确类型) const pdfContainerMap = new Map<string | number, HTMLDivElement>() // 每个 tab 的滚动容器(DOM 引用,明确类型) const scrollRefMap = new Map<string | number, HTMLDivElement>() // 每个 tab 的缩放比例(0.5~3 范围) const tabScale = new Map<string | number, number>() // 每个 tab 的 PDF 原始视口(用于计算坐标,明确类型) const tabBaseViewport = new Map<string | number, PageViewport>() // PDF 真实渲染尺寸 const pdfRealSize = ref({ width: 0, height: 0 }) // 弹窗 DOM 引用(明确类型) const modalRef = ref<HTMLDivElement | null>(null) // 异步渲染中断控制器(防止切换 tab 时重复渲染) let abortController: AbortController | null = null // 当前 PDF 拖拽对应的滚动容器(解决事件参数不匹配问题) let currentScrollContainer: HTMLDivElement | null = null /* ---------------- 弹窗位置 & 样式(修复 cursor 类型) ---------------- */ // 显式声明 modalStyle 类型,cursor 支持所有 CSS cursor 值 const modalStyle = reactive<{ left: string top: string width: string height: string cursor: CSSProperties['cursor'] // 关键:使用 CSS 原生 cursor 类型 }>({ left: '0px', top: '0px', width: '800px', height: '600px', cursor: 'default' // 初始值符合类型 }) // 拖拽起始坐标 let dragStart: { x: number; y: number } | null = null /* ---------------- 标签页初始化 & 渲染 ---------------- */ /** * 打开 PDF 标签页(外部调用入口) * @param list 标签页数据:code(唯一ID)/name(标签名)/url(PDF地址) */ async function openWithTabs( list: Array<{ code: string; name: string; url: string }> ) { // 1. 清空历史数据(防止前一次残留导致重复) tabs.value = [] pdfContainerMap.clear() scrollRefMap.clear() tabScale.clear() tabBaseViewport.clear() if (abortController) { abortController.abort() abortController = null } // 2. 格式化标签页数据(过滤 all_files 项) tabs.value = list .filter(item => item.name !== 'all_files') .map(item => ({ id: item.code, name: item.name, url: item.url })) // 3. 激活第一个标签页(转为字符串,适配 el-tabs) const firstTabId = tabs.value[0]?.id.toString() ?? '' activeTab.value = firstTabId emit('update:visible', true) // 4. 等待 DOM 更新后,渲染第一个标签页的 PDF await nextTick() const firstTab = tabs.value[0] if (firstTab) { const container = pdfContainerMap.get(firstTab.id) if (container) { await renderSinglePDF(firstTab.url, container, firstTab.id) } } // 5. 弹窗居中显示 const windowWidth = window.innerWidth const windowHeight = window.innerHeight modalStyle.left = `${(windowWidth - 800) / 2}px` modalStyle.top = `${(windowHeight - 600) / 2}px` } /** * 绑定 PDF 容器到 Map(明确类型) * @param tabId 标签页唯一ID * @param el DOM 元素(已断言为 HTMLDivElement) */ function setPdfContainer(tabId: string | number, el: HTMLDivElement) { pdfContainerMap.set(tabId, el) } /** * 渲染单个标签页的 PDF(核心渲染逻辑,修复 pdfjs 类型) * @param pdfUrl PDF 文件地址 * @param container 渲染容器(明确 HTMLDivElement 类型) * @param currentTabId 当前标签页ID(防止异步渲染错位) */ async function renderSinglePDF( pdfUrl: string, container: HTMLDivElement, currentTabId: string | number ) { // 校验:当前标签页是否已切换,若切换则终止渲染 if (activeTab.value !== currentTabId.toString()) return // 校验:容器是否合法 if (!(container instanceof HTMLDivElement)) return // 1. 中断前一次未完成的渲染(解决重复加载) if (abortController) { abortController.abort() abortController = null } abortController = new AbortController() // 2. 清空容器(删除所有旧内容,防止残留) container.innerHTML = '' // 获取当前标签页的滚动容器 const scrollContainer = scrollRefMap.get(currentTabId) if (!scrollContainer) return try { // 3. 加载 PDF 文件(显式声明 PDFLoadingTask 类型,解决 promise 类型错误) const pdfLoadingTask: PDFLoadingTask<PDFDocumentProxy> = pdfjsLib.getDocument({ url: pdfUrl, signal: abortController.signal }) const pdfDoc = await pdfLoadingTask.promise // 现在 TS 能识别 promise 属性 // 渲染第一页(显式声明 Page 类型) const pdfPage = await pdfDoc.getPage(1) // 再次校验标签页(异步延迟防护) if (activeTab.value !== currentTabId.toString()) return // 4. 计算渲染尺寸(基于当前标签页的缩放比例) const baseViewport = pdfPage.getViewport({ scale: 1 }) tabBaseViewport.set(currentTabId, baseViewport) const currentScale = tabScale.get(currentTabId) ?? 1 // 默认缩放 1 // 基准宽度 800px,适配弹窗初始宽度 const renderScale = currentScale * (800 / baseViewport.width) const renderViewport = pdfPage.getViewport({ scale: renderScale }) // 5. 创建 Canvas 并渲染 PDF const canvas = document.createElement('canvas') const ctx = canvas.getContext('2d') if (!ctx) { container.innerHTML = '<div style="padding:20px;text-align:center;color:#f56c6c;">Canvas 初始化失败</div>' return } canvas.width = renderViewport.width canvas.height = renderViewport.height canvas.dataset.scale = String(currentScale) // 存储当前缩放比例 container.appendChild(canvas) // 渲染 PDF 内容(显式声明 RenderTask 类型,解决参数类型警告) const renderTask: RenderTask = pdfPage.render({ canvasContext: ctx, viewport: renderViewport, signal: abortController.signal } as Parameters<typeof pdfPage.render>[0]) // 类型断言匹配渲染参数 await renderTask.promise // 第三次校验标签页(渲染完成后防护) if (activeTab.value !== currentTabId.toString()) { container.innerHTML = '' return } // 6. 设置滚动容器尺寸(适配 PDF 渲染尺寸) scrollContainer.style.width = `${renderViewport.width}px` scrollContainer.style.height = `${renderViewport.height}px` pdfRealSize.value = { width: renderViewport.width, height: renderViewport.height } // 7. 若有高亮需求,渲染高亮区域 if (props.highlight) { drawHighlight(props.highlight, currentTabId) } } catch (error) { // 忽略中断错误,其他错误提示 if ((error as Error).name !== 'AbortError') { console.error(`PDF 加载失败(标签页: ${currentTabId}):`, error) container.innerHTML = `<div style="padding:20px;text-align:center;color:#f56c6c;">PDF 加载失败,请重试</div>` } } } /* ---------------- 标签页切换 & 缩放 ---------------- */ /** * 切换标签页时触发(防止 el-tabs 自带事件与 watch 冲突) */ function handleTabClick(tab: TabsPaneContext) { emit('tabschange', tab.paneName) } /** * 监听 activeTab 变化:切换标签页时重新渲染 PDF */ watch(activeTab, (newTabIdStr) => { // 将字符串 tabId 转回原始类型(适配 Map 存储的 key) const newTabId = tabs.value.find(tab => tab.id.toString() === newTabIdStr)?.id if (!newTabId) return const targetTab = tabs.value.find(tab => tab.id === newTabId) const targetContainer = pdfContainerMap.get(newTabId) if (targetTab && targetContainer) { renderSinglePDF(targetTab.url, targetContainer, newTabId) } }, { immediate: false }) /** * 放大 PDF(当前激活标签页) */ const zoomIn = () => { const currentTabIdStr = activeTab.value const currentTabId = tabs.value.find(tab => tab.id.toString() === currentTabIdStr)?.id if (!currentTabId) return // 缩放范围:0.5 ~ 3 const newScale = Math.min((tabScale.get(currentTabId) ?? 1) + 0.2, 3) tabScale.set(currentTabId, newScale) rerenderActiveTab() } /** * 缩小 PDF(当前激活标签页) */ const zoomOut = () => { const currentTabIdStr = activeTab.value const currentTabId = tabs.value.find(tab => tab.id.toString() === currentTabIdStr)?.id if (!currentTabId) return // 缩放范围:0.5 ~ 3 const newScale = Math.max((tabScale.get(currentTabId) ?? 1) - 0.2, 0.5) tabScale.set(currentTabId, newScale) rerenderActiveTab() } /** * 重新渲染当前激活的标签页 */ async function rerenderActiveTab() { const currentTabIdStr = activeTab.value const currentTabId = tabs.value.find(tab => tab.id.toString() === currentTabIdStr)?.id if (!currentTabId) return const targetTab = tabs.value.find(tab => tab.id === currentTabId) const targetContainer = pdfContainerMap.get(currentTabId) if (targetTab && targetContainer) { await renderSinglePDF(targetTab.url, targetContainer, currentTabId) } } /* ---------------- 弹窗拖拽功能 ---------------- */ /** * 开始拖拽弹窗(仅头部和遮罩区域触发) */ function startDrag(e: MouseEvent) { const target = e.target as HTMLElement // 仅允许点击遮罩或头部拖拽 if (!target.classList.contains('pdf-mask') && !target.closest('.pdf-header')) { return } if (!modalRef.value) return dragStart = { x: e.clientX, y: e.clientY } modalStyle.cursor = 'grabbing' // 现在类型兼容(CSSProperties['cursor'] 支持) // 绑定全局拖拽/停止事件(使用同一函数引用) document.addEventListener('mousemove', onDrag) document.addEventListener('mouseup', stopDrag) e.preventDefault() } /** * 拖拽中:更新弹窗位置 */ function onDrag(e: MouseEvent) { if (!dragStart) return const dx = e.clientX - dragStart.x const dy = e.clientY - dragStart.y // 更新弹窗位置(基于当前位置累加偏移) modalStyle.left = `${parseFloat(modalStyle.left) + dx}px` modalStyle.top = `${parseFloat(modalStyle.top) + dy}px` // 更新拖拽起始点 dragStart = { x: e.clientX, y: e.clientY } } /** * 停止拖拽:清理事件和状态 */ function stopDrag() { dragStart = null modalStyle.cursor = 'default' // 解绑全局事件(与绑定的函数一致) document.removeEventListener('mousemove', onDrag) document.removeEventListener('mouseup', stopDrag) } /* ---------------- PDF 内容拖拽(放大后支持,修复事件参数) ---------------- */ let isPdfDragging = false // 是否处于 PDF 拖拽状态 let pdfDragStart = { x: 0, y: 0 } // PDF 拖拽起始坐标 let scrollStart = { left: 0, top: 0 } // 滚动容器起始位置 /** * PDF 内容鼠标按下:开始拖拽(仅放大时支持,存储当前滚动容器) */ function onPdfMouseDown(e: MouseEvent, tabId: string | number) { const scrollContainer = scrollRefMap.get(tabId) if (!scrollContainer) return // 仅当缩放比例 >1 时允许拖拽 const currentScale = tabScale.get(tabId) ?? 1 if (currentScale <= 1) return isPdfDragging = true pdfDragStart = { x: e.clientX, y: e.clientY } scrollStart = { left: scrollContainer.scrollLeft, top: scrollContainer.scrollTop } // 存储当前拖拽对应的滚动容器(解决事件参数不匹配) currentScrollContainer = scrollContainer // 绑定全局拖拽/停止事件(使用无额外参数的函数) document.addEventListener('mousemove', onPdfMouseMove) document.addEventListener('mouseup', onPdfMouseUp) e.preventDefault() } /** * PDF 内容拖拽中:更新滚动位置(不依赖外部参数,从状态获取容器) */ function onPdfMouseMove(e: MouseEvent) { if (!isPdfDragging || !currentScrollContainer) return const dx = e.clientX - pdfDragStart.x const dy = e.clientY - pdfDragStart.y // 反向滚动(拖拽方向与滚动方向相反) currentScrollContainer.scrollTo({ left: scrollStart.left - dx, top: scrollStart.top - dy, behavior: 'auto' // 即时滚动,无动画 }) } /** * PDF 内容拖拽停止:清理事件和状态 */ function onPdfMouseUp() { isPdfDragging = false currentScrollContainer = null // 清空当前滚动容器 // 解绑全局事件(与绑定的函数一致) document.removeEventListener('mousemove', onPdfMouseMove) document.removeEventListener('mouseup', onPdfMouseUp) } /* ---------------- 滚轮事件(防止冒泡影响页面) ---------------- */ function onWheel(e: WheelEvent, tabId: string | number) { e.stopPropagation() // 阻止滚轮事件冒泡到页面 const scrollContainer = scrollRefMap.get(tabId) if (scrollContainer) { // 自定义滚轮行为(横向+纵向滚动) scrollContainer.scrollLeft -= e.deltaX scrollContainer.scrollTop -= e.deltaY e.preventDefault() // 防止页面整体滚动 } } /* ---------------- 高亮区域绘制 ---------------- */ /** * 绘制 PDF 高亮区域(支持外部调用) * @param rect 高亮坐标(PDF 原始坐标) * @param tabId 标签页ID(默认当前激活标签页) */ function drawHighlight( rect: { x0: number; y0: number; x1: number; y1: number }, tabId?: string | number ) { const targetTabId = tabId ?? ( tabs.value.find(tab => tab.id.toString() === activeTab.value)?.id ?? '' ) const pdfContainer = pdfContainerMap.get(targetTabId) const scrollContainer = scrollRefMap.get(targetTabId) if (!pdfContainer || !scrollContainer) return // 1. 清除旧的高亮区域 pdfContainer.querySelectorAll('.pdf-highlight').forEach(el => el.remove()) // 2. 获取 Canvas 和基础数据 const canvas = pdfContainer.querySelector('canvas') const baseViewport = tabBaseViewport.get(targetTabId) if (!canvas || !baseViewport) return const currentScale = parseFloat(canvas.dataset.scale || '1') // 计算 PDF 原始坐标到 Canvas 渲染坐标的比例 const scaleX = canvas.width / baseViewport.width const scaleY = canvas.height / baseViewport.height // 3. 计算高亮区域在 Canvas 上的位置和尺寸 const highlightStyle = { left: `${rect.x0 * scaleX}px`, top: `${rect.y0 * scaleY}px`, width: `${(rect.x1 - rect.x0) * scaleX}px`, height: `${(rect.y1 - rect.y0) * scaleY}px` } // 4. 创建高亮元素 const highlightEl = document.createElement('div') highlightEl.className = 'pdf-highlight' Object.assign(highlightEl.style, { ...highlightStyle, position: 'absolute', background: 'rgba(0, 255, 0, 0.4)', border: '2px dashed #ff4d4f', pointerEvents: 'none', // 不影响鼠标事件 zIndex: 9999 // 确保高亮在最上层 }) // 5. 添加到容器并自动滚动到高亮区域 pdfContainer.style.position = 'relative' pdfContainer.appendChild(highlightEl) // 等待 DOM 更新后执行滚动 nextTick(() => { const containerWidth = scrollContainer.clientWidth const containerHeight = scrollContainer.clientHeight // 滚动到高亮区域中心 scrollContainer.scrollTo({ left: Math.max(0, parseFloat(highlightStyle.left) + parseFloat(highlightStyle.width) / 2 - containerWidth / 2), top: Math.max(0, parseFloat(highlightStyle.top) + parseFloat(highlightStyle.height) / 2 - containerHeight / 2), behavior: 'smooth' // 平滑滚动 }) }) } /** * 外部调用的高亮方法(适配暴露接口) */ function drawHighlightByCoords(rect: { x0: number; y0: number; x1: number; y1: number }) { drawHighlight(rect) } /* ---------------- 监听 Props 变化 ---------------- */ // 监听 visible 变化:弹窗显示/隐藏时的处理 watch( () => props.visible, async (isVisible, oldVisible) => { // 弹窗隐藏时:重置缩放和中断渲染 if (!isVisible) { tabScale.clear() if (abortController) { abortController.abort() abortController = null } currentScrollContainer = null // 清空拖拽容器状态 return } // 弹窗从隐藏变为显示时:居中弹窗 await nextTick() if (oldVisible === false) { const windowWidth = window.innerWidth const windowHeight = window.innerHeight modalStyle.left = `${(windowWidth - 800) / 2}px` modalStyle.top = `${(windowHeight - 600) / 2}px` } }, { immediate: true } ) // 监听 highlight 变化:外部传入高亮坐标时绘制 watch( () => props.highlight, (newHighlight) => { if (newHighlight && activeTab.value) { drawHighlight(newHighlight) } }, { immediate: false, deep: true } ) /* ---------------- 关闭弹窗 ---------------- */ function close() { // 中断渲染并重置状态 if (abortController) { abortController.abort() abortController = null } currentScrollContainer = null pdfRealSize.value = { width: 0, height: 0 } emit('update:visible', false) } /* ---------------- 组件卸载清理 ---------------- */ onUnmounted(() => { // 1. 中断所有未完成的渲染 if (abortController) { abortController.abort() abortController = null } // 2. 解绑所有全局事件(防止内存泄漏,确保与绑定函数一致) document.removeEventListener('mousemove', onDrag) document.removeEventListener('mouseup', stopDrag) document.removeEventListener('mousemove', onPdfMouseMove) document.removeEventListener('mouseup', onPdfMouseUp) // 3. 清空所有状态 pdfContainerMap.clear() scrollRefMap.clear() tabScale.clear() tabBaseViewport.clear() currentScrollContainer = null }) /* ---------------- 暴露外部调用的方法 ---------------- */ defineExpose({ openWithTabs, // 打开 PDF 标签页 drawHighlight: drawHighlightByCoords // 绘制高亮区域 }) </script> <style scoped> /* 遮罩层:全屏覆盖 */ .pdf-mask { position: fixed; inset: 0; z-index: 9999; pointer-events: none; /* 仅弹窗区域可点击 */ background: rgba(0, 0, 0, 0.3); /* 增加半透明背景,提升体验 */ } /* 弹窗容器:可拖拽、可缩放 */ .pdf-modal { position: absolute; background: #ffffff; border: 1px solid #e4e7ed; border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15); display: flex; flex-direction: column; pointer-events: auto; min-width: 400px; min-height: 300px; resize: both; /* 允许手动缩放弹窗 */ overflow: hidden; } /* 弹窗头部:标题 + 操作按钮 */ .pdf-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: #f5f7fa; cursor: grab; /* 拖拽光标 */ border-bottom: 1px solid #e4e7ed; user-select: none; /* 禁止文本选中 */ } .pdf-header:active { cursor: grabbing; /* 拖拽中光标 */ } .pdf-header .title { font-size: 16px; font-weight: 500; color: #1f2937; } .pdf-header .tools { display: flex; gap: 8px; /* 按钮间距 */ } /* 标签页容器:占满弹窗剩余高度 */ .pdf-tabs { flex: 1; display: flex; flex-direction: column; overflow: hidden; } /* PDF 内容容器:滚动区域 */ .pdf-body { flex: 1; overflow: auto; position: relative; /* 为高亮区域提供定位上下文 */ } /* PDF Canvas:禁止鼠标事件(避免影响拖拽) */ .pdf-body canvas { pointer-events: none; display: block; /* 消除 Canvas 默认空隙 */ } /* 高亮区域样式 */ .pdf-highlight { box-sizing: border-box; /* 边框不影响尺寸计算 */ } /* 适配 Element Plus 标签页样式(避免冲突) */ .pdf-tabs .el-tabs__content { flex: 1; overflow: hidden; padding: 0 !important; /* 清除默认内边距 */ } .pdf-tabs .el-tab-pane { height: 100%; display: flex; flex-direction: column; } </style>这个代码里面有个别是错误的,比如没有PDFLoadingTask,需要帮我重新分析下代码,功能逻辑,把错误的更新掉,并给我重新生成代码
最新发布
08-28
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Display; namespace WaterPipelineGIS2 { public partial class Form1 : Form { private IFeatureLayer _selectedFeatureLayer; private System.Drawing.Point _lastRightClickPosition; private enum SelectionMode { None, Rectangle, Circle, Polygon, Polyline } private SelectionMode currentMode = SelectionMode.None; public Form1() { InitializeComponent(); axMapControl1.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControl1_OnMouseMove); } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { // 获取地图坐标 double mapX = e.mapX; double mapY = e.mapY; // 格式化坐标显示(保留3位小数) lblCoordinate.Text = string.Format("X: {0:F3} Y: {1:F3}", mapX, mapY); // 立即刷新状态栏 statusStrip1.Refresh(); } private void toolStripMenuItem2_Click(object sender, EventArgs e) { using (var rotateForm = new RotateForm()) { if (rotateForm.ShowDialog() == DialogResult.OK) { try { axMapControl1.Rotation = rotateForm.RotationAngle; axMapControl1.ActiveView.Refresh(); // 可选:更新状态栏 lblCoordinate.Text += " 旋转角度:{rotateForm.RotationAngle}°"; } catch (Exception ex) { MessageBox.Show("旋转失败:{ex.Message}"); } } } } private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { if (e.button == 2) // 右键 { // 保存点击位置(控件坐标系) _lastRightClickPosition = new System.Drawing.Point(e.x, e.y); ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; // 修改点1:声明为接口类型并初始化为null IBasicMap basicMap = null; ILayer layer = null; // 修改点2:使用Type.Missing代替new object() object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 修改点3:正确传递ref参数 tocControl.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref other, ref index); if (itemType == esriTOCControlItem.esriTOCControlItemLayer && layer != null) { contextMenuStripTOC.Show(axTOCControl1, e.x, e.y); } } } // 修改后(使用 MouseEventArgs) private void openAttributeTableToolStripMenuItem_Click(object sender, EventArgs e) { ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; IBasicMap basicMap = null; ILayer layer = null; object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 使用保存的控件坐标系位置 tocControl.HitTest( _lastRightClickPosition.X, _lastRightClickPosition.Y, ref itemType, ref basicMap, ref layer, ref other, ref index ); IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer != null) { _selectedFeatureLayer = featureLayer; // 确保使用正确的构造函数 AttributeTableForm attrForm = new AttributeTableForm( _selectedFeatureLayer, axMapControl1.Object as IMapControl2 ); attrForm.Show(); } else { MessageBox.Show("请选择有效的要素图层!"); } } private void SetSelectionSymbol() { // 使用接口创建符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbol() as ISimpleFillSymbol; fillSymbol.Color = GetRgbColor(255, 0, 0); fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; ISimpleLineSymbol lineSymbol = new SimpleLineSymbol() as ISimpleLineSymbol; lineSymbol.Color = GetRgbColor(255, 255, 0); lineSymbol.Width = 2; fillSymbol.Outline = lineSymbol; // 设置渲染器 if (_selectedFeatureLayer != null) { IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)_selectedFeatureLayer; ISimpleRenderer renderer = new SimpleRenderer() as ISimpleRenderer; renderer.Symbol = (ISymbol)fillSymbol; geoLayer.Renderer = (IFeatureRenderer)renderer; axMapControl1.ActiveView.Refresh(); } } private IRgbColor GetRgbColor(int r, int g, int b) { IRgbColor color = new RgbColor() as IRgbColor; // 正确方式 color.Red = r; color.Green = g; color.Blue = b; return color; } public object _featureLayer { get; set; } // 矩形选择 private void btnRectSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Rectangle; axMapControl1.CurrentTool = null; } // 圆形选择 private void btnCircleSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Circle; axMapControl1.CurrentTool = null; } // 多边形选择 private void btnPolygonSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polygon; axMapControl1.CurrentTool = null; } // 折线选择 private void btnLineSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polyline; axMapControl1.CurrentTool = null; } // 地图鼠标按下事件 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { IGeometry geometry = null; switch (currentMode) { case SelectionMode.Rectangle: // 绘制矩形 geometry = axMapControl1.TrackRectangle(); break; case SelectionMode.Circle: // 获取用户拖拽的矩形范围 IEnvelope envelope = axMapControl1.TrackRectangle(); // 创建圆心点 IPoint center = new PointClass(); center.PutCoords(envelope.XMin + envelope.Width / 2, envelope.YMin + envelope.Height / 2); // 正确创建圆形几何(修复部分) ICircularArc circularArc = new CircularArcClass(); IConstructCircularArc constructArc = (IConstructCircularArc)circularArc; double radius = Math.Min(envelope.Width, envelope.Height) / 2; constructArc.ConstructCircle(center, radius, true); // 创建多边形表示圆形 IGeometryCollection geomColl = new PolygonClass(); geomColl.AddGeometry((IGeometry)circularArc); geometry = (IGeometry)geomColl; geometry.SpatialReference = axMapControl1.SpatialReference; break; case SelectionMode.Polygon: // 绘制多边形 geometry = axMapControl1.TrackPolygon(); break; case SelectionMode.Polyline: // 绘制折线 geometry = axMapControl1.TrackLine(); break; } if (geometry != null) { PerformSpatialSelection(geometry); HighlightGeometry(geometry); } } catch (Exception ex) { MessageBox.Show("操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 执行空间选择 private void PerformSpatialSelection(IGeometry geometry) { IMap map = axMapControl1.Map; // 创建空间过滤器 ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = geometry; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; // 清除之前的选择 map.ClearSelection(); // 遍历所有图层执行选择 for (int i = 0; i < map.LayerCount; i++) { IFeatureLayer featureLayer = map.get_Layer(i) as IFeatureLayer; if (featureLayer == null) continue; spatialFilter.GeometryField = featureLayer.FeatureClass.ShapeFieldName; IFeatureSelection featureSelection = (IFeatureSelection)featureLayer; featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); } // 高亮显示选择图形 private void HighlightGeometry(IGeometry geometry) { IMap map = axMapControl1.Map; IActiveView activeView = map as IActiveView; IGraphicsContainer graphicsContainer = map as IGraphicsContainer; // 清除之前的高亮 graphicsContainer.DeleteAllElements(); // 创建高亮符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRGBColor(255, 0, 0, 50); // 半透明红色 fillSymbol.Outline.Color = GetRGBColor(0, 0, 255); ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRGBColor(0, 0, 255); lineSymbol.Width = 2; // 创建元素 IElement element = null; if (geometry.GeometryType == esriGeometryType.esriGeometryPolygon) { IFillShapeElement fillElement = new PolygonElementClass(); fillElement.Symbol = fillSymbol; element = (IElement)fillElement; } else if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline) { ILineElement lineElement = new LineElementClass(); lineElement.Symbol = lineSymbol; element = (IElement)lineElement; } else if (geometry.GeometryType == esriGeometryType.esriGeometryPoint) { // 圆形选择使用点符号 IMarkerElement markerElement = new MarkerElementClass(); ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = fillSymbol.Color; markerSymbol.Size = 10; markerElement.Symbol = markerSymbol; element = (IElement)markerElement; } if (element != null) { element.Geometry = geometry; graphicsContainer.AddElement(element, 0); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } // 创建颜色对象 private IRgbColor GetRGBColor(int r, int g, int b, int alpha = 255) { IRgbColor color = new RgbColorClass(); color.Red = r; color.Green = g; color.Blue = b; color.Transparency = (byte)(255 - alpha); return color; } // 清除选择 private void btnClearSelection_Click(object sender, EventArgs e) { axMapControl1.Map.ClearSelection(); IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer; graphicsContainer.DeleteAllElements(); axMapControl1.Refresh(); } } }还是点击空间选择中的多边形选择、圆形选择等无法进行选择呢,如何结局
07-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值