請教: 繼承問題,請看程式碼!

简叔 2004-01-30 09:15:32

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

class DrawHouse : PrintableForm
{
public new static void Main()
{
Application.Run(new DrawHouse());
}

public DrawHouse()
{
Text = "Draw A House";
}

protected override void DoPage(Graphics grfx,Color color, int cy, int cy)
{
grfx.DrawLines(new Pen(color),
new Point[]{
new Point(cx / 4, 3* cy/4),// Lower left
new Point(cx / 4, cy / 2),
new Point(cx / 2, cy / 2),// Peak
new Point(cx / 4, cy / 2),
new Point(cx/4, 3*cy/4 ),// Lower left
new Point(3*cx/4, 3*cy/4), // Lower right
});
}
}


class PrintableForm : Form
{
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
Text = "Printable Form";
ResizeRedraw = true;
}

protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}

protected override void OnClick(EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}

void PrintDocumentOnPrintPage(object sender,PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
SizeF sizef = grfx.VisibleClipBounds.Size;
DoPage(grfx, Color.Black,(int)sizef.Width, (int)sizef.Height);
}

protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);

grfx.DrawLine(pen,0,0,cx-1,cy-1);
grfx.DrawLine(pen,cx-1,0,0,cy-1);
}
}


---------- C# Compiler ----------
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

DrawHouse.cs(39,24): error CS0017: 程式 'c:\CS\Study C#\DrawHouse.exe' 有一個以上的已定義進入點: 'PrintableForm.Main()'
DrawHouse.cs(12,28): error CS0017: 程式 'c:\CS\Study C#\DrawHouse.exe' 有一個以上的已定義進入點: 'DrawHouse.Main()'
Output completed (0 sec consumed) - Normal Termination
...全文
39 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
简叔 2004-01-30
  • 打赏
  • 举报
回复
謝謝 haowork1977.
haowork1977 2004-01-30
  • 打赏
  • 举报
回复
你犯的错误是把入口点也继承了,你如果真要继承,应该把这些入口全去掉然后单独使用一个另外的入口来调用,因为入口点对继承是没有任何好处的。
public new static void Main()
{
Application.Run(new DrawHouse());
}
haowork1977 2004-01-30
  • 打赏
  • 举报
回复
你的代码好多错误哦
我整理了一下,可以使用,结果是画了一个难看无比的框框,文件名为PrintableForm.cs:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class DrawHouse : PrintableForm
{
public new static void Main()
{
Application.Run(new DrawHouse());
}

public DrawHouse()
{
Text = "Draw A House";
}

protected override void DoPage(Graphics grfx,Color color, int cx, int cy)
{
grfx.DrawLines(new Pen(color),
new Point[]{
new Point(cx / 4, 3* cy/4),// Lower left
new Point(cx / 4, cy / 2),
new Point(cx / 2, cy / 2),// Peak
new Point(cx / 4, cy / 2),
new Point(cx/4, 3*cy/4 ),// Lower left
new Point(3*cx/4, 3*cy/4), // Lower right
});
}
}


class PrintableForm : Form
{

public PrintableForm()
{
Text = "Printable Form";
ResizeRedraw = true;
}

protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}

protected override void OnClick(EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}

void PrintDocumentOnPrintPage(object sender,PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
SizeF sizef = grfx.VisibleClipBounds.Size;
DoPage(grfx, Color.Black,(int)sizef.Width, (int)sizef.Height);
}

protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);

grfx.DrawLine(pen,0,0,cx-1,cy-1);
grfx.DrawLine(pen,cx-1,0,0,cy-1);
}
}
简叔 2004-01-30
  • 打赏
  • 举报
回复
我記得是在 Vs.net 的專案中,可以有兩個以上的 Main , 但是您可以指定設定某一個專案為起始專案就可以了,如我上面所列的指令類似. 為什麼在 Command Line Mode 下就不行了呢?

那請問: 怎樣正確的繼承一個 form ?
简叔 2004-01-30
  • 打赏
  • 举报
回复
Error Message 中已經講的得很清楚了:" 有一個以上的已定義進入點: 'DrawHouse.Main()'"

問題是現在怎樣來解決這個問題.

我把程序分成兩個 file 用如下指令來編譯也是出現同樣的問題

csc PrintableForm.cs DrawHouse.cs /m: DrawHouse.cs
bankliu 2004-01-30
  • 打赏
  • 举报
回复
你入口点多了
八爪鱼-杭州 2004-01-30
  • 打赏
  • 举报
回复
一个程序只能有一个main,也就是说只能有一个入口
简叔 2004-01-30
  • 打赏
  • 举报
回复
可是我在 subclass 中的 Main 方法前用了
public new static void Main()

為何還是不行呢?
八爪鱼-杭州 2004-01-30
  • 打赏
  • 举报
回复
如果你是要显示窗体的话,可以这样
Form f=new Form();
f.show();
八爪鱼-杭州 2004-01-30
  • 打赏
  • 举报
回复
Application.Run()只能有一个

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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