WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System_Window_Forms

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Color Structure Issue

 Keywords: System.Drawing.Color Color Structure BackColor ForeColor C# GenerateInMemory

This is an oddity in the FCL's implementation of the structures and there is really not much that can be done about it. System.Drawing.Color has a lot of static methods that are quite handy but they return an VT_I4 variant when returning a color. This wouldn't be a problem if all the properties that expect that structure also accepted VT_I4s as an appropriate type. But many of them don't. They are expecting some kind of VT_RECORD instead. This wouldn't be a problem either if the FCL provided an IRECORD interface for the FCL structure so WinBatch could automagically convert the type but it doen't do that either! It is a catch 22 and ostensibly a inconsistency in the implementation of the FCL.

Here is a workaround that sets Color using C# GenerateInMemory:

;***************************************************************************
;**   Use a Windows .Net Form with a ForeColor and BackColor
;**
;** Purpose: Set the ForeColor and BackColor of a Windows Form
;** Inputs:
;** Outputs: Results written to screen as a form
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk / TD 2014.04.16
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf
 _True = ObjectType( 'BOOL', -1 )
 _False = ObjectType( 'BOOL', 0 )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrOption - Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;ObjectClrOption('use','System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
ObjectClrOption( 'use', 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' )
ObjectClrOption( 'use', 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrNew - Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1 = ObjectClrNew('System.Windows.Forms.Form')
Button1 =  ObjectClrNew('System.Windows.Forms.Button')

; Enumerations
enumStartPos = ObjectClrNew('System.Windows.Forms.FormStartPosition')
enumDialogResult = ObjectClrNew( 'System.Windows.Forms.DialogResult')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Form1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the caption bar text of the form.
Form1.Text = 'BackColor'
; Set the start position of the form to the center of the screen.
Form1.StartPosition = ObjectClrType( 'System.Windows.Forms.FormStartPosition', enumStartPos.CenterScreen ) ;CenterScreen
; Set the accept button of the form to button1.
button1.Location = ObjectClrNew('System.Drawing.Point',100,100)
Form1.AcceptButton = button1
; Obtain the Width of the form for later use with controls
Form1.Size =   ObjectClrNew( 'System.Drawing.Size',900,550)
formwidth = Form1.Width

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set Colors
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GoSub WbSetColor
objWbSetColor = ObjectClrNew('WinBatch.WbSetColor')

;Set Foreground color to WHITE
a=255
r=255
g=255
b=255
objWbSetColor.SetForeColor(Form1,a,r,g,b)

;Set Background color to RED
a=255
r=255
g=0
b=0
objWbSetColor.SetBackColor(Form1,a,r,g,b)

;Set Button Background color to BLUE
a=255
r=0
g=0
b=255
objWbSetColor.SetBackColor(Button1,a,r,g,b)


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Button1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the position of the button on the form.
button1.Location = ObjectClrNew('System.Drawing.Point',100,110)
; Set the text of button1 to 'OK'.
button1.Text = 'OK'
; Specify tabindex
button1.TabIndex  = 1
; IMPORTANT: The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form.
button1.DialogResult = ObjectClrType( 'System.Windows.Forms.DialogResult', enumDialogResult.OK )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Add Controls
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1.Controls.Add(Button1)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Display the form as a modal dialog box.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ret = Form1.ShowDialog()

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Clean up
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Button1.Dispose()
Form1.Dispose()

Exit


:WbSetColor
;***************************************************************************
;**
;**   Set Color Structure using C# GenerateInMemory
;**
;***************************************************************************
;This is an oddity in the FCL's implementation of the structures and there is really not much that can be done about it.
;System.Drawing.Color has a lot of static methods that are quite handy but they return an VT_I4 variant when returning a color.
;This wouldn't be a problem if all the properties that expect that structure also accepted VT_I4s as an appropriate type.
;But many of them don't.  They are expecting some kind of VT_RECORD instead.  This wouldn't be a problem either
;if the FCL provided an IRECORD interface for the FCL structure so WinBatch could automagically convert the type but it doen't do that either!
;It is a catch 22 and ostensibly a inconsistency in the implementation of the FCL. C'est la guerre.
;objColor  = ObjectClrNew( 'System.Drawing.Color' )
;Form1.BackColor =  objColor.Blue
objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE
objParams.ReferencedAssemblies.Add("System.Drawing.dll")
objParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
objParams.ReferencedAssemblies.Add("System.dll")
strSource = ""
strSource = strSource:`using System.Drawing;`:@CRLF
strSource = strSource:`using System.Windows.Forms;`:@CRLF
strSource = strSource:`namespace WinBatch`:@CRLF
strSource = strSource:`{`:@CRLF
strSource = strSource:` class WbSetColor`:@CRLF
strSource = strSource:` {`:@CRLF
strSource = strSource:`    public void SetForeColor(Control objControl, int a, int r, int g, int b )`:@CRLF
strSource = strSource:`    {`:@CRLF
strSource = strSource:`        try `:@CRLF
strSource = strSource:`        { `:@CRLF
strSource = strSource:`            objControl.ForeColor = Color.FromArgb(a,r,g,b);`:@CRLF
strSource = strSource:`        }`:@CRLF
strSource = strSource:`        catch (System.Exception ex)`:@CRLF
strSource = strSource:`        {`:@CRLF
strSource = strSource:`           System.Diagnostics.Debug.WriteLine (ex.ToString());`:@CRLF
strSource = strSource:`        }`:@CRLF
strSource = strSource:`     }`:@CRLF
strSource = strSource:`    public void SetBackColor(Control objControl, int a, int r, int g, int b )`:@CRLF
strSource = strSource:`    {`:@CRLF
strSource = strSource:`        try `:@CRLF
strSource = strSource:`        { `:@CRLF
strSource = strSource:`            objControl.BackColor = Color.FromArgb(a,r,g,b);`:@CRLF
strSource = strSource:`        }`:@CRLF
strSource = strSource:`        catch (System.Exception ex)`:@CRLF
strSource = strSource:`        {`:@CRLF
strSource = strSource:`           System.Diagnostics.Debug.WriteLine (ex.ToString());`:@CRLF
strSource = strSource:`        }`:@CRLF
strSource = strSource:`     }`:@CRLF
strSource = strSource:`  }`:@CRLF
strSource = strSource:`}`:@CRLF
objResult = objCSharp.CompileAssemblyFromSource(objParams,strSource)
;Compiler Output
If objResult.Output.Count > 0
   strOutput = ''
   For x = 0 To objResult.Output.Count-1
      If strOutput == "" Then strOutput = objResult.Output.Item(x)
      Else strOutput = strOutput:@LF:objResult.Output.Item(x)
   Next
   Pause('Compiler Output',strOutput)
EndIf
Return

Article ID:   W17845
Filename:   Color Structure Issue.txt
File Created: 2014:04:16:09:44:00
Last Updated: 2014:04:16:09:44:00