Skip to content
Closed
4 changes: 2 additions & 2 deletions src/Common/src/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4044,7 +4044,7 @@ public struct WINDOWPOS {
}

[StructLayout(LayoutKind.Sequential)]
public unsafe struct HDLAYOUT {
public struct HDLAYOUT {
public IntPtr prc; // pointer to a RECT
public IntPtr pwpos; // pointer to a WINDOWPOS
}
Expand Down Expand Up @@ -4374,7 +4374,7 @@ public struct LVITEM {
public int cColumns; // tile view columns
public IntPtr puColumns;

public unsafe void Reset() {
public void Reset() {
pszText = null;
mask = 0;
iItem = 0;
Expand Down
30 changes: 15 additions & 15 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12842,15 +12842,15 @@ private void WmMove(ref Message m) {
/// Handles the WM_NOTIFY message
/// </devdoc>
/// <internalonly/>
private unsafe void WmNotify(ref Message m) {
NativeMethods.NMHDR* nmhdr = (NativeMethods.NMHDR*)m.LParam;
if (!ReflectMessage(nmhdr->hwndFrom,ref m)) {
if(nmhdr->code == NativeMethods.TTN_SHOW) {
m.Result = UnsafeNativeMethods.SendMessage(new HandleRef(null, nmhdr->hwndFrom), Interop.WindowMessages.WM_REFLECT + m.Msg, m.WParam, m.LParam);
private void WmNotify(ref Message m) {
ref readonly NativeMethods.NMHDR nmhdr = ref m.GetLParamRef<NativeMethods.NMHDR>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there actually any guarantee that the underlying nmhdr is readonly or are you just using this to say the local WmNotify shouldn't modify the state?

if (!ReflectMessage(nmhdr.hwndFrom,ref m)) {
if(nmhdr.code == NativeMethods.TTN_SHOW) {
m.Result = UnsafeNativeMethods.SendMessage(new HandleRef(null, nmhdr.hwndFrom), Interop.WindowMessages.WM_REFLECT + m.Msg, m.WParam, m.LParam);
return;
}
if(nmhdr->code == NativeMethods.TTN_POP) {
UnsafeNativeMethods.SendMessage(new HandleRef(null, nmhdr->hwndFrom), Interop.WindowMessages.WM_REFLECT + m.Msg, m.WParam, m.LParam);
if(nmhdr.code == NativeMethods.TTN_POP) {
UnsafeNativeMethods.SendMessage(new HandleRef(null, nmhdr.hwndFrom), Interop.WindowMessages.WM_REFLECT + m.Msg, m.WParam, m.LParam);
}

DefWndProc(ref m);
Expand Down Expand Up @@ -13089,27 +13089,27 @@ private void WmSetCursor(ref Message m) {
/// Handles the WM_WINDOWPOSCHANGING message
/// </devdoc>
/// <internalonly/>
private unsafe void WmWindowPosChanging(ref Message m) {
private void WmWindowPosChanging(ref Message m) {

// We let this fall through to defwndproc unless we are being surfaced as
// an ActiveX control. In that case, we must let the ActiveX side of things
// manipulate our bounds here.
//
if (IsActiveX) {
NativeMethods.WINDOWPOS* wp = (NativeMethods.WINDOWPOS *)m.LParam;
ref NativeMethods.WINDOWPOS wp = ref m.GetLParamRef<NativeMethods.WINDOWPOS>();
// Only call UpdateBounds if the new bounds are different.
//
bool different = false;

if ((wp->flags & NativeMethods.SWP_NOMOVE) == 0 && (wp->x != Left || wp->y != Top)) {
if ((wp.flags & NativeMethods.SWP_NOMOVE) == 0 && (wp.x != Left || wp.y != Top)) {
different = true;
}
if ((wp->flags & NativeMethods.SWP_NOSIZE) == 0 && (wp->cx != Width || wp->cy != Height)) {
if ((wp.flags & NativeMethods.SWP_NOSIZE) == 0 && (wp.cx != Width || wp.cy != Height)) {
different = true;
}

if (different) {
ActiveXUpdateBounds(ref wp->x, ref wp->y, ref wp->cx, ref wp->cy, wp->flags);
ActiveXUpdateBounds(ref wp.x, ref wp.y, ref wp.cx, ref wp.cy, wp.flags);
}
}

Expand Down Expand Up @@ -13323,15 +13323,15 @@ private void WmUpdateUIState(ref Message m) {
/// Handles the WM_WINDOWPOSCHANGED message
/// </devdoc>
/// <internalonly/>
private unsafe void WmWindowPosChanged(ref Message m) {
private void WmWindowPosChanged(ref Message m) {
DefWndProc(ref m);
// Update new size / position
UpdateBounds();
if (parent != null && UnsafeNativeMethods.GetParent(new HandleRef(window, InternalHandle)) == parent.InternalHandle &&
(state & STATE_NOZORDER) == 0) {

NativeMethods.WINDOWPOS* wp = (NativeMethods.WINDOWPOS *)m.LParam;
if ((wp->flags & NativeMethods.SWP_NOZORDER) == 0) {
ref readonly NativeMethods.WINDOWPOS wp = ref m.GetLParamRef<NativeMethods.WINDOWPOS>();
if ((wp.flags & NativeMethods.SWP_NOZORDER) == 0) {
parent.UpdateChildControlIndex(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29802,22 +29802,22 @@ private void WmGetDlgCode(ref Message m)
}
}

private unsafe bool WmNotify(ref Message m)
private bool WmNotify(ref Message m)
{
if (m.LParam == IntPtr.Zero)
{
return false;
}

NativeMethods.NMHDR* nmhdr = (NativeMethods.NMHDR *)m.LParam;
if (nmhdr->code == NativeMethods.TTN_GETDISPINFO && !DesignMode)
ref readonly NativeMethods.NMHDR nmhdr = ref m.GetLParamRef<NativeMethods.NMHDR>();
if (nmhdr.code == NativeMethods.TTN_GETDISPINFO && !DesignMode)
{
string toolTip = this.ToolTipPrivate;

if (!string.IsNullOrEmpty(toolTip))
{
// MSDN: Setting the max width has the added benefit of enabling multiline tool tips!
UnsafeNativeMethods.SendMessage(new HandleRef(this, nmhdr->hwndFrom), NativeMethods.TTM_SETMAXTIPWIDTH, 0, SystemInformation.MaxWindowTrackSize.Width);
UnsafeNativeMethods.SendMessage(new HandleRef(this, nmhdr.hwndFrom), NativeMethods.TTM_SETMAXTIPWIDTH, 0, SystemInformation.MaxWindowTrackSize.Width);
NativeMethods.TOOLTIPTEXT ttt = (NativeMethods.TOOLTIPTEXT) m.GetLParam(typeof(NativeMethods.TOOLTIPTEXT));

ttt.lpszText = toolTip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ protected override void OnSystemColorsChanged(EventArgs e) {
private void WmReflectCommand(ref Message m) {
if (m.HWnd == Handle) {

NativeMethods.NMHDR nmhdr = (NativeMethods.NMHDR)m.GetLParam(typeof(NativeMethods.NMHDR));
ref readonly NativeMethods.NMHDR nmhdr = ref m.GetLParamRef<NativeMethods.NMHDR>();
switch (nmhdr.code) {
case NativeMethods.DTN_CLOSEUP:
WmCloseUp(ref m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ private void FillRectDither(IntPtr dc, NativeMethods.RECT rc) {

protected override void WndProc(ref Message m) {
if (m.Msg == Interop.WindowMessages.WM_REFLECT + Interop.WindowMessages.WM_NOTIFY) {
NativeMethods.NMHDR nmh = (NativeMethods.NMHDR)m.GetLParam(typeof(NativeMethods.NMHDR));
ref readonly NativeMethods.NMHDR nmh = ref m.GetLParamRef<NativeMethods.NMHDR>();
if (nmh.code == NativeMethods.NM_CUSTOMDRAW) {
OnCustomDraw(ref m);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
Expand All @@ -22,7 +21,7 @@ internal DpiChangedEventArgs(int old, Message m)
DeviceDpiOld = old;
DeviceDpiNew = NativeMethods.Util.SignedLOWORD(m.WParam);
Debug.Assert(NativeMethods.Util.SignedHIWORD(m.WParam) == DeviceDpiNew, "Non-square pixels!");
NativeMethods.RECT suggestedRect = Marshal.PtrToStructure<NativeMethods.RECT>(m.LParam);
ref readonly NativeMethods.RECT suggestedRect = ref m.GetLParamRef<NativeMethods.RECT>();
SuggestedRectangle = Rectangle.FromLTRB(suggestedRect.left, suggestedRect.top, suggestedRect.right, suggestedRect.bottom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ public void Update(bool timerCaused) {
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case Interop.WindowMessages.WM_NOTIFY:
NativeMethods.NMHDR nmhdr = (NativeMethods.NMHDR)m.GetLParam(typeof(NativeMethods.NMHDR));
ref readonly NativeMethods.NMHDR nmhdr = ref m.GetLParamRef<NativeMethods.NMHDR>();
if (nmhdr.code == NativeMethods.TTN_SHOW || nmhdr.code == NativeMethods.TTN_POP)
{
OnToolTipVisibilityChanging(nmhdr.idFrom, nmhdr.code == NativeMethods.TTN_SHOW);
Expand Down
26 changes: 12 additions & 14 deletions src/System.Windows.Forms/src/System/Windows/Forms/ImageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,21 +627,19 @@ private void CopyBitmapData(BitmapData sourceData, BitmapData targetData) {
// do the actual copy
int offsetSrc = 0;
int offsetDest = 0;
unsafe {
for (int i = 0; i < targetData.Height; i++)
{
IntPtr srcPtr, destPtr;
if (IntPtr.Size == 4) {
srcPtr = new IntPtr(sourceData.Scan0.ToInt32() + offsetSrc);
destPtr = new IntPtr(targetData.Scan0.ToInt32() + offsetDest);
} else {
srcPtr = new IntPtr(sourceData.Scan0.ToInt64() + offsetSrc);
destPtr = new IntPtr(targetData.Scan0.ToInt64() + offsetDest);
}
UnsafeNativeMethods.CopyMemory(new HandleRef(this, destPtr), new HandleRef(this, srcPtr), Math.Abs(targetData.Stride));
offsetSrc += sourceData.Stride;
offsetDest += targetData.Stride;
for (int i = 0; i < targetData.Height; i++)
{
IntPtr srcPtr, destPtr;
if (IntPtr.Size == 4) {
srcPtr = new IntPtr(sourceData.Scan0.ToInt32() + offsetSrc);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntPtr has a operator +(IntPtr pointer, int offset) method.

destPtr = new IntPtr(targetData.Scan0.ToInt32() + offsetDest);
} else {
srcPtr = new IntPtr(sourceData.Scan0.ToInt64() + offsetSrc);
destPtr = new IntPtr(targetData.Scan0.ToInt64() + offsetDest);
}
UnsafeNativeMethods.CopyMemory(new HandleRef(this, destPtr), new HandleRef(this, srcPtr), Math.Abs(targetData.Stride));
offsetSrc += sourceData.Stride;
offsetDest += targetData.Stride;
}
}

Expand Down
Loading