fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Trying to use RGB() function from Core to set Color on .Net control...

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8946

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
I am trying to use the RGB() function from XSharp.Core, to set the background color of a .Net UI control.

It gives an error:
XS0029 Cannot implicitly convert type 'dword' to 'System.Drawing.Color'



I also tried to cast the result to Sytem.Drawing.Color, but it still gave an error: Error XS0030 Cannot convert type 'dword' to 'System.Drawing.Color'




On a whim, I added a reference to the VfpToolkit for .Net, and used Common.RGB() function and it compiles and runs with no errors.
( github.com/mattslay/Visual-FoxPro-Toolkit-for-.NET )



Here is the source from from for the RGB() function in VfpToolkit for .Net.
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by FoxProMatt_MattSlay.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8949

  robert's Avatar robert Offline Posts: 946
Matt,

RGB is a VO compatibilty function. We should probably move it out of XSharp.Core and into XSharp.VO.
VO did not know System.Drawing.Color

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8952

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
You can easily use the code from VFP Toolkit for .Net to add this Function correctly typed to XSharp.Core, right?

Here's the C# code:
/// <summary>
/// This method receives three parameters and returns a color object from those values.
/// In VFP, this method returned a value whereas, in this case we are actually returning
/// a System.Drawing.Color object.
/// <p/><pre>
/// Example:
/// Color o = VFPToolkit.common.RGB(255,255,255);
/// </pre>
/// </summary>
/// <param name="R"></param>
/// <param name="G"></param>
/// <param name="B"></param>
/// <returns></returns>
public static System.Drawing.Color RGB(int R, int G, int B)
{
	return System.Drawing.Color.FromArgb(R,G,B);
}

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8954

  Chris's Avatar Chris Offline Posts: 1109
Hi Matt,

Why creating a new function, when you can already use directly Color.FromArgb(r,g,b) ?

The only reason RGB() was implemented in X# is because it is a function available in VO, used in conjunction with the system classes of VO, some of which indeed expect colors in the form of dword values. In Windows.Forms/.Net GDI classes, there's a special class used for representing Colors, that's the Color class, so you'd normally simply use the static method of this class, to obtain your desired color with Color.FromArgb().
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8955

  Karl-Heinz's Avatar Karl-Heinz Offline Posts: 234
Hi Chris,

The X# RGB() returns a DWORD, but the VO 2.8 SP3 RGB() returns an INT. Just searched the VO-NG and it seems that the SP4 RGB() introduced the DWORD.

regards
Karl-Heinz

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8956

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129

Why creating a new function, when you can already use directly Color.FromArgb(r,g,b) ?


FoxPro compatibility, so we don’t have to change VFP code.

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8957

  Chris's Avatar Chris Offline Posts: 1109
Hi Karl-Heinz,

Are you absolutely sure? I do not have SP3 to check, but I checked it in VO27 and already in it it returned a DWORD.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8958

  Chris's Avatar Chris Offline Posts: 1109

FoxProMatt_MattSlay wrote:

Why creating a new function, when you can already use directly Color.FromArgb(r,g,b) ?


FoxPro compatibility, so we don’t have to change VFP code.


Ah, OK, so there's a function named RGB() also in VFP? What value does it return? Obviously not a System.Drawing.Color...

In case it does not return a numeric value (which I suspect it does), then we will need to do what Robert said, move the current RGB() function to the XSharp.VO library, and implement a new one in XSharp.VFP, which will behave exactly as it behaves in VFP. People who port VO apps will be using the XSharp.VO library and people porting VFP apps will be using the XSharp.VFP library.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8959

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
VFP RGB() returns integer.

See my code sample to Robert above where the VFP Toolkit for .Net has a RGB() function to return Color type.

Most all VFP Functions have been implemented in .Net flavor in that Toolkit.

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8960

  Chris's Avatar Chris Offline Posts: 1109

FoxProMatt_MattSlay wrote: VFP RGB() returns integer.

