CComboBox::SetItemData,以及自绘我们的CComBox

来源:互联网 发布:手机淘宝加入购物车 编辑:程序博客网 时间:2024/06/05 08:39

1.这个函数有什么用呢?

我们如果自己绘制 控件的时候呢,我们可能会比如一个CComboBox中,一个文字和一个图像的索引号保存起来,在我们需要使用的时候使用,这个的功能有点类似于

Android中我们SetTag的属性!我们建立一个结构体就可以把两样东西保存起来了,这样就可以收获一个简单的模式,非常的棒,获得自己想要的图片图片列表的索引!

我们可以修改他的AddString(text,UINT image)//这样我们继承修改的类,在外部使用起来非常的方便,随意的增加图像以及文字,在自绘显示出来就好了!

{

   int I=CComboxBox::AddString(szText);

SData* p=new SData;

p->nImage=nImage;

p->szText=text;

SetItemData(i,(DWORD)p);

}

Sets the 32-bit value associated with the specified item in a combo box.

 
int SetItemData(   int nIndex,   DWORD_PTR dwItemData );

Parameters

nIndex

Contains a zero-based index to the item to set.

dwItemData

Contains the new value to associate with the item.

这样我们在比如别的地方(CListCtrl)获取图片资源,或者为他的资源,就可以直接使用GetItemData(nIndex),使用我们列表中的数据以及我们的图像之内的东西了!

但是一定要注意,保存在我们的Data,一定要使用堆空间创造,最后的时候要记得销毁我们的资源

3.我们在DrawItem中获取图像的序号那么就简单了瑟

  如: SData* pdata=(SData*)GetItemData(intdex);

        ASSERT(pdata);

      return pData->nImage;图像的序号,在结构体中的~


4.我么如何自绘图标呢?

直接::DrawICon(dc.GetSaftHdc(0,rc.rect,rect.top.m_hIcon,16,16,0,0,DI_NOMAL);?

还是使用:CImaglist中的Draw或DrawEx?->Draw(&dc,nImage(序号),rc.topleft(),属性)

Draws an image list item in the specified device context.

 
BOOL DrawEx(   CDC* pDC,   int nImage,   POINT pt,   SIZE sz,   COLORREF clrBk,   COLORREF clrFg,   UINT nStyle );

Parameters

pDC

Pointer to the destination device context.

nImage

Zero-based index of the image to draw.

pt

Location at which to draw within the specified device context.

sz

Size of the portion of the image to draw relative to the upper-left corner of the image. Seedx anddy in ImageList_DrawEx in the Platform SDK.

clrBk

Background color of the image. See rgbBk in ImageList_DrawEx in the Platform SDK.

clrFg

Foreground color of the image. See rgbFg in ImageList_DrawEx in the Platform SDK.

nStyle

Flag specifying the drawing style. See fStyle in ImageList_DrawEx in thePlatform SDK.

Collapse imageReturn Value

Nonzero if successful; otherwise 0.

Collapse imageRemarks

The function uses the specified drawing style and blends the image with the specified color. 


ImageList_DrawEx Function


Draws an image list item in the specified device context. The function uses the specified drawing style and blends the image with the specified color.

Syntax

BOOL ImageList_DrawEx(          HIMAGELIST himl,    int i,    HDC hdcDst,    int x,    int y,    int dx,    int dy,    COLORREF rgbBk,    COLORREF rgbFg,    UINT fStyle);

Parameters

himl
A handle to the image list
i
The index of the image to draw.
hdcDst
A handle to the destination device context.
x
The x-coordinate at which to draw within the specified device context.
y
The y-coordinate at which to draw within the specified device context.
dx
The width of the portion of the image to draw relative to the upper-left corner of the image. Ifdx anddy are zero, the function draws the entire image. The function does not ensure that the parameters are valid.
dy
The height of the portion of the image to draw, relative to the upper-left corner of the image. Ifdx anddy are zero, the function draws the entire image. The function does not ensure that the parameters are valid.
rgbBk
The background color of the image. This parameter can be an application-defined RGB value or one of the following values:
CLR_NONE
No background color. The image is drawn transparently.
CLR_DEFAULT
The default background color. The image is drawn using the background color of the image list.
rgbFg
The foreground color of the image. This parameter can be an application-defined RGB value or one of the following values:
CLR_NONE
No blend color. The image is blended with the color of the destination device context.
CLR_DEFAULT
The default foreground color. The image is drawn using the system highlight color as the foreground color.
fStyle
The drawing style and, optionally, the overlay image. For information about specifying an overlay image index, see the comments section at the end of this topic. This parameter can be a combination of an overlay image index and one or more of the following values:
ILD_BLEND25,
ILD_FOCUS
Draws the image, blending 25 percent with the blend color specified by rgbFg. This value has no effect if the image list does not contain a mask.
ILD_BLEND50,
ILD_SELECTED,
ILD_BLEND
Draws the image, blending 50 percent with the blend color specified by rgbFg. This value has no effect if the image list does not contain a mask.
ILD_MASK
Draws the mask.
ILD_NORMAL
Draws the image using the background color for the image list. If the background color is the CLR_NONE value, the image is drawn transparently using the mask.
ILD_TRANSPARENT
Draws the image transparently using the mask, regardless of the background color. This value has no effect if the image list does not contain a mask.
ILD_IMAGE
If the overlay does not require a mask to be drawn set the ILD_IMAGE flag. This causesImageList_DrawEx to just draw the image, ignoring the mask.


我们根据下拉表的索引,绘制我们的每一项。  我们还可以修改每一项的高度值!可以通过重载虚函数,MeasureItem(),这样可以设置我们的每一项的高度之内的事情了!

The MEASUREITEMSTRUCT structure informs Windows of the dimensions of an owner-drawn control or menu item.

 
typedef struct tagMEASUREITEMSTRUCT {   UINT CtlType;   UINT CtlID;   UINT itemID;   UINT itemWidth;   UINT itemHeight;   DWORD itemData} MEASUREITEMSTRUCT;

Parameters

CtlType

Contains the control type. The values for control types are as follows:

  • ODT_COMBOBOX   Owner-draw combo box

  • ODT_LISTBOX   Owner-draw list box

  • ODT_MENU   Owner-draw menu

CtlID

Contains the control ID for a combo box, list box, or button. This member is not used for a menu.

itemID

Contains the menu-item ID for a menu or the list-box-item ID for a variable-height combo box or list box. This member is not used for a fixed-height combo box or list box, or for a button.

itemWidth

Specifies the width of a menu item. The owner of the owner-draw menu item must fill this member before it returns from the message.

itemHeight

Specifies the height of an individual item in a list box or a menu. Before it returns from the message, the owner of the owner-draw combo box, list box, or menu item must fill out this member. The maximum height of a list box item is 255.

itemData

For a combo box or list box, this member contains the value that was passed to the list box by one of the following:





0 0
原创粉丝点击