public AlphabetRenderGroup(Size size) { this.Size = size; this.renderList = new List <AlphabetRender>(); this.renderDict = new Dictionary <string, AlphabetRender>(); string text = "A"; //添加26个英文首字母 for (int i = 0; i < 26; i++) { render = new AlphabetRender(); render.Enabled = false; render.Text = text; this.AddRender(render); this.renderList.Add(render); if (!this.renderDict.ContainsKey(render.Text)) { this.renderDict.Add(render.Text, render); } text = Convert.ToChar(Convert.ToInt16(text.ToCharArray()[0]) + 1).ToString(); } //添加其他 text = "OTHER"; render = new AlphabetRender(); render.Enabled = false; render.Text = text; render.Size = new Size(40, 24); this.AddRender(render); this.renderList.Add(render); if (!this.renderDict.ContainsKey(render.Text)) { this.renderDict.Add(render.Text, render); } Relocation(); }
private void Relocation() { AlphabetRender pre = null; if (renderList != null && renderList.Count != 0) { // 确定默认的绘制内容的纵坐标 int x = 10; int y = (this.Size.Height - renderList[0].Size.Height) / 2; int width = 0; int height = ConstHeight; // 遍历需要绘制的内容 foreach (AlphabetRender render in renderList) { if (pre == null) { // 如果是第一个内容 render.Location = new Point(this.Location.X + x, this.Location.Y + y); } else { // 根据前一个内容的位置信息,确定当前显示内容的坐标位置 render.Location = new Point(pre.Location.X + pre.Size.Width, this.Location.Y + y); } // 记录前一个绘制内容 pre = render; if (pre.Location.X + pre.Size.Width > this.Size.Width - pre.Size.Width / 2) { width = 0; pre = null; y += ConstHeight; height += ConstHeight; } else { // 累加当前内容的绘制占用总宽度 width += pre.Size.Width; } } // 更新当前容器的大小 // this.Size = new Size(width, this.Size.Height); this.Size = new Size(this.Size.Width, height); } this.Invalidate(); }