重拾VB6(22):Mouse Pointer, Keyboard Events, Interrupting Background Processing

本文介绍了使用Visual Basic定制鼠标指针及响应键盘事件的方法。通过设置属性可以改变鼠标图标,使用预定义指针或加载自定义图标。对于键盘事件,文章详细讲解了KeyPress、KeyUp和KeyDown事件的使用场景,并说明如何编写表单级别的键盘事件处理程序。

来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Programmer’s Guide/Part 2: What Can You Do With Visual Basic/Responding to Mouse and Keyboard Events/

1. Customizing the Mouse Pointer

(1)You can use the MousePointer and MouseIcon properties to display a custom icon, cursor, or any one of a variety of predefined mouse pointers.

(2)With the MousePointer property you can select any one of sixteen predefined pointers.

When you set the MousePointer property for a control, the pointer appears when the mouse is over the corresponding control. When you set the MousePointer property for a form, the selected pointer appears both when the mouse is over blank areas of the form and when the mouse is over controls with the MousePointer property set to 0-Default.

(3)To use a custom icon or cursor, you set both the MousePointer and MouseIcon properties.

a) Select a form or control and set the MousePointer property to 99-Custom.

b) Load an .ico file into the MouseIcon property. For example, for a form:

Form1.MouseIcon = LoadPicture("c:/Program _
Files/Microsoft Visual _
Basic/Icons/Computer/Disk04.ico")

(4)Cursors are .cur files and, like icons, are essentially bitmaps. 

Cursors also contain hot spot information. The hot spot is a pixel which tracks the location of the cursor — the x and y coordinates. Typically, the hot spot is located at the center of the cursor. Icons, when loaded into Visual Basic through the MouseIcon property, are converted to the cursor format and the hot spot is set to the center pixel.

The two differ in that the hot spot location of a .cur file can be changed, whereas that of an .ico file cannot.

Cursor files can be edited in Image Editor, which is available in the Windows SDK.

Visual Basic does not support animated cursor (.ani) files.

2. Responding to Keyboard Events

(1) The KeyPress, KeyUp, and KeyDown events

(2) Only the object that has the focus can receive a keyboard event.

A form has the focus only if it is active and no control on that form has the focus. This happens only on blank forms and forms on which all controls have been disabled. However, if you set the KeyPreview property on a form to True, the form receives all keyboard events for every control on the form before the control recognizes them.

(3) The KeyDown and KeyUp events provide the lowest level of keyboard response. Use these events to detect a condition that the KeyPress event is unable to detect.

Before using the KeyUp and KeyDown events, make sure that the KeyPress event isn't sufficient. This event detects keys that correspond to all the standard ASCII characters: letters, digits, and punctuation on a standard keyboard, as well as the ENTER, TAB, and BACKSPACE keys. It's generally easier to write code for the KeyPress event.

You also should consider using shortcut and access keys. You can assign shortcut keys without writing additional code.

(4) Note that some controls (command buttons, option buttons, and check boxes) do not receive arrow-key events: Instead, arrow keys cause movement to another control.

3. Key codes

(1)

Private Sub Text1_KeyPress (KeyAscii As Integer)
   KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

(2) When the user types uppercase "A," the KeyDown event gets the ASCII code for "A." The KeyDown event gets the same code when the user types lowercase "a." To determine whether the character pressed is uppercase or lowercase, these events use the shift argument. In contrast, the KeyPress event treats the uppercase and lowercase forms of a letter as two separate ASCII characters.

(3) Key codes for letter keys are the same as the ASCII codes of the uppercase character of the letter. So the keycode for both "A" and "a" is the value returned by Asc("A").

4. Writing Form-Level Keyboard Handlers

(1) If you have defined a shortcut key for a menu control, the Click event for that menu control occurs automatically when the user types that key, and no key event occurs.

(2) Similarly, if there is a command button on the form with the Default property set to True, the ENTER key causes the Click event for that command button to occur instead of a key event. If there is a command button with the Cancel property set to True, the ESC key causes the Click event for that command button to occur instead of a key event.

(3) Notice that the TAB key moves the focus from control to control and does not cause a key event unless every control on the form is disabled or has TabStop set to False.

(4) When the KeyPreview property of the form is set to True, the form recognizes the keyboard events before the controls, but the events still occur for the controls. To prevent this, you can set the keyascii or keycode arguments in the form key-event procedures to 0.

Private Sub Form_KeyPress (KeyAscii As Integer)
Dim NextTabIndex As Integer, i As Integer
   If KeyAscii = 13 Then
      If Screen.ActiveControl.TabIndex = _
      Count - 1 Then
         NextTabIndex = 0
      Else
         NextTabIndex = Screen.ActiveControl._
         TabIndex + 1
      End If
      For i = 0 To Count - 1
         If Me.Controls(i).TabIndex = _
         NextTabIndex Then
            Me.Controls(i).SetFocus
            Exit For
         End If
      Next i
      KeyAscii = 0
   End If
End Sub

5. Interrupting Background Processing 

(1) Windows is a preemptively multitasking operating system, which means that idle processor time is efficiently shared among background tasks. Priority is always given to the application that the user is working with, however. This ensures that the mouse and keyboard always respond immediately.

During long background tasks, your application cannot respond to user input. Therefore, you should provide the user with a way to interrupt or cancel the background processing by writing code for either the mouse or keyboard events.

(2) One way to allow users to interrupt a task is to display a Cancel button and allow its Click event to be processed. You can do this by placing the code for your background task in a timer event, using the following guidelines.

  • a) Use static variables for information that must persist between occurrences of the Timer event procedure.
  • b) When the Timer event gets control, allow it to run slightly longer than the time you specified for the Interval property. This ensures that your background task will use every bit of processor time the system can give it. The next Timer event will simply wait in the message queue until the last one is done.
  • c) Use a fairly large value — five to ten seconds — for the timer's Interval property, as this makes for more efficient processing. Preemptive multitasking prevents other applications from being blocked, and users are generally tolerant of a slight delay in canceling a long task.
  • d) Use the Enabled property of the Timer as a flag to prevent the background task from being initiated when it is already running.

