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

extracted constants #49

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override void OnPaint(PaintEventArgs pevent)
protected override void WndProc(ref Message m)
{
// Filter out the WM_ERASEBKGND message since we do that on our own with foreground painting
if (m.Msg == NativeMethods.WM_ERASEBKGND)
if (m.Msg == NativeConstants.Messages.WM_ERASEBKGND)
{
// return 0 (no erasing)
m.Result = (IntPtr)1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ private void wndProcInternal(ref Message m)
switch (m.Msg)
{
//disabled box
case NativeMethods.WM_CTLCOLORSTATIC:
case NativeConstants.Messages.WM_CTLCOLORSTATIC:
NativeMethods.SetBkColorInternal(m.WParam, ColorTranslator.ToWin32(Color.Orange));

IntPtr brush = NativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(Color.BlueViolet));
m.Result = brush;
return;

case 0x133: //coloredit, for the edit area of editable comboboxes
NativeMethods.SetBkModeInternal(m.WParam, NativeMethods.BKM_OPAQUE);
NativeMethods.SetBkModeInternal(m.WParam, NativeConstants.BKM_OPAQUE);
NativeMethods.SetTextColorInternal(m.WParam, ColorTranslator.ToWin32(ForeColor));
NativeMethods.SetBkColorInternal(m.WParam, ColorTranslator.ToWin32(BackColor));

Expand All @@ -158,7 +158,7 @@ private void wndProcInternal(ref Message m)
return;

case 0x134: //colorlistbox
NativeMethods.SetBkModeInternal(m.WParam, NativeMethods.BKM_OPAQUE);
NativeMethods.SetBkModeInternal(m.WParam, NativeConstants.BKM_OPAQUE);
NativeMethods.SetTextColorInternal(m.WParam, ColorTranslator.ToWin32(ForeColor));
NativeMethods.SetBkColorInternal(m.WParam, ColorTranslator.ToWin32(BackColor));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override void Dispose(bool disposing)
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != NativeMethods.WM_ERASEBKGND)
if (m.Msg != NativeConstants.Messages.WM_ERASEBKGND)
{
base.OnNotifyMessage(m);
}
Expand All @@ -107,9 +107,9 @@ protected override void WndProc(ref Message m)
}
private void wndProcInternal(ref Message m)
{
if (m.Msg != NativeMethods.WM_REFLECT + NativeMethods.WM_NOFITY)
if (m.Msg != NativeConstants.Messages.WM_REFLECT + NativeConstants.Messages.WM_NOFITY)
{
if (m.Msg == NativeMethods.WM_LBUTTONUP)
if (m.Msg == NativeConstants.Messages.WM_LBUTTONUP)
{
base.DefWndProc(ref m);
return;
Expand All @@ -127,7 +127,7 @@ private void wndProcInternal(ref Message m)
}

NativeMethods.NMHDR pnmhdr = (NativeMethods.NMHDR)nmhdrParam;
if (pnmhdr.code != NativeMethods.NM_CUSTOMDRAW)
if (pnmhdr.code != NativeConstants.NM_CUSTOMDRAW)
{
base.WndProc(ref m);
return;
Expand All @@ -147,7 +147,7 @@ private void wndProcInternal(ref Message m)
{
m.Result = pnmlv.dwItemType switch
{
NativeMethods.LVCDI_GROUP => drawGroupHeader(m.HWnd, pnmlv),
NativeConstants.LVCDI_GROUP => drawGroupHeader(m.HWnd, pnmlv),
_ => new IntPtr((int)NativeMethods.CDRF.NotifyItemDraw)
};

Expand All @@ -157,7 +157,7 @@ private void wndProcInternal(ref Message m)
{
switch (pnmlv.dwItemType)
{
case NativeMethods.LVCDI_ITEM:
case NativeConstants.LVCDI_ITEM:
int itemIndex = (int)pnmlv.nmcd.dwItemSpec;

// skip items that are not selected as they are already drawn correctly
Expand Down Expand Up @@ -194,7 +194,7 @@ private IntPtr drawItem(IntPtr mHWnd, int itemIndex, NativeMethods.NMLVCUSTOMDRA
left = (int)ItemBoundsPortion.Entire
};

NativeMethods.SendMessageInternal(mHWnd, NativeMethods.LVM_GETITEMRECT, itemIndex, ref rectHeader);
NativeMethods.SendMessageInternal(mHWnd, NativeConstants.Messages.LVM_GETITEMRECT, itemIndex, ref rectHeader);
using (Graphics g = Graphics.FromHdc(pnmlv.nmcd.hdc))
{
// background color
Expand Down Expand Up @@ -229,12 +229,12 @@ private IntPtr drawGroupHeader(IntPtr mHWnd, NativeMethods.NMLVCUSTOMDRAW pnmlv)
{
NativeMethods.RECT rectHeader = new()
{
top = NativeMethods.LVGGR_HEADER
top = NativeConstants.LVGGR_HEADER
};

int groupIndex = (int)pnmlv.nmcd.dwItemSpec;

NativeMethods.SendMessageInternal(mHWnd, NativeMethods.LVM_GETGROUPRECT, groupIndex,
NativeMethods.SendMessageInternal(mHWnd, NativeConstants.Messages.LVM_GETGROUPRECT, groupIndex,
ref rectHeader);
using (Graphics g = Graphics.FromHdc(pnmlv.nmcd.hdc))
{
Expand All @@ -246,11 +246,11 @@ private IntPtr drawGroupHeader(IntPtr mHWnd, NativeMethods.NMLVCUSTOMDRAW pnmlv)
// Group header text
NativeMethods.LVGROUP listviewGroup = new();
listviewGroup.cbSize = (uint)Marshal.SizeOf(listviewGroup);
listviewGroup.mask = NativeMethods.LVGF_GROUPID | NativeMethods.LVGF_HEADER;
listviewGroup.mask = NativeConstants.LVGF_GROUPID | NativeConstants.LVGF_HEADER;

NativeMethods.SendMessageInternal(
mHWnd,
NativeMethods.LVM_GETGROUPINFO,
NativeConstants.Messages.LVM_GETGROUPINFO,
groupIndex,
ref listviewGroup);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private void findUpDown()
bool bFound = false;

// find the UpDown control
IntPtr pWnd = NativeMethods.GetWindow(Handle, NativeMethods.GW_CHILD);
IntPtr pWnd = NativeMethods.GetWindow(Handle, NativeConstants.GW_CHILD);

while (pWnd != IntPtr.Zero)
{
Expand All @@ -486,7 +486,7 @@ private void findUpDown()
break;
}

pWnd = NativeMethods.GetWindow(pWnd, NativeMethods.GW_HWNDNEXT);
pWnd = NativeMethods.GetWindow(pWnd, NativeConstants.GW_HWNDNEXT);
}

if (!bFound && _bUpDown)
Expand Down Expand Up @@ -519,7 +519,7 @@ private int scUpDown_SubClassedWndProc(ref Message m)

switch (m.Msg)
{
case NativeMethods.WM_PAINT:
case NativeConstants.Messages.WM_PAINT:
//------------------------
// redraw
IntPtr hDc = NativeMethods.GetWindowDC(_scUpDown.Handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System.Globalization;
using System.Reflection;

namespace StylableWinFormsControls.Native
{
/// <summary>
/// contains constants required for native calls
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Roslynator",
"RCS1181:Convert comment to documentation comment.",
Justification = "Most of the comments are not documentation, but internal notes.")]
internal static class NativeConstants
{
/// <summary>
/// Background is filled with the current background color before the text, hatched brush, or pen is drawn.
/// </summary>
internal const int BKM_OPAQUE = 2;

/// <summary>
/// Background remains untouched.
/// </summary>
internal const int BKM_TRANSPARENT = 1;
/// <summary>
/// constant to define dark mode option
/// </summary>
internal const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;

/// <summary>
/// constant to define dark mode option
/// </summary>
internal const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20_H1 = 19;

internal const int GW_CHILD = 5;
internal const int GW_HWNDFIRST = 0;
internal const int GW_HWNDLAST = 1;
internal const int GW_HWNDNEXT = 2;
internal const int GW_HWNDPREV = 3;
internal const int GW_OWNER = 4;
internal const int LVCDI_GROUP = 0x1;
internal const int LVCDI_ITEM = 0x0;
internal const int LVCDI_ITEMSLIST = 0x2;

internal const int LVGA_FOOTER_CENTER = 0x10;
internal const int LVGA_FOOTER_LEFT = 0x8;
// Don't forget to validate exclusivity
internal const int LVGA_FOOTER_RIGHT = 0x20;

internal const int LVGA_HEADER_CENTER = 0x2;
internal const int LVGA_HEADER_LEFT = 0x1;
// Don't forget to validate exclusivity
internal const int LVGA_HEADER_RIGHT = 0x4;

internal const int LVGF_ALIGN = 0x8;
// pszDescriptionBottom is valid
internal const int LVGF_DESCRIPTIONBOTTOM = 0x800;

// pszDescriptionTop is valid
internal const int LVGF_DESCRIPTIONTOP = 0x400;

// iExtendedImage is valid
internal const int LVGF_EXTENDEDIMAGE = 0x2000;

internal const int LVGF_FOOTER = 0x2;
internal const int LVGF_GROUPID = 0x10;
internal const int LVGF_HEADER = 0x1;
// iFirstItem and cItems are valid
internal const int LVGF_ITEMS = 0x4000;

// Listview group specific flags
internal const int LVGF_NONE = 0x0;

internal const int LVGF_STATE = 0x4;
// pszSubsetTitle is valid
internal const int LVGF_SUBSET = 0x8000;

// readonly, cItems holds count of items in visible subset, iFirstItem is valid
internal const int LVGF_SUBSETITEMS = 0x10000;

// pszSubtitle is valid
internal const int LVGF_SUBTITLE = 0x100;

// pszTask is valid
internal const int LVGF_TASK = 0x200;

// iTitleImage is valid
internal const int LVGF_TITLEIMAGE = 0x1000;

// Entire expanded group
internal const int LVGGR_GROUP = 0;

// Header only (collapsed group)
internal const int LVGGR_HEADER = 1;

// Label only
internal const int LVGGR_LABEL = 2;

// subset link only
internal const int LVGGR_SUBSETLINK = 3;

internal const int LVGS_COLLAPSED = 0x1;
internal const int LVGS_COLLAPSIBLE = 0x8;
internal const int LVGS_FOCUSED = 0x10;
internal const int LVGS_HIDDEN = 0x2;
internal const int LVGS_NOHEADER = 0x4;
// Listview group styles
internal const int LVGS_NORMAL = 0x0;

internal const int LVGS_SELECTED = 0x20;
internal const int LVGS_SUBSETED = 0x40;
internal const int LVGS_SUBSETLINKFOCUSED = 0x80;
internal const int LVM_FIRST = 0x1000;
/// <summary>
/// Return value from <see cref="WM_MOUSEACTIVATE"/>: Activates the window, and does not discard the mouse message
/// </summary>
internal const uint MA_ACTIVATE = 1;

/// <summary>
/// Return value from <see cref="WM_MOUSEACTIVATE"/>: Activates the window, and discards the mouse message
/// </summary>
internal const uint MA_ACTIVATEANDEAT = 2;

internal const int NM_CLICK = NM_FIRST - 2;
internal const int NM_CUSTOMDRAW = NM_FIRST - 12;
internal const int NM_FIRST = 0;
/*
* GetWindow() Constants
*/
internal static class Messages
{
internal const int LVM_GETGROUPINFO = LVM_FIRST + 149;
internal const int LVM_GETGROUPRECT = LVM_FIRST + 98;
internal const int LVM_GETITEMRECT = LVM_FIRST + 14;
/// <summary>
/// A static control, or an edit control that is read-only or disabled,
/// sends the WM_CTLCOLORSTATIC message to its parent window when the control is about to be drawn.
/// By responding to this message, the parent window can use the specified device context handle to set the text and
/// background colors of the static control.
/// </summary>
internal const int WM_CTLCOLORSTATIC = 0x138;
/// <summary>
/// Sent when the window background must be erased (for example, when a window is resized).
/// The message is sent to prepare an invalidated portion of a window for painting.
/// </summary>
internal const int WM_ERASEBKGND = 0x14;

/// <summary>
/// Posted when the user releases the left mouse button while the cursor is in the client area of a window.
/// </summary>
internal const int WM_LBUTTONUP = 0x202;
/// <summary>
/// Sent when the cursor is in an inactive window and the user presses a mouse button.
/// </summary>
internal const uint WM_MOUSEACTIVATE = 0x21;

/// <summary>
/// Sent by a common control to its parent window when an event has occurred or the control requires some information.
/// </summary>
internal const int WM_NOFITY = 0x4E;

/// <summary>
/// The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window.
/// </summary>
internal const int WM_PAINT = 0xF;

/// <summary>
/// MFC Message Reflection
/// </summary>
/// <remarks>See more: https://learn.microsoft.com/en-us/cpp/mfc/tn062-message-reflection-for-windows-controls</remarks>
internal const int WM_REFLECT = 0x2000;

/// <summary>
/// Sent to a window to allow changes in that window to be redrawn, or to prevent changes in that window from being redrawn
/// </summary>
internal const int WM_SETREDRAW = 11;

#region reverse msg value logic
private static readonly Dictionary<long, string> MESSAGE_NAME_DICT = new();
private static void initMessageNameFromValue()
{
//get all constants
FieldInfo[] fieldInfos = typeof(Messages).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
List<FieldInfo> constants = (from f in fieldInfos where f.IsLiteral && !f.IsInitOnly && (typeof(int).IsAssignableFrom(f.FieldType) || typeof(uint).IsAssignableFrom(f.FieldType)) select f).ToList();


//add all constants to the messageNameDict
constants.ForEach(f =>
{
object? value = f.GetValue(null);
if (value is not null)
{
MESSAGE_NAME_DICT.Add(Convert.ToInt64(value, CultureInfo.InvariantCulture), f.Name);
}
}
);
}
public static string Reverse(int value)
{
if (MESSAGE_NAME_DICT.Count == 0)
{
initMessageNameFromValue();
}
if (MESSAGE_NAME_DICT.ContainsKey(value))
{
return MESSAGE_NAME_DICT[value];
}
return $"Unknown Value({value})";
}
#endregion
}
}
}
Loading