“-="的陷阱

1.有如下代码(显示下拉框时,屏幕最大化右键菜单的显示坐标处理):

起初:

Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
//获取在屏幕的坐标
Point screenPoint = this.ListTree.PointToScreen(e.Location);
Point cmsPoint = new Point(e.Location.X + 5, e.Location.Y + 5);    
if (screenPoint.X + this._itemContextMenuStrip.Width > rect.Width)
{//超过屏幕了,反向显示
    cmsPoint.X -= this._itemContextMenuStrip.Width;
}

  

发现需要向左移动一点,故初改如下:

if (screenPoint.X + this._itemContextMenuStrip.Width > rect.Width)
{//超过屏幕了,反向显示
    cmsPoint.X -= this._itemContextMenuStrip.Width - 5;
}

发现反而往右移动了- -,”-=”操作符的陷阱啊,一不注意就掉进去了,需要加上括号。

The End.

  

原文地址:https://www.cnblogs.com/wishFreedom/p/3683813.html