subject_16274.htm
来自「一些关于vc的问答」· HTM 代码 · 共 48 行 · 第 1/3 页
HTM
48 行
<p>
序号:16274 发表者:bank 发表日期:2002-09-26 20:38:07
<br>主题:求助,雷神,谢谢了
<br>内容:using System;<BR>using System.Drawing;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Windows.Forms;<BR>using System.Data;<BR><BR><BR>namespace WindowsApplication1<BR>{<BR> /// <summary><BR> /// Form1 的摘要说明。<BR> /// </summary><BR> public class Form1 : System.Windows.Forms.Form<BR> {<BR> private System.Windows.Forms.MainMenu mainMenu1;<BR> private System.Windows.Forms.MenuItem menuItem1;<BR> private System.Windows.Forms.MenuItem menuItem2;<BR> private System.Windows.Forms.MenuItem menuItem3;<BR> private System.Windows.Forms.MenuItem menuItem4;<BR> private System.Windows.Forms.MenuItem menuItem5;<BR> private System.Windows.Forms.MenuItem menuItem6;<BR> private System.Windows.Forms.PictureBox picBox1;<BR> private System.Windows.Forms.Timer timer1;<BR> private System.Windows.Forms.PictureBox pictureBox1;<BR> private System.ComponentModel.IContainer components;<BR> //开始添加类声明<BR> <BR> public Form1()<BR> {<BR> //<BR> // Windows 窗体设计器支持所必需的<BR> //<BR> InitializeComponent();<BR> <BR> //<BR> // TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR> //<BR> }<BR><BR> /// <summary><BR> /// 清理所有正在使用的资源。<BR> /// </summary><BR> protected override void Dispose( bool disposing )<BR> {<BR> if( disposing )<BR> {<BR> if (components != null) <BR> {<BR> components.Dispose();<BR> }<BR> }<BR> base.Dispose( disposing );<BR> }<BR><BR> #region Windows Form Designer generated code<BR> /// <summary><BR> /// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR> /// 此方法的内容。<BR> /// </summary><BR> private void InitializeComponent()<BR> {<BR> this.components = new System.ComponentModel.Container();<BR> System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));<BR> this.picBox1 = new System.Windows.Forms.PictureBox();<BR> this.mainMenu1 = new System.Windows.Forms.MainMenu();<BR> this.menuItem1 = new System.Windows.Forms.MenuItem();<BR> this.menuItem2 = new System.Windows.Forms.MenuItem();<BR> this.menuItem3 = new System.Windows.Forms.MenuItem();<BR> this.menuItem4 = new System.Windows.Forms.MenuItem();<BR> this.menuItem5 = new System.Windows.Forms.MenuItem();<BR> this.menuItem6 = new System.Windows.Forms.MenuItem();<BR> this.timer1 = new System.Windows.Forms.Timer(this.components);<BR> this.pictureBox1 = new System.Windows.Forms.PictureBox();<BR> this.SuspendLayout();<BR> // <BR> // picBox1<BR> // <BR> this.picBox1.AccessibleDescription = ((string)(resources.GetObject("picBox1.AccessibleDescription")));<BR> this.picBox1.AccessibleName = ((string)(resources.GetObject("picBox1.AccessibleName")));<BR> this.picBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("picBox1.Anchor")));<BR> this.picBox1.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("picBox1.BackgroundImage")));<BR> this.picBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;<BR> this.picBox1.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("picBox1.Dock")));<BR> this.picBox1.Enabled = ((bool)(resources.GetObject("picBox1.Enabled")));<BR> this.picBox1.Font = ((System.Drawing.Font)(resources.GetObject("picBox1.Font")));<BR> this.picBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("picBox1.Image")));<BR> this.picBox1.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("picBox1.ImeMode")));<BR> this.picBox1.Location = ((System.Drawing.Point)(resources.GetObject("picBox1.Location")));<BR> this.picBox1.Name = "picBox1";<BR> this.picBox1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("picBox1.RightToLeft")));<BR> this.picBox1.Size = ((System.Drawing.Size)(resources.GetObject("picBox1.Size")));<BR> this.picBox1.SizeMode = ((System.Windows.Forms.PictureBoxSizeMode)(resources.GetObject("picBox1.SizeMode")));<BR> this.picBox1.TabIndex = ((int)(resources.GetObject("picBox1.TabIndex")));<BR> this.picBox1.TabStop = false;<BR> this.picBox1.Text = resources.GetString("picBox1.Text");<BR> this.picBox1.Visible = ((bool)(resources.GetObject("picBox1.Visible")));<BR> // <BR> // mainMenu1<BR> // <BR> this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<BR> this.menuItem1,<BR> this.menuItem5});<BR> this.mainMenu1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("mainMenu1.RightToLeft")));<BR> // <BR> // menuItem1<BR> // <BR> this.menuItem1.Enabled = ((bool)(resources.GetObject("menuItem1.Enabled")));<BR> this.menuItem1.Index = 0;<BR> this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<BR> this.menuItem2,<BR> this.menuItem3,<BR> this.menuItem4});<BR> this.menuItem1.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem1.Shortcut")));<BR> this.menuItem1.ShowShortcut = ((bool)(resources.GetObject("menuItem1.ShowShortcut")));<BR> this.menuItem1.Text = resources.GetString("menuItem1.Text");<BR> this.menuItem1.Visible = ((bool)(resources.GetObject("menuItem1.Visible")));<BR> // <BR> // menuItem2<BR> // <BR> this.menuItem2.Enabled = ((bool)(resources.GetObject("menuItem2.Enabled")));<BR> this.menuItem2.Index = 0;<BR> this.menuItem2.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem2.Shortcut")));<BR> this.menuItem2.ShowShortcut = ((bool)(resources.GetObject("menuItem2.ShowShortcut")));<BR> this.menuItem2.Text = resources.GetString("menuItem2.Text");<BR> this.menuItem2.Visible = ((bool)(resources.GetObject("menuItem2.Visible")));<BR> this.menuItem2.Click += new System.EventHandler(this.start);<BR> // <BR> // menuItem3<BR> // <BR> this.menuItem3.Enabled = ((bool)(resources.GetObject("menuItem3.Enabled")));<BR> this.menuItem3.Index = 1;<BR> this.menuItem3.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem3.Shortcut")));<BR> this.menuItem3.ShowShortcut = ((bool)(resources.GetObject("menuItem3.ShowShortcut")));<BR> this.menuItem3.Text = resources.GetString("menuItem3.Text");<BR> this.menuItem3.Visible = ((bool)(resources.GetObject("menuItem3.Visible")));<BR> // <BR> // menuItem4<BR> // <BR> this.menuItem4.Enabled = ((bool)(resources.GetObject("menuItem4.Enabled")));<BR> this.menuItem4.Index = 2;<BR> this.menuItem4.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem4.Shortcut")));<BR> this.menuItem4.ShowShortcut = ((bool)(resources.GetObject("menuItem4.ShowShortcut")));<BR> this.menuItem4.Text = resources.GetString("menuItem4.Text");<BR> this.menuItem4.Visible = ((bool)(resources.GetObject("menuItem4.Visible")));<BR> this.menuItem4.Click += new System.EventHandler(this.exit);<BR> // <BR> // menuItem5<BR> // <BR> this.menuItem5.Enabled = ((bool)(resources.GetObject("menuItem5.Enabled")));<BR> this.menuItem5.Index = 1;<BR> this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<BR> this.menuItem6});<BR> this.menuItem5.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem5.Shortcut")));<BR> this.menuItem5.ShowShortcut = ((bool)(resources.GetObject("menuItem5.ShowShortcut")));<BR> this.menuItem5.Text = resources.GetString("menuItem5.Text");<BR> this.menuItem5.Visible = ((bool)(resources.GetObject("menuItem5.Visible")));<BR> // <BR> // menuItem6<BR> // <BR> this.menuItem6.Enabled = ((bool)(resources.GetObject("menuItem6.Enabled")));<BR> this.menuItem6.Index = 0;<BR> this.menuItem6.Shortcut = ((System.Windows.Forms.Shortcut)(resources.GetObject("menuItem6.Shortcut")));<BR> this.menuItem6.ShowShortcut = ((bool)(resources.GetObject("menuItem6.ShowShortcut")));<BR> this.menuItem6.Text = resources.GetString("menuItem6.Text");<BR> this.menuItem6.Visible = ((bool)(resources.GetObject("menuItem6.Visible")));<BR> this.menuItem6.Click += new System.EventHandler(this.设计者);<BR> // <BR> // pictureBox1<BR> // <BR> this.pictureBox1.AccessibleDescription = ((string)(resources.GetObject("pictureBox1.AccessibleDescription")));<BR> this.pictureBox1.AccessibleName = ((string)(resources.GetObject("pictureBox1.AccessibleName")));<BR> this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("pictureBox1.Anchor")));<BR> this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));<BR> this.pictureBox1.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("pictureBox1.Dock")));<BR> this.pictureBox1.Enabled = ((bool)(resources.GetObject("pictureBox1.Enabled")));<BR> this.pictureBox1.Font = ((System.Drawing.Font)(resources.GetObject("pictureBox1.Font")));<BR> this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));<BR> this.pictureBox1.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("pictureBox1.ImeMode")));<BR> this.pictureBox1.Location = ((System.Drawing.Point)(resources.GetObject("pictureBox1.Location")));<BR> this.pictureBox1.Name = "pictureBox1";<BR> this.pictureBox1.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("pictureBox1.RightToLeft")));<BR> this.pictureBox1.Size = ((System.Drawing.Size)(resources.GetObject("pictureBox1.Size")));<BR> this.pictureBox1.SizeMode = ((System.Windows.Forms.PictureBoxSizeMode)(resources.GetObject("pictureBox1.SizeMode")));<BR> this.pictureBox1.TabIndex = ((int)(resources.GetObject("pictureBox1.TabIndex")));<BR> this.pictureBox1.TabStop = false;<BR> this.pictureBox1.Text = resources.GetString("pictureBox1.Text");<BR> this.pictureBox1.Visible = ((bool)(resources.GetObject("pictureBox1.Visible")));<BR> // <BR> // Form1<BR> // <BR> this.AccessibleDescription = ((string)(resources.GetObject("$this.AccessibleDescription")));<BR> this.AccessibleName = ((string)(resources.GetObject("$this.AccessibleName")));<BR> this.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("$this.Anchor")));<BR> this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize")));<BR> this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll")));<BR> this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin")));<BR> this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize")));<BR> this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));<BR> this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));<BR> this.Controls.AddRange(new System.Windows.Forms.Control[] {<BR> this.pictureBox1,<BR> this.picBox1});<BR> this.Cursor = System.Windows.Forms.Cursors.Hand;<BR> this.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("$this.Dock")));<BR> this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));<BR> this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));<BR> this.ForeColor = System.Drawing.SystemColors.ActiveCaption;<BR> this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;<BR> this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));<BR> this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode")));<BR> this.KeyPreview = true;<BR> this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));<BR> this.MaximizeBox = false;<BR> this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize")));<BR> this.Menu = this.mainMenu1;<BR> this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize")));<BR> this.Name = "Form1";<BR> this.Opacity = 0.800000011920929;<BR> this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft")));<BR> this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition")));<BR> this.Text = resources.GetString("$this.Text");<BR> this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0)));<BR> this.Visible = ((bool)(resources.GetObject("$this.Visible")));<BR> this.ResumeLayout(false);<BR><BR> }<BR> #endregion<BR><BR> /// <summary><BR> /// 应用程序的主入口点。<BR> /// </summary><BR> [STAThread]<BR> static void Main() <BR> {<BR> Application.Run(new Form1());<BR> }<BR> // 游戏中要使用类的声明<BR> <BR> <BR><BR><BR> private void exit(object sender, System.EventArgs e)<BR> {<BR> this.Close();<BR> }<BR> public void 设计者(object sender, System.EventArgs e)<BR> {<BR> <BR> Form2 newMDIChild = new Form2();<BR> // Set the Parent Form of the Child window.<BR> //newMDIChild.MDIParent = this;<BR> // Display the new form.<BR> newMDIChild.Show();<BR> }<BR> <BR> public void start(object sender, System.EventArgs e)<BR> {<BR> <BR><BR> // Create image.<BR><BR> Image newImage = Image.FromFile("one.jpg");<BR><BR> // Create Point for upper-left corner of image.<BR><BR> Point ulCorner = new Point( 100, 100);<BR><BR> // Draw image to screen.<BR><BR> Graphics.DrawImage(newImage, ulCorner);<BR> <BR> }<BR> <BR><BR><BR><BR><BR><BR><BR> }<BR>}<BR><BR> <BR><BR>我想在程序里添加一个可以控制移动的方块,可是怎么才能画呢?最后的public void start(object sender, System.EventArgs e)开始就是画图,怎么能画出来呢?我参考了msdn,可是写道这里不会了,说和静态什么的有关
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:雷神 回复日期:2002-09-27 14:33:16
<br>内容:你需要一些FORM1类的额外字段,例如BRUSH和PEN,以及绘图的位置坐标等等。<BR>一般将BRUSH声明为静态,因为C#不允许一个实例来初始化另一个实例,所以你只能用声明成静态来解决。<BR><BR>你可以直接参考MSDN的GDI+。<BR><BR>看你的代码,好象是要显示一个one.jpg图片在FORM里。你可以这样做<BR>在FORM类声明<BR>public class Form1 : System.Windows.Forms.Form<BR> {<BR> private System.ComponentModel.Container components = null;<BR> Image imgPic; //这里添加<BR><BR> public Form1()<BR> {<BR> InitializeComponent();<BR> }<BR><BR> private void InitializeComponent()<BR> {<BR> this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR> this.ClientSize = new System.Drawing.Size(292, 273);<BR> this.Name = "Form1";<BR> this.Text = "图片移动演示";<BR> this.Load += new System.EventHandler(this.Form1_Load);<BR> imgPic=Image.FromFile(@"c:\ONE.jpg");//图片的路径 this.AutoScrollMinSize=imgPic.Size;<BR><BR><BR> }<BR> //重写OnPaint()<BR> protected override void OnPaint(PaintEventArgs e)<BR> {<BR> Graphics dc=e.Graphics;<BR> dc.DrawImageUnscaled(imgPic,this.AutoScrollPosition);<BR> base.OnPaint(e);<BR> }<BR> }<BR>}<BR><BR>这样做就可以了,如果想让它动,你还需要重写FORM类的鼠标或键盘方法。<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:bank 回复日期:2002-09-27 16:47:46
<br>内容:十分的感谢,多亏有了你<BR>在下还有些不明白的地方:<BR>InitializeComponent();<BR>private System.ComponentModel.Container components = null;<BR>InitializeComponent(),components = null是什么意思,他们两行之间有什么关系么?<BR><BR>
<br>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?