当前位置:编程学习 > wap >>

mobile listview 九宫格

那位大侠教教我 mobile 中的listview怎么用   最好有个教程
我想做个9宫格,查资料时候还看见 listctrl  这个是什么空间 我怎么没在vs 中找到?
还有那位大家 接着教教我  如何做9宫格     --------------------编程问答-------------------- 如果是VC的话,重载CListViewCtrl控件。 --------------------编程问答-------------------- 如果是WTL工程的话,用下边的代码加图标和处理消息。

#define ID_LIST 99

BEGIN_MSG_MAP(XXX_wce_main)
    NOTIFY_HANDLER(ID_LIST,NM_CLICK, OnListClick)
    CHAIN_MSG_MAP(CAppStdDialogImpl<sync_wce_main>)
END_MSG_MAP()


LRESULT OnListClick(int idCtrl, LPNMHDR pnmh, BOOL& bHandled){
    int rewr=1;
    return 0;
};


以下是建立控件
m_list.Create(m_hWnd,CRect(left,top,200,100),NET263_WCE_STR_TITLE,WS_CHILD|WS_VISIBLE|LVS_ICON,0,ID_LIST);

   CImageList pImageList;
pImageList.CreateFromImage (IDB_BITMAP1, 32, 32, CLR_NONE, IMAGE_BITMAP,0 );

HBITMAP hBitmap;
CBitmap cBmp1;

hBitmap=cBmp1.LoadBitmap(IDB_BITMAP2);
pImageList.Add(hBitmap, RGB(255,0, 255));
cBmp1.DeleteObject();

hBitmap=cBmp1.LoadBitmap(IDB_BITMAP3);
pImageList.Add(hBitmap, RGB(255,0, 255));
cBmp1.DeleteObject();

hBitmap=cBmp1.LoadBitmap(IDB_BITMAP4);
pImageList.Add(hBitmap, RGB(255,0, 255));
cBmp1.DeleteObject();

hBitmap=cBmp1.LoadBitmap(IDB_BITMAP5);
pImageList.Add(hBitmap, RGB(255,0, 255));
cBmp1.DeleteObject();

hBitmap=cBmp1.LoadBitmap(IDB_BITMAP6);
pImageList.Add(hBitmap, RGB(255,0, 255));
cBmp1.DeleteObject();

m_list.SetImageList(pImageList, LVSIL_NORMAL);

wstring titles[6];

titles[0]=L"XXXXX_0";
titles[1]=L"XXXXX_1";
titles[2]=L"XXXXX_2";
titles[3]=L"XXXXX_3";
titles[4]=L"XXXXX_4";
titles[5]=L"XXXXX_5";

int count=sizeof(titles)/sizeof(titles[0]);

for(int i=0;i<count;i++)

   int iItem=m_list.InsertItem(i,titles[i].c_str(),i);
   DWORD *data=new DWORD;
   memset(data,0,sizeof(DWORD));
   memcpy(data,&i,sizeof(DWORD));
   m_list.SetItemData( iItem, reinterpret_cast<DWORD>( data ) );
}
--------------------编程问答-------------------- 我用的是c# 
忘说了   --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Collections;

namespace ScreenItemMenu
{
    public partial class ScreenItemMenu : UserControl
    {
        public ScreenItemMenu()
        {
            this.Items = new ScreenItemCollections();
        }

