Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackColor and ForeColor in DrawListViewColumnHeaderEventArgs have alpha component set to 0 #13048

Closed
anders9ustafsson opened this issue Feb 28, 2025 · 8 comments · Fixed by #13062
Assignees
Labels
area-controls-ListView 💥 regression-release Regression from a public release customer-reported waiting-author-feedback The team requires more information from the author
Milestone

Comments

@anders9ustafsson
Copy link

anders9ustafsson commented Feb 28, 2025

.NET version

.NET 8.0

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

In our code, we are using the OwnerDraw mode for System.Windows.Forms.ListView. When accessing the ForeColor and BackColor properties of DrawListViewColumnHeaderEventArgs, we notice that the alpha component of respective color is 0, although we would have expected 255. Therefore, e.g. when drawing text using ForeColor, no text can be seen in the header.

Looking at the source code where DrawListViewColumnHeaderEventArgs is defined in ListView, I notice that the colors are obtained using P/Invoke methods GetTextColor and GetBkColor, respectively:

Color foreColor = Color.FromArgb((int)PInvoke.GetTextColor(nmcd->hdc).Value);
Color backColor = Color.FromArgb((int)PInvoke.GetBkColor(nmcd->hdc).Value);

However, if I interpret the documentation for the GetTextColor function correctly, it returns a COLORREF value, which only appears to be an RGB color representation with remaining byte set to 0 (0x00bbggrr). Is this the explanation why the alpha component is 0 in the ForeColor and BackColor properties?

Steps to reproduce

var listView = new ListView { OwnerDraw = true, View = View.Details, Columns = { "A", "B", "C" } };
listView.DrawColumnHeader += (s, e) => e.Graphics.DrawString(e.Header.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Location);

Compare with:

var listView = new ListView { OwnerDraw = false, View = View.Details, Columns = { "A", "B", "C" } };
@anders9ustafsson anders9ustafsson added the untriaged The team needs to look at this issue in the next triage label Feb 28, 2025
@Zheng-Li01
Copy link
Member

Zheng-Li01 commented Mar 3, 2025

This is a regression issue, can reproduce from 8.0 to latest .NET 10.0, cannot reproduce on .NET 6.0/7.0 and .NET Framework as below screenshot.

Not repro:
Image

Repro:
Image

@Zheng-Li01 Zheng-Li01 added the 💥 regression-release Regression from a public release label Mar 3, 2025
@anders9ustafsson
Copy link
Author

anders9ustafsson commented Mar 3, 2025

Thanks @Zheng-Li01 for starting to look into this issue. In the release/7.0 branch foreColor and backColor are defined like this (see source code):

Color foreColor = Gdi32.GetTextColor(nmcd->hdc);
Color backColor = Gdi32.GetBkColor(nmcd->hdc);

@anders9ustafsson
Copy link
Author

Using dotPeek to decompile the .NET Framework 4.8 implementation gives this source code:

Color foreColor = ColorTranslator.FromWin32(SafeNativeMethods.GetTextColor(new HandleRef(this, ptr2->hdc)));
Color backColor = ColorTranslator.FromWin32(SafeNativeMethods.GetBkColor(new HandleRef(this, ptr2->hdc)));

@JeremyKuhne JeremyKuhne self-assigned this Mar 3, 2025
JeremyKuhne added a commit to JeremyKuhne/winforms that referenced this issue Mar 3, 2025
We accidentally dropped the call to ColorTranslator, which properly handles alpha when converting COLORREF. COLORREF's implicit conversion does this correctly.

Fixes dotnet#13048
@dotnet-policy-service dotnet-policy-service bot added the 🚧 work in progress Work that is current in progress label Mar 3, 2025
@JeremyKuhne
Copy link
Member

@anders9ustafsson Thanks for the report! I've got a fix up for .NET 10. Hopefully you can work around this by changing the color in the event args to remove the transparency.

JeremyKuhne added a commit that referenced this issue Mar 3, 2025
We accidentally dropped the call to ColorTranslator, which properly handles alpha when converting COLORREF. COLORREF's implicit conversion does this correctly.

Fixes #13048
@dotnet-policy-service dotnet-policy-service bot removed the untriaged The team needs to look at this issue in the next triage label Mar 3, 2025
@JeremyKuhne JeremyKuhne reopened this Mar 3, 2025
@JeremyKuhne JeremyKuhne added waiting-author-feedback The team requires more information from the author customer-reported and removed 🚧 work in progress Work that is current in progress labels Mar 3, 2025
@anders9ustafsson
Copy link
Author

Many thanks, @JeremyKuhne ! Any chance that the fix will make it into the next .NET 8 update as well?

@JeremyKuhne
Copy link
Member

@anders9ustafsson I would have to try and get approval from servicing. If I get approval, it might not make the next update (might be the one after). Are you able to work around this?

@anders9ustafsson
Copy link
Author

Yes, I have already implemented a workaround, so we can cope without a correction in .NET 8. I just want to get rid of the workaround as soon as possible 🙂

@Philip-Wang01
Copy link
Contributor

Verified the issue on latest .NET SDK 10.0.100-preview.3.25155.12 + private dlls built from the main branch of WinForms repo. The issue has been fixed that the text can be seen in the header.

Image

@Philip-Wang01 Philip-Wang01 added this to the 10.0 Preview3 milestone Mar 6, 2025
LeafShi1 pushed a commit to LeafShi1/winforms that referenced this issue Mar 6, 2025
We accidentally dropped the call to ColorTranslator, which properly handles alpha when converting COLORREF. COLORREF's implicit conversion does this correctly.

Fixes dotnet#13048
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-ListView 💥 regression-release Regression from a public release customer-reported waiting-author-feedback The team requires more information from the author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants