重拾VB6(25):Using Graphics Methods

本文介绍了使用Visual Basic进行基本图形绘制的方法,包括清除图形、设置像素颜色、获取像素颜色、绘制直线、圆圈、矩形等操作。此外,还讨论了各种绘图方法的特点及其适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来自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/Working with Text and Graphics/

1. basics of Graphics Methods

(1)

MethodDescription
ClsClears all graphics and Print output.
PSetSets the color of an individual pixel.
PointReturns the color value of a specified point.
LineDraws a line, rectangle, or filled-in box.
CircleDraws a circle, ellipse, or arc.
PaintPicturePaints graphics at arbitrary locations.

(2) The graphics methods work well in situations where using graphical controls require too much work.

(3) Graphics methods offer some visual effects that are not available in the graphical controls. For example, you can only create arcs or paint individual pixels using the graphics methods.

(4) Graphics you create with these graphics methods appear on the form in a layer of their own. This layer is below all other controls on a form, so using the graphics methods can work well when you want to create graphics that appear behind everything else in your application.

(5) 短处:在设计时不可见(而用图形控件则可在设计时就知道效果)。

(6) Every graphics method draws output on a form, in a picture box, or to the Printer object. To indicate where you want to draw, precede a graphics method with the name of a form or picture box control.

Each drawing area has its own coordinate system that determines what units apply to the coordinates. In addition, every drawing area has its own complete set of graphics properties.

2. Drawing points, lines, boxes, circles, ellipses, pictures

(1) You can precede each of these points with the Step keyword, specifying that the location of the point is relative to the last point drawn.

(2) 用Line可以画box: 直接画4条线或用B参数。还可用F参数及指定填充颜色和FillStyle。

(3) The Circle method draws a variety of circular and elliptical (oval) shapes. In addition, Circle draws arcs (segments of circles) and pie-shaped wedges.

(4) The PaintPicture method can be used in place of the BitBlt Windows API function to perform a wide variety of bit operations while moving a rectangular block of graphics from one position to any other position.

The pic argument must be a Picture object, as from the Picture property of a form or control.

3. DrawWidth, DrawMode

(1) The DrawWidth property specifies the width of the line for output from the graphics methods. The BorderWidth property specifies the outline thickness of line and shape controls.

(2) The DrawMode property determines what happens when you draw one pattern on top of another.

(3)  image (4)

SettingDescription
4Not Copy Pen. Draws the inverse of the line pattern, regardless of what is already there.
7Xor Pen. Displays the difference between the line pattern and the existing display, as explained later in this section. Drawing an object twice with this mode restores the background precisely as it was.
11No operation. In effect, this turns drawing off.
13Copy Pen (default). Applies the line’s pattern, regardless of what is already there.

4. Creating Graphics When a Form Loads

When creating graphics that appear on a form when it loads, consider placing the graphics methods in the Form_Paint event. Form_Paint graphics will get repainted automatically in every paint event. If you place graphics in the Form_Load event, set the AutoRedraw property on the form to True. In this case, Form_Load should show the form, then draw the graphics. Remember, forms are not visible during the Form_Load event. Because Visual Basic does not process graphics methods on a form that is not visible, graphics methods in the Form_Load event are ignored unless AutoRedraw is set to True.

vb.net 如何实现划词功能,禁止使用复制粘贴,目前已有如下方式,但是对于不少自定义特殊控件无法sendmessage,从而无法获取文本,请给出新的方法 Private Function GetSelectedTextUsingWin32() As String ' 通过Win32 API获取当前窗口选中文本的函数 Try ' 步骤1:获取当前前台窗口句柄 Dim hForegroundWindow As IntPtr = NativeMethods.GetForegroundWindow() If hForegroundWindow = IntPtr.Zero Then Return Nothing ' 无活动窗口时返回空 ' 步骤2:获取并记录窗口标题(用于调试) Dim length As Integer = NativeMethods.GetWindowTextLength(hForegroundWindow) If length <= 0 Then Return String.Empty Dim sb1 As New StringBuilder(length + 1) ' +1为null终止符预留空间 NativeMethods.GetWindowText(hForegroundWindow, sb1, sb1.Capacity) Debug.Print("当前窗口标题: " & sb1.ToString()) win_title = sb1.ToString() ' 保存到全局/类级变量供其他地方使用 ' 步骤3:处理线程输入关联 Dim processId As Integer = 0 ' 获取创建窗口的线程ID Dim foregroundThreadId As Integer = NativeMethods.GetWindowThreadProcessId(hForegroundWindow, processId) Dim currentThreadId As Integer = NativeMethods.GetCurrentThreadId() ' 当前线程ID ' 如果当前线程和前台窗口线程不同,附加到输入线程 If foregroundThreadId <> currentThreadId Then ' 附加线程输入以正确获取焦点控件 NativeMethods.AttachThreadInput(currentThreadId, foregroundThreadId, True) End If ' 步骤4:获取当前焦点控件的句柄 Dim hFocusWindow As IntPtr = NativeMethods.GetFocus() If hFocusWindow = IntPtr.Zero Then Debug.Print("未获取到当前控件句柄: ") Return Nothing ' 没有焦点控件时返回空 Else Debug.Print($"当前控件句柄:{hFocusWindow.ToString()} ") End If Dim className As New StringBuilder(256) If NativeMethods.GetClassName(hFocusWindow, className, className.Capacity) > 0 Then Debug.Print($"控件类型: {className.ToString()}") Else Debug.Print("获取控件类型失败") End If ' 步骤5:获取选中文本的起始和结束位置 Dim startPos As Integer = 0, endPos As Integer = 0 ' 发送EM_GETSEL消息获取选择范围(适用于编辑控件) NativeMethods.SendMessage(hFocusWindow, NativeMethods.EM_GETSEL, startPos, endPos) ' 未选择任何文本时返回空 If startPos = endPos Then Return Nothing ' 步骤6:获取控件中的完整文本 ' 发送WM_GETTEXTLENGTH消息获取文本长度 Dim textLength As Integer = NativeMethods.SendMessage(hFocusWindow, NativeMethods.WM_GETTEXTLENGTH, 0, 0) If textLength <= 0 Then Return Nothing ' 创建足够大的缓冲区接收文本(+1用于null终止符) Dim sb As New StringBuilder(textLength + 1) ' 发送WM_GETTEXT消息获取实际文本内容 NativeMethods.SendMessage(hFocusWindow, NativeMethods.WM_GETTEXT, sb.Capacity, sb) ' 步骤7:处理边界情况并返回选中部分 ' 防止结束位置超过实际文本长度 If endPos > sb.Length Then endPos = sb.Length Return sb.ToString().Substring(startPos, endPos - startPos) Catch ex As Exception ' 异常处理:记录错误信息到调试输出 Debug.WriteLine("Win32 方式获取选中文本失败: " & ex.Message) Return Nothing Finally ' (注意:如果需要,这里应添加清理代码,如DetachThreadInput) End Try End Function
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值