(3) Although Timer events are the best tool for background processing, particularly for very long tasks, the DoEvents function provides a convenient way to allow a task to be canceled.

DoEvents switches control to the operating-environment kernel. Control returns to your application as soon as all other applications in the environment have had a chance to respond to pending events. This doesn't cause the current application to give up the focus, but it does enable background events to be processed.

You may want to prevent an event procedure that gives up control with DoEvents from being called again before DoEvents returns. Otherwise, the procedure might end up being called endlessly, until system resources are exhausted. You can prevent this from happening either by temporarily disabling the control or by setting a static "flag" variable, as in the earlier example.

(4) Avoiding DoEvents When Using Global Data

内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多类型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统中的建模与求解实践,帮助用户掌握MOPSO在实际工程问题中的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。
内容概要:本文系统研究了基于模型预测控制(MPC)的滚动优化方法在微电网多时间尺度能量管理调度中的应用。通过构建包含风能、光伏、储能等多种分布式能源的微电网综合系统模型,充分利用MPC的前瞻性预测与滚动优化机制,实现对系统内部能量流的精细化、动态化调控。研究重点解决了新能源出力强不确定性带来的调度挑战,兼顾系统运行的经济性、稳定性与可靠性,在日前、日内及实时等多个时间尺度上实现了优化决策的协同。文中配套提供了完整的Python代码实现,涵盖模型构建、约束处理、目标函数设定与求解全过程,具有较强的可复现性与工程参考价值。; 适合人群:具备一定电力系统、优化理论基础和Python编程能力的研究生、科研人员及从事微电网、综合能源系统、能源互联网等领域研究的工程技术人员。; 使用场景及目标:①深入理解MPC在复杂能源系统调度中的核心原理与技术优势;②学习并复现多时间尺度滚动优化的完整建模与求解流程;③为微电网能量管理系统(EMS)的开发、相关学术研究或工程项目提供直接的算法实现参考与技术支撑; 阅读建议:建议读者结合所提供的Python代码进行逐行研读与调试,亲自动手修改系统参数、负荷曲线或新能源出力数据,以深刻体会MPC算法的动态响应特性与优化效果,进而在此基础上开展二次开发与创新性研究。
智能安防是依托人工智能、大数据、物联网等前沿技术构建的新一代安全防护体系,彻底打破了传统安防“被动监控、事后追溯”的局限。它不再是孤立的摄像头、门禁和报警器的简单组合,而是通过全域感知设备的互联互通,实现对人员、车辆、环境等多维度数据的实时采集与智能分析。从社区出入口的人脸无感通行、异常行为识别,到道路上的违章智能抓拍、重点区域的入侵预警,再到企业园区的消防隐患预判、设备故障自动告警,智能安防能在毫秒级完成风险研判,把安全防线从“事后处置”前移到“事前预防”。如今,它早已渗透到城市治理、居家生活、商业运营等各类场景,成为守护公共安全与私人空间的核心技术支撑。 不同于传统安防依赖人工盯守的高成本模式,智能安防凭借算法的持续迭代,不断拓展安全防护的边界。它可以通过对历史数据的深度挖掘,提前识别人群聚集、消防通道占用等潜在风险,联动公安、物业、应急等多部门快速响应,大幅降低安全事件的发生概率和处置时长。在老旧小区改造中,智能安防设备的加装解决了过去流动人口管理难、高空抛物溯源难等长期痛点;在家庭场景里,智能门锁、可视门铃、燃气泄漏报警器等设备组成的居家安防网络,让用户通过手机就能随时掌握家中安全状态。随着数字城市建设的推进,智能安防正从单一的安全工具,进化为构建智慧城市安全底座的关键组成部分,为人们的日常工作与生活筑牢更高效、更精准的防护屏障。
内容概要:本文针对“考虑算力负荷时空迁移特性的多微电网-共享储能协同优化调度”开展深入研究,提出了一种融合算力负荷动态迁移特征的多微电网系统协同优化模型,并基于Matlab完成仿真代码实现。研究核心在于揭示算力负荷(如数据中心、边缘计算等)与电力负荷之间的耦合关系,通过引入共享储能机制实现多微电网间的能量互补与灵活调度,从而提升系统在复杂时空负荷环境下的运行经济性、稳定性与能源利用效率。文中系统阐述了模型架构设计、多目标优化函数构建(涵盖成本最小化、可再生能源消纳最大化等)、关键约束条件(如功率平衡、储能容量、网络潮流等)以及高效求解算法的应用,具备较强的理论深度与工程实践价值。; 适合人群:具备电力系统、能源互联网、优化理论或智能调度相关基础知识,从事微电网运行、共享储能配置、算力与能源协同管理等领域研究的研究生、科研人员及工程技术开发者。; 使用场景及目标:①应用于含有动态算力负荷的多微电网系统协同调度优化决策;②为共享储能资源的规划配置、运行策略制定及商业模式设计提供量化分析工具;③推动“东数西算”背景下能源与算力基础设施的深度融合与协同发展。; 阅读建议:建议结合Matlab代码实现部分进行动手仿真实验,重点关注算力负荷时空特性建模方法与优化模型求解过程的实现细节,推荐使用实际历史数据或典型场景进行验证,并尝试拓展至更复杂的网络结构或多目标权衡分析。
内容概要:本文围绕考虑能量-物流耦合的港口综合能源系统优化调度问题展开研究,构建了涵盖电能、氢能、热能等多种能源形式与港口货物装卸、运输等物流活动协同优化的数学模型。研究采用Matlab进行代码实现,充分考虑风能等可再生能源出力的不确定性及时序性作业特征,提出一种能够有效降低系统运行成本、提升能源综合利用效率并减少碳排放的优化调度策略。文中系统阐述了目标函数设计、多类型约束建模及高效求解算法的选择过程,并通过具体仿真案例验证了所提模型与方法在调度效果和鲁棒性方面的优越性。; 适合人群:具备电力系统、综合能源系统或运筹优化等相关背景,熟悉Matlab编程,从事能源系统规划、运行优化等领域科研与工程应用的人员,尤其适合研究生、高校研究人员及能源行业工程师。; 使用场景及目标:①用于港口综合能源系统的规划设计与运行管理决策,提升多能协同效率;②为含多能互补与物流耦合特性的复杂能源系统提供建模思路与求解技术支持;③支撑科研论文复现、学术研究深化及实际工程项目的方案论证与优化。; 阅读建议:建议读者结合Matlab代码与理论内容同步学习,重点理解能量-物流耦合机制的数学表征、多目标优化的处理技巧以及约束条件的精细化建模方法,宜在掌握基本优化理论的基础上开展仿真调试与结果分析。
内容概要:本文系统介绍了名为《【复现】考虑数据中心共享储能与计算负荷时空迁移特性的虚拟电厂优化运行方法(Matlab代码实现)》的技术资源,聚焦于融合数据中心算力负荷调度与电力系统储能协同管理的虚拟电厂优化运行模型。该方法充分考虑了计算负荷在时间和空间上的可迁移特性,结合共享储能机制,构建了提升能源利用效率与系统经济性的综合优化框架,适用于“算力-电力”深度耦合的新型电力系统研究。文中不仅提供了完整的Matlab仿真代码、数学模型及配套论文资料,还强调科研需具备缜密逻辑、善用资源,并倡导在扎实基础上进行创新思考,以实现科研突破。; 适合人群:具备电力系统、能源互联网、优化调度等相关领域基础知识的研究生、科研人员及工程技术人员,特别适合从事虚拟电厂、数据中心能源管理、共享储能、综合能源系统等方向研究的专业人士。; 使用场景及目标:①用于复现和深入理解计及算力负荷时空迁移特性的虚拟电厂优化模型;②支撑高水平科研论文撰写、科研课题攻关或学位论文的仿真验证工作;③掌握利用Matlab进行复杂能源系统建模、优化求解与仿真实践的关键技能。; 阅读建议:建议读者严格按照资料目录顺序系统学习,同步下载并运行网盘中的完整资源(代码、模型、论文),重点关注其优化建模的理论推导与代码实现细节,坚持理论分析与仿真实验相结合,以深刻把握“算力-电力”协同优化的核心机制与技术精髓。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值