如何确定控件是否允许C#透明?

时间:2018-02-21 11:12:49

标签: c# .net vb.net winforms transparency

在Windows Forms技术下,我对 Form 类进行了子类化并覆盖了 OnControlAdded 事件调用器,我的意图是每个控件都添加到控件中我的表单的集合,将其Control.BackColor属性设置为Color.Transparent值,而不管在我的表单中添加控件时的默认颜色继承。

但是,正如您可能知道的,某些控件(例如ListView)不接受透明度,并且在尝试设置Color.Transparent值时,会抛出System.ArgumentException,告知您可以设置透明颜色。

然后,我的问题是:

在C#或VB.NET中,这是正确的方法(可能使用Reflection,或者可能调用Win32函数)在运行时确定控件是否允许透明的背景颜色(Color.Transparent)?例如,而不是处理指定的异常或在switch情况下对控件类型进行分类。

1 个答案:

答案 0 :(得分:4)

要检查控件是否支持URL obj = new URL(url); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); connection.connect(); ObjectMapper mapper = new ObjectMapper(); //building the json request.. tested and works fine String json = mapper.writeValueAsString(requestParameters); DataOutputStream os = new DataOutputStream(connection.getOutputStream()); os.writeBytes(json); os.flush(); os.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); //Program throws exception here for large response String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); connection.disconnect(); 背景颜色,可以通过传递GetStyle标志作为输入来使用控件的ControlStyles.SupportsTransparentBackColor方法。

结果是Transparent值,告诉您控件是否支持样式。

  

如果bool,则控件接受带有alpha分量的true   小于255来模拟透明度。将模拟透明度   仅当UserPaint位设置为true且父控件为   源自Control。

该方法受到保护,因此可以在派生类中访问它。

如果要从控件外部使用它,则需要通过反射调用它:

BackColor