在窗体上画一矩形,当我按一下button1,这个矩形往右移动10点,怎么实现

cainiaobus 2003-09-24 08:10:35
在窗体上画一矩形,当我按一下button1,这个矩形往右移动10点,
当我把这个窗体最小化,再还原,那矩形不能消失掉

请问怎么实现
...全文
129 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjcccc 2003-09-24
  • 打赏
  • 举报
回复
用那个控件无所谓啊,方法在上面我的第二例中,不过是用c#重写的,但这也容易换成VB的。
cainiaobus 2003-09-24
  • 打赏
  • 举报
回复
测试我要的功能的方法:
建一form1,在form1上画一个msFlexGrid--->msGrid0,两个button-->Button1、Button2
窗体载入后
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim g As Graphics
Dim p As New Pen(Color.Blue, 1)
Dim rect As New Rectangle(10, 10, 40, 40)
g = MsGrid0.CreateGraphics() 'msGrid0画矩形,不然会跑到msGrid0的背后去,看不到
MsGrid0.set_TextMatrix(1, 1, "测试Grid控件") '显示两个字,看一下矩形会不会所这些字盖住
g.DrawRectangle(p, rect)
g.Dispose()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  '想办法把刚button1按下时产生的矩表往右移动10点
End Sub
boyors 2003-09-24
  • 打赏
  • 举报
回复
你不会用imagebox这个控件么?用它代替picturebox控件,就可实现你的要求了。
cainiaobus 2003-09-24
  • 打赏
  • 举报
回复
TO:jjcccc()
 你给的代码我正在试

在这里,我不能用picturebox,因为我想要的是,在某个控件上画一矩形,
这个矩形得在这个控件上面(用form1.createGraphics所画出来的图表会显示在控件下面)
这个矩形是空心的,不能盖住控件上面的文字(如果用pictureBox,则会盖住控件上的文字,也许可以设计picturebox为透明,但好象没有这种功能)


jjcccc 2003-09-24
  • 打赏
  • 举报
回复
再给个Button的Click的关键代码:
private void button1_Click(object sender, System.EventArgs e)
{
Pen red=new Pen(Color.Red);
Graphics g=pictureBox1.CreateGraphics();//pictureBox1为窗体上的一个PictureBox控件
g.Clear(this.BackColor);//清除原图
this.picLeft+=10;//picLeft is Form's member,initial value = 0;
g.DrawRectangle(red,picLeft,10,100,50);
g.Dispose();
}
jjcccc 2003-09-24
  • 打赏
  • 举报
回复
要是只要移动,不想让整个窗体重绘的画,用个PictrueBox控件,在这上面画矩形,移动的画就移动PictrueBox,更容易多了。
jjcccc 2003-09-24
  • 打赏
  • 举报
回复
这是一个测试代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;

private int left=10;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(416, 272);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 309);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.Red),left,10,300,200);
}

private void button1_Click(object sender, System.EventArgs e)
{
this.left+=10;
this.Refresh();
}
}
}
cainiaobus 2003-09-24
  • 打赏
  • 举报
回复
TO:hikele(幽夜朝阳)

没有Location属性,你可以这样试试:
Dim G As Graphics = frm.CreateGraphics()
Dim p As New pen(Color.Black, 2)
Dim rect As rectangle = new rectangle(10,10,40,30)
g.drawretangle(p,rect)


TO:jjcccc()
怎么擦掉原图

TO:minajo21(大眼儿:人吃五谷谁无屎)
我要的是移动矩形,这是重点,最好不要用重画功能


 


eileendl 2003-09-24
  • 打赏
  • 举报
回复
form 的activated事件中重画矩形。
jjcccc 2003-09-24
  • 打赏
  • 举报
回复
当你按一下button1,擦掉原图,重画新图(新图右移10点)
hikele 2003-09-24
  • 打赏
  • 举报
回复
定义矩形的Location试试

ctl.Location = New System.Drawing.Point(ctl.Location.X-10, ctl.Location.Y)
minajo21 2003-09-24
  • 打赏
  • 举报
回复


请参考画圆的:
Public Sub DrawCircle(ByVal frm As Windows.Forms.Form, ByVal Rect As Rectangle, ByVal fColor As Color)
Dim a As Graphics = frm.CreateGraphics()
Dim pen As New pen(System.Drawing.Color.Black, 2)
Dim sBrush As SolidBrush = New SolidBrush(fColor)
a.DrawEllipse(pen, Rect)
a.FillEllipse(sBrush, Rect)

pen = Nothing
sBrush = Nothing
End Sub
Brunhild 2003-09-24
  • 打赏
  • 举报
回复
看看这段代码行不行:
Public Class CRectangle
Dim m_r As Rectangle
Dim m_Container As Control
Dim m_Visible As Boolean = True

Public Sub New(ByVal Container As Control, ByVal r As Rectangle)
m_Container = Container
AddHandler m_Container.Paint, AddressOf Me.PaintEvent
Me.Rect = r
End Sub

Public Property Rect() As Rectangle
Get
Return m_r
End Get
Set(ByVal Value As Rectangle)
If m_r.IsEmpty() = False Then
Me.Clear()
End If
m_r = Value
Me.Invalidate()
End Set
End Property

Public Property Visible() As Boolean
Get
Return m_Visible
End Get
Set(ByVal Value As Boolean)
m_Visible = Value
Me.Invalidate()
End Set
End Property

Private Sub Clear()
Dim b As Boolean = m_Visible
Me.m_Visible = False
Me.Invalidate()
Me.m_Visible = b
End Sub

Private Sub Invalidate()
'*** 很奇怪,失效区域必须比实际图形区域大1才能全部刷新
Me.m_Container.Invalidate(New Region(New Rectangle(m_r.X, m_r.Y, m_r.Width + 1, m_r.Height + 1)))
End Sub

Private Sub PaintEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
If m_Visible Then
If e.ClipRectangle.IntersectsWith(m_r) Then
e.Graphics.DrawRectangle(System.Drawing.Pens.Black, m_r)
End If
End If
End Sub
End Class

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