See my code sample to Robert above where the VFP Toolkit for .Net has a RGB() function to return Color type.

Most all VFP Functions have been implemented in .Net flavor in that Toolkit.


OK, so people who will be using the Toolkit, will be using this version of RGB(). But in order to be fully compatible with VFP in the official X# runtime/VFP dialect, we will probably need to implement RGB() in a way that it returns the same thing as it does in VFP. Possibly the already implemented function will do.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8961

  Chris's Avatar Chris Offline Posts: 1109
Just to explain why we need to make it work in a compatible way with VFP:

I guess it is very possible that people in VFP have written code like that:

LOCAL color
...
color = RGB(30,50,100)
...

LOCAL blue
blue = Mod(color , 256)
....

LOCAL newcolor
newcolor = RGB(255,0,blue) // create a new color based on the previous one

With the current X# implementation of RGB(), this code will continue working just fine. If we made RGB() return a system.Drawing.Color object instead, then the above code would still compile, but would throw an error at runtime. So VFP programmers would need to go to all such occurrences in their code and change it (possibly in thousands of lines...) so that it takes into account the new behavior of RGB().

Obviously nobody would want to do that and we also do not want to force people to make such and so many changes. And that does not apply only to RGB(), but the same concept applies to every other VFP function and code construct.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8962

  lumberjack's Avatar lumberjack Offline Posts: 414

robert wrote: RGB is a VO compatibilty function. We should probably move it out of XSharp.Core and into XSharp.VO.
VO did not know System.Drawing.Color

I initially thought the problem can be solved by a mere overloaded function, but alas VO RGB() return a Number (DWORD), while the VFP RGB() return a Color object. I second what you suggest.
Just my 2c worth.
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

Last edit: by lumberjack.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8963

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
Chris - Indeed, it is complex, and will need to be figured out.

The use case that I was tackling was a .net UI control where I needed to apply a set of rgb integers from VFP to a .net Color, so I assumed RGB(), or some version of it, was the way to go.

It’s easy to teach or document what to do when writing new code, but I don’t know the answer of how to easily handle this when converting exiting VFP code to work with native .Net UI controls.

Please Log in or Create an account to join the conversation.

Last edit: by FoxProMatt_MattSlay.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8964

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
Johan - Native VFP RGB() returns an integer, not a Color object. VFP has no such thing as a Color object.

The RGB() function in the VFP Toolkit for .Net does return a Color object.

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8965

  Karl-Heinz's Avatar Karl-Heinz Offline Posts: 234

Chris wrote: Hi Karl-Heinz,

Are you absolutely sure? I do not have SP3 to check, but I checked it in VO27 and already in it it returned a DWORD.


Hi Chris,

I´m absolutely sure.

The 2.8 SP3 declaration looks a a little bit strange ( note the two missing AS BYTE ) :

_DLL FUNCTION RGB (bR, bG, bB AS BYTE) AS INT PASCAL:VO28RUN.Rgb

But it works as expected:
LOCAL iColor AS INT  
   
    iColor := RGB ( 127 , 0 , 127 )  

while this throws the error:

"overflow or loss of data possible converting longint -> dword:51422"
LOCAL dwColor AS DWORD

    dwColor := RGB ( 127 , 0 , 127 )  

I´ve never used 2.7, but i´ve started in a VM VO 2.5b3 and indeed a DWORD is returned - can´t remember that ;)It seems that at least 2.8 SP3 introduced the INT, while SP4 switched back to a DWORD again.

There are also some places in the x# GitHUb VO sources that indicate that RGB () did return an INT:

e.g.

CLASS MailTreeView INHERIT TreeView

METHOD CustomDraw(lParam)
LOCAL pNMCustomDraw AS _winNMLVCUSTOMDRAW
LOCAL dwDrawStage AS DWORD

pNMCustomDraw := PTR(_CAST, lParam)
dwDrawStage := pNMCustomDraw.nmcd.dwDrawStage

