快捷搜索:

Mobile开发(九宫格控件)

Mobile开拓小我感到应用九宫格的时刻并不多,但无意偶尔候又不得不应用,有点类似于手机开拓标志性的控件了,客户指明要用,咱们还能说啥。

惯例子,先上控件,控件竖屏时显示为九格,横屏时显示为八格,详细需求大年夜家可以在控件代码中自行变动,同样支持键盘和触摸操作。大年夜家读着代码再往下看。九宫格控件

着实,控件和代码便是自己的助手,根据自己开拓的实际环境来应用,不是我有什么我都给你加上。预测客户的真实意图,满意客户实际需求你的法度榜样就能交工了。废话不多说了,首先先容控件的一些要点:

在FlexStartMenu文件中,下面这行代码取消注释将显示九宫格边框,看小我喜爱

Code

//绘制九宫格边框

if (viewableItemCount == 9)

{

//横向

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), 0, 0, this.Width, 0);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), 0, this.Height / 3, this.Width, this.Height / 3);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), 0, this.Height * 2 / 3, this.Width, this.Height * 2 / 3);

//纵向

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), this.Width / 3, 0, this.Width / 3, this.Height);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), this.Width * 2 / 3, 0, this.Width * 2 / 3, this.Height);

}

else if (viewableItemCount == 8)

{

//横向

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), 0, 0, this.Width, 0);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), 0, this.Height / 2, this.Width, this.Height / 2);

//纵向

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), this.Width / 4, 0, this.Width / 4, this.Height);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), this.Width / 2, 0, this.Width / 2, this.Height);

gOffScreen.DrawLine(new Pen(SystemColors.InactiveCaption), this.Width * 3 / 4, 0, this.Width * 3 / 4, this.Height);

}

控件中可显示图片,翰墨,图片还分为选中前图片和选中后图片,节制代码如下:

Code

//选中时的图片

if (m_item.Press && m_item.PressIcon != null)

{

bmp = m_item.PressIcon;

selFont = new Font("宋体", 9, FontStyle.Bold);

}

else

{

//初始化图片

bmp = m_item.Icon;

selFont = new Font("宋体", 9, FontStyle.Regular);

}

//imgRect默认位置范围(绘制图片范围)

Rectangle imgRect = new Rectangle((m_item.Left), (m_item.Top+this.Height / rowsCount * 2 / 5-30), this.Width/cellsCount, this.Height / rowsCount);

//imgRect

if (bmp != null)

{

bmp.Draw(gOffScreen, imgRect);

}

//绘制翰墨

//gOffScreen.DrawString(m_item.ItemText, selFont, new SolidBrush(Color.White), (m_item.Left + 40 - gOffScreen.MeasureString(m_item.ItemText, selFont).Width / 2), (m_item.Top + this.Height / rowsCount * 2 / 5));

控件属性设置,绘制控件:

Code

protected override void OnPaint(PaintEventArgs e)

{

if (offBitmap != null)

{

AdjustFormLayout();

gxBuffer = Graphics.FromImage(offBitmap);

gxBuffer.Clear(this.BackColor);

this.BackColor = SystemColors.MenuText;

//

FlexMenu.SysColor = this.BackColor;

//

FlexMenu.ViewCount = viewCount;

//

FlexMenu.Location = new System.Drawing.Point(0, 0);

FlexMenu.TabIndex = 0;

//防止开启自动滚动条时屏幕越过报错

FlexMenu.Height = formSize.Height - 50;

FlexMenu.Width = formSize.Width;

e.Graphics.DrawImage(offBitmap, 0, 0);

}

else

{

e.Graphics.Clear(this.BackColor);

}

}

protected override void OnResize(EventArgs e)

{

base.OnResize(e);

if (offBitmap != null)

offBitmap.Dispose();

offBitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppRgb);

}

public void AdjustFormLayout()

{

Rectangle screenBounds = Screen.PrimaryScreen.Bounds;

float widthRatio = 0.0F;

float heightRatio = 0.0F;

DisplayManager.GetDisplayRatiosByDpi(this, ref widthRatio, ref heightRatio);

您可能还会对下面的文章感兴趣: