C# 如何使用枚举?
我在新类中创建的枚举该类位于dll(库项目)中我在该解决方案中有两个项目第一个dll(库)和第二个windows窗体: 我创建并要使用的枚举是以下命令:C# 如何使用枚举?,c#,.net,winforms,enums,C#,.net,Winforms,Enums,我在新类中创建的枚举该类位于dll(库项目)中我在该解决方案中有两个项目第一个dll(库)和第二个windows窗体: 我创建并要使用的枚举是以下命令: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Threading; namespace Capture.Interface { public
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
namespace Capture.Interface
{
public enum DannysCommands
{
Displayoverlays,
Dontdisplayoverlays
}
public enum Direct3DVersion
{
Unknown,
AutoDetect,
Direct3D9,
Direct3D10,
Direct3D10_1,
Direct3D11,
Direct3D11_1,
}
[Serializable]
public delegate void RecordingStartedEvent(CaptureConfig config);
[Serializable]
public delegate void RecordingStoppedEvent();
[Serializable]
public delegate void MessageReceivedEvent(MessageReceivedEventArgs message);
[Serializable]
public delegate void ScreenshotReceivedEvent(ScreenshotReceivedEventArgs response);
[Serializable]
public delegate void DisconnectedEvent();
[Serializable]
public delegate void ScreenshotRequestedEvent(ScreenshotRequest request);
[Serializable]
public delegate void DisplayTextEvent(DisplayTextEventArgs args);
public enum MessageType
{
Debug,
Information,
Warning,
Error
}
[Serializable]
public class CaptureInterface : MarshalByRefObject
{
在同一个项目中,有两条线可以绘制,我想在显示覆盖图绘制和不显示覆盖图不绘制时绘制
我还可以从project windows窗体的form1调用该类。
在form1中,我想使用一个按钮单击,并使用bool变量进行检查,以便在显示覆盖图时绘制线条,在不显示覆盖图时不绘制线条
在dll(库)项目中,还有另一个类具有绘图线:
if (Capture.Interface.DannysCommands.Displayoverlays)
{
}
_spriteEngine.DrawString(textElement.Location.X + 1800, textElement.Location.Y, textElement.Text, textElement.Color.R, textElement.Color.G, textElement.Color.B, textElement.Color.A, font);
_spriteEngine.DrawString(textElement.Location.X + 1800, textElement.Location.Y + 25,
DateTime.Now.ToString("h:mm tt"), textElement.Color.R, textElement.Color.G, textElement.Color.B, textElement.Color.A, font);
我知道enum不是bool,所以如果我这样做了,就会出现错误
我如何在类中使用带有form1的枚举和带有绘制线的枚举
在form1中,我想在我单击按钮一次绘制绘制绘制线以绘制第二次单击按钮时,不使用枚举进行绘制。实际上不需要使用枚举。据我所知,你只需要画一次线。假设您有一个表单:
public partial class FrmMain : Form
{
private bool isClicked;
private void FrmMain_Load(object sender, EventArgs e)
{
isClicked = false;
}
private void Button1_Click(object sender, EventArgs e)
{
if (isClicked) return;
isClicked = true;
//...draw lines here...
}
}
Enum适用于您有选项并且希望为程序员提供一组标准的选项,以限制输入,当然也限制不需要的输入的错误
Enum您可以将其与
开关
语句一起使用。您需要设置类型为的变量。您可以使用该变量并在if语句中检查它的值,就像您通常所做的那样。
因此:
DannysCommands cmd=DannysCommands.InitialValue
在某些时候,您将执行cmd=DannysCommands.DisplayOverlays代码>
然后:
if(cmd == DannysCommands.DisplayOverlays)
{
...
}
注意:您正在使用枚举跟踪状态。虽然还有其他使用枚举的方法,但我相信这是最自然的方法。您实际上是在用bool跟踪状态。这对于OPs的需求来说可能太简单了(如果没有看到完整的代码,理解无处不在的语言,我们就不知道了)。在很多情况下,使用枚举可以极大地提高代码的复杂性和可读性。我也不认为您可以使用枚举跟踪状态。在任何情况下,我都无法看到完整的代码,因此无法提出更好的解决方案。