IF dwDrawStage = CDDS_PREPAINT
RETURN CDRF_NOTIFYITEMDRAW
ELSEIF dwDrawStage = CDDS_ITEMPREPAINT
IF _And( pNMCustomDraw.nmcd.uItemState , CDIS_SELECTED ) > 0
pNMCustomDraw.clrText := DWORD(RGB(255, 255, 255))
pNMCustomDraw.clrTextBk := DWORD(RGB(0, 0, 128))
ELSE
pNMCustomDraw.clrText := DWORD(RGB(0, 0, 0))
pNMCustomDraw.clrTextBk := DWORD(RGB(190, 255, 255))
ENDIF
ENDIF

RETURN CDRF_DODEFAULT


regards
Karl-Heinz

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 6 days ago #8966

  lumberjack's Avatar lumberjack Offline Posts: 414
Hi Matt,

FoxProMatt_MattSlay wrote: The use case that I was tackling was a .net UI control where I needed to apply a set of rgb integers from VFP to a .net Color, so I assumed RGB(), or some version of it, was the way to go.
(snipped...)but I don’t know the answer of how to easily handle this when converting exiting VFP code to work with native .Net UI controls.

The standard UI Winform do it this way, no need to call RGB()
SELF:oTextBox2:BackColor := System.Drawing.Color.FromArgb( 0,0,170 )
SELF:oTextBox3:BackColor := System.Drawing.Color.Brown
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

Last edit: by lumberjack. Reason: Two examples

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 5 days ago #8967

  robert's Avatar robert Offline Posts: 946
Karl-Heinz, Chris,

I know it's strange but you are both right. The function RGB in VO has been defined twice with a different prototype:

In VOLIB\SysLib\StdLib.prg as
_DLL FUNCTION RGB (bR, bG, bB AS BYTE) AS DWORD PASCAL:VO28RUN.Rgb

However the repo has this function ion SysLib.prg
_DLL FUNCTION RGB (bR, bG, bB AS BYTE) AS INT PASCAL:VO28RUN.Rgb

I am not sure how this difference was created but it should have been the same, since the repo was created from this SysLib.prg.
Apparently the source in SysLib.prg has been changed since the last time the repo was built.
In 2.8 SP4 both definitions are:
_DLL FUNCTION RGB (bR, bG, bB AS BYTE) AS DWORD PASCAL:VO28RUN.Rgb

Robert

Robert




There is also a (commented out) source code version of this in the Win32 source (wingdi.prg)
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 5 days ago #8968

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
Johan - Here is what I have coming out of a VFP repo:



So, a string of 3 numbers, separated by 2 commas. Now I have to render that into a corresponding .Net control property with a function of some sort to get in into a Color object.

It's basically just some string manipulation, so it's not rocket science, but there are lots of "xxxColor" properties in VFP, and they have to be mapped to the correct .Net Color property, if present, or add a wrapper property with the same name as the VFP property and map it to the correct .Net property in the Setter.
Attachments:

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 3 days ago #8975

  lumberjack's Avatar lumberjack Offline Posts: 414

FoxProMatt_MattSlay wrote: Johan - Here is what I have coming out of a VFP repo:


So, a string of 3 numbers, separated by 2 commas. Now I have to render that into a corresponding .Net control property with a function of some sort to get in into a Color object.
It's basically just some string manipulation, so it's not rocket science, but there are lots of "xxxColor" properties in VFP, and they have to be mapped to the correct .Net Color property, if present, or add a wrapper property with the same name as the VFP property and map it to the correct .Net property in the Setter.

Can you give some examples of the Color formats for "Black, Cyan, etc."? Am sure those directly map onto the same name in .NET...
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

Trying to use RGB() function from Core to set Color on .Net control... 2 weeks 3 days ago #8976

  FoxProMatt_MattSlay's Avatar Topic Author FoxProMatt_MattSlay Away Posts: 129
Attachments:

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2