        public ScreenItemCollections Items
        {
            get;
            set;
        }
        /// <summary>
        /// 被选中的项
        /// </summary>
        public ScreenItem SelectedItem
        {
            get
            {
                ScreenItem m_item = null;
                for (int i = 0; i < this.Items.Count(); i++)
                {
                    if (Items[i].Press == true)
                    {
                        m_item = Items[i];
                        break;
                    }
                }
                return m_item;
            }
        }
        //定义一个Bitmap变量用双缓冲的方式防止闪烁
        protected Bitmap backBuffer;
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (backBuffer != null)
            {
                Graphics gOffScreen = Graphics.FromImage(backBuffer);
                gOffScreen.Clear(this.BackColor);
                for (int i = 0; i < this.Items.Count(); i++)
                {
                    DrawItem(this.Items[i], i, gOffScreen);
                    
                }
                e.Graphics.DrawImage(backBuffer, 0, 0);
            }
            else
                e.Graphics.Clear(this.BackColor);
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (backBuffer != null)
                backBuffer.Dispose();
            backBuffer = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppRgb);
        }
        // 刘卫修改 2010-06-21
        private void DrawItem(ScreenItem m_item, int index, Graphics gOffScreen)
        {
            m_item.Index = index + 1;
            TopAndLeft(m_item);
            Bitmap bmp = null;
            //选中时的图片
            if (m_item.Press && m_item.PressIcon != null)
                bmp = new Bitmap(m_item.PressIcon);
            else
                bmp = new Bitmap(m_item.Icon);
            ImageAttributes ia = new ImageAttributes();
            ia.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
            Rectangle imgRect = new Rectangle(m_item.Left * 11 / 12 + this.Width / 12, m_item.Top/* * 11 / 12 + this.Height / 12*/, bmp.Width, bmp.Height);
            int iLeft = m_item.Left * 11 / 12 + this.Width / 12;
            int iTop  = m_item.Top * 11 / 12 + this.Height / 12;
            //Rectangle imgRect = new Rectangle( iLeft, iTop - 15, bmp.Width, bmp.Height);
            gOffScreen.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
            System.Drawing.Font drawfont = this.Font;
            System.Drawing.Brush drawbrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
            imgRect.Offset(0, bmp.Height);
            imgRect.X = m_item.Left;
            imgRect.Width = this.Width / 3;
            imgRect.Height = (this.Height / 3 - bmp.Height) / 2;
            System.Drawing.StringFormat drawformat = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoWrap);
            drawformat.Alignment = StringAlignment.Center;
            drawformat.LineAlignment = StringAlignment.Near;
            gOffScreen.DrawString(m_item.ItemText, drawfont, drawbrush, imgRect,drawformat);
            drawbrush.Dispose();
            bmp.Dispose();
        }
        private void DynamicIcon()
        {
            Timer m_timer = new Timer();
            m_timer.Interval = 1000;
            m_timer.Tick += new EventHandler(ChangeIcon);
        }
        private void ChangeIcon(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 捕获点击动作
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            for (int i = 0; i < this.Items.Count(); i++)
            {
                Rectangle itemRec = new Rectangle(this.Items[i].Left, this.Items[i].Top, this.Width / 3, this.Height / 3);
                if (itemRec.Contains(e.X, e.Y))
                {
                    this.Items[i].Press = true;
                    for (int j = 0; j < this.Items.Count(); j++)
                    {
                        if (j != i)
                        {
                            this.Items[j].Press = false;
                        }
                    }
                    this.Invalidate();
                    break;
                }
            }
            base.OnMouseDown(e);
        }
        /// <summary>
        /// 计算九宫格的长宽高
        /// </summary>
        /// <param name="m_item"></param>
        private void TopAndLeft(ScreenItem m_item)
        {
            if ((double)m_item.Index / 3 > 2)
                m_item.Top = this.Height * 2 / 3;
            else if ((double)m_item.Index / 3 > 1)
                m_item.Top = this.Height / 3;
            else
                m_item.Top = 0;
            if (m_item.Index % 3 == 0 && m_item.Index != 1)
                m_item.Left = this.Width * 2 / 3;
            else if (m_item.Index == 2 || m_item.Index == 5 || m_item.Index == 8)
                m_item.Left = this.Width / 3;
            else
                m_item.Left = 0;
        } --------------------编程问答--------------------  protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            // base.OnPaintBackground(e);
            //e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
        }
        /// <summary>
        /// 改变按键移动时选项的选中情况
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (SelectedItem != null)
            {
                int index = SelectedItem.Index - 1;
                switch (e.KeyCode)
                {
                    case Keys.Down:
                        if (SelectedItem.Index > 6)
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index - 6].Press = true;
                            this.Invalidate();
                        }
                        else
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index + 3].Press = true;
                            this.Invalidate();
                        }
                        break;
                    case Keys.Up:
                        if (SelectedItem.Index <= 3)
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index + 6].Press = true;
                            this.Invalidate();
                        }
                        else
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index - 3].Press = true;
                            this.Invalidate();
                        }
                        break;
                    case Keys.Left:
                        if (SelectedItem.Index == 1)
                        {
                            this.SelectedItem.Press = false;
                            this.Items[8].Press = true;
                            this.Invalidate();
                        }
                        else
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index - 1].Press = true;
                            this.Invalidate();
                        }
                        break;
                    case Keys.Right:
                        if (SelectedItem.Index == 9)
                        {
                            this.SelectedItem.Press = false;
                            this.Items[0].Press = true;
                            this.Invalidate();
                        }
                        else
                        {
                            this.SelectedItem.Press = false;
                            this.Items[index + 1].Press = true;
                            this.Invalidate();
                        }
                        break;
                    case Keys.Enter:
                        this.OnClick(e);
                        break;
                }
                ////以上只针对九个图标的,下面针对两个活动图标
                //if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                //{
                //    if (SelectedItem.Index == 1)
                //    {
                //        this.SelectedItem.Press = false;
                //        this.Items[1].Press = true;

                //    }
                //    else
                //    {
                //        this.SelectedItem.Press = false;
                //        this.Items[0].Press = true;
                //    }
                //    this.Invalidate();
                //}
                //else if (e.KeyCode == Keys.Enter)
                //{
                //    this.OnClick(e);
                //}
            }
            else
            {
                this.Items[0].Press = true;
                this.Invalidate();
            }
            base.OnKeyDown(e);
        }
    }

    public class ScreenItemCollections : CollectionBase
    {
        public void Remove(ScreenItem value)
        {
            List.Remove(value);
        }
        public bool Contains(ScreenItem value)
        {
            return (List.Contains(value));
        }
        public void Insert(int index, ScreenItem value)
        {
            List.Insert(index, value);
        }
        public int IndexOf(ScreenItem value)
        {
            return (List.IndexOf(value));
        }
        public int Add(ScreenItem value)
        {
            return (List.Add(value));
        }
        public ScreenItem this[int index]
        {
            get { return (ScreenItem)List[index]; }
            set { List[index] = value; }

        }
        
        internal int Count()
        {
            return base.Count;
        }
        
    }
    public class ScreenItem
    {
        /// <summary>
        /// 显示文本
        /// </summary>
        public string ItemText
        {
            get;
            set;
        }
        /// <summary>
        /// 选项图片
        /// </summary>
        public Image Icon
        {
            get;
            set;
        }
        /// <summary>
        /// 选中时的图片
        /// </summary>
        public Image PressIcon
        {
            get;
            set;
        }
        /// <summary>
        /// 选项距离顶部的距离
        /// </summary>
        public int Top
        {
            get;
            set;
        }
        /// <summary>
        /// 选项距离左边栏的距离
        /// </summary>
        public int Left
        {
            get;
            set;
        }
        /// <summary>
        /// 选项编号
        /// </summary>
        public int Index
        {
            get;
            set;
        }
        /// <summary>
        /// 是否被选中
        /// </summary>
        public bool Press
        {
            get;
            set;
        }
    }
} --------------------编程问答-------------------- 用得都是listview不过感觉有点死板不怎么好看,如果要好看的很费劲! --------------------编程问答-------------------- 继承Control自己重绘一个九宫格 --------------------编程问答-------------------- 俺的九宫格直接是在窗口上绘制图标,处理鼠标和按键消息。

感觉用ListCtrl还不如自己画的灵活。 --------------------编程问答-------------------- 2L,你的头像是按桌面背景 --------------------编程问答--------------------
引用 9 楼 huhacn 的回复:
2L,你的头像是按桌面背景


按=俺? 害我研究了好半天才明白你这句话是怎么回事,鄙视打错别字的。 --------------------编程问答-------------------- 谢谢各位大侠收藏了 --------------------编程问答-------------------- 学习了.c#的收藏
补充:移动开发 ,  Windows Phone
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,