我正在尝试将整数变量设置为enum
值的组合,enum
为:
public enum WebBrowserDownloadControlFlags : uint
{
DLIMAGES = 0x00000010,
VIDEOS = 0x00000020,
BGSOUNDS = 0x00000040,
NO_SCRIPTS = 0x00000080,
NO_JAVA = 0x00000100,
NO_RUNACTIVEXCTLS = 0x00000200,
NO_DLACTIVEXCTLS = 0x00000400,
DOWNLOADONLY = 0x00000800,
NO_FRAMEDOWNLOAD = 0x00001000,
RESYNCHRONIZE = 0x00002000,
PRAGMA_NO_CACHE = 0x00004000,
NO_BEHAVIORS = 0x00008000,
NO_METACHARSET = 0x00010000,
URL_ENCODING_DISABLE_UTF8 = 0x00020000,
URL_ENCODING_ENABLE_UTF8 = 0x00040000,
NOFRAMES = 0x00080000,
FORCEOFFLINE = 0x10000000,
NO_CLIENTPULL = 0x20000000,
SILENT = 0x40000000,
OFFLINEIFNOTCONNECTED = 0x80000000,
OFFLINE = OFFLINEIFNOTCONNECTED,
}
DownloadControlFlags
是一个整数字段:
public int DownloadControlFlags
{
get
{
return _downloadControlFlags;
}
set
{
if (_downloadControlFlags == value)
return;
_downloadControlFlags = value;
IOleControl ctl = (IOleControl)ActiveXInstance;
ctl.OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL);
}
}
要设置此变量,我使用summation:
webBrowser1.DownloadControlFlags = (int)WebBrowserDownloadControlFlags.DLIMAGES
+ (int)WebBrowserDownloadControlFlags.NOFRAMES
+ (int)WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD
+ (int)WebBrowserDownloadControlFlags.NO_JAVA
+ (int)WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS
+ (int)WebBrowserDownloadControlFlags.NO_BEHAVIORS
+ (int)WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS
+ (int)WebBrowserDownloadControlFlags.SILENT;
我不确定这是否是正确的方法,但它似乎有效。但现在我想检查变量是否包含特定值。对于正常enum
,解决方案可以是此问题的答案How to check if any flags of a flag combination are set?,但我不确定在我的情况下是否可以使用相同的方式。
如何检查是否已设置特定标志?!例如,DownloadControlFlags
是否包含WebBrowserDownloadControlFlags.NO_JAVA
!
答案 0 :(得分:3)
对于正常的
enum
,解决方案可能是此问题How to check if any flags of a flag combination are set?的答案,但我不确定在我的情况下是否可以使用相同的方式。
你试过吗?
int flags = (int)WebBrowserDownloadControlFlags.DLIMAGES
+ (int)WebBrowserDownloadControlFlags.NOFRAMES
+ (int)WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD
+ (int)WebBrowserDownloadControlFlags.NO_JAVA
+ (int)WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS
+ (int)WebBrowserDownloadControlFlags.NO_BEHAVIORS
+ (int)WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS
+ (int)WebBrowserDownloadControlFlags.SILENT;
您可以使用Enum.HasFlag
将其转换回枚举类型:
Console.WriteLine(((WebBrowserDownloadControlFlags)flags).HasFlag(WebBrowserDownloadControlFlags.NOFRAMES)); // true
Console.WriteLine(((WebBrowserDownloadControlFlags)flags).HasFlag(WebBrowserDownloadControlFlags.VIDEOS)); // false
或者您可以将要比较的枚举标记转换为int,然后进行逐位比较:
Console.WriteLine((flags & (int)WebBrowserDownloadControlFlags.NO_JAVA) != 0); // true
Console.WriteLine((flags & (int)WebBrowserDownloadControlFlags.VIDEOS) != 0); // false
但是你应该考虑一下你是否真的不希望将内部标志存储为WebBrowserDownloadControlFlags
类型。没有理由尽早将其转换为int。你也可以这样做:
WebBrowserDownloadControlFlags flags = WebBrowserDownloadControlFlags.DLIMAGES
| WebBrowserDownloadControlFlags.NOFRAMES
| WebBrowserDownloadControlFlags.NO_FRAMEDOWNLOAD
| WebBrowserDownloadControlFlags.NO_JAVA
| WebBrowserDownloadControlFlags.NO_DLACTIVEXCTLS
| WebBrowserDownloadControlFlags.NO_BEHAVIORS
| WebBrowserDownloadControlFlags.NO_RUNACTIVEXCTLS
| WebBrowserDownloadControlFlags.SILENT;
Console.WriteLine(flags.HasFlag(WebBrowserDownloadControlFlags.NOFRAMES)); // true
Console.WriteLine(flags.HasFlag(WebBrowserDownloadControlFlags.VIDEOS)); // false
Console.WriteLine((flags & WebBrowserDownloadControlFlags.NO_JAVA) != 0); // true
Console.WriteLine((flags & WebBrowserDownloadControlFlags.VIDEOS) != 0); // false
每当你真的需要一个int值时,你可以将整个事物转换成一个int:
Console.WriteLine((int)flags); // 1074304784