结构体数组的初始化

XStack 2008-05-27 10:40:02
我程序中有个问题要请教大家一下。
void ColorSchemesPane::FillPreviewControl ( void )
{
for ( sal_Int32 nIndex = 0; nIndex < 6; nIndex++ )
{
mpVSColorSchemesStyles->InsertItem( static_cast<USHORT>(nIndex+1), Image( CreateStylePreview( myColor )) );
}
}
在CreateStylePreview函数里,我要画一些图,是一些矩形,线条之类,但是我需要动态的颜色值,因为我要画6张图,所以必须将其颜色值作的参数传进去,其函数里共需6种颜色,所以我想到用数组的方式,并定义一个结构体,
struct BKColor
{
Color BmpBKColor;
Color penBKColor;
Color aRectBKColor;
Color aaRectBKColor;
Color aaaRectBKColor;
Color aaaaRectBKColor;

BKColor myColor[6];
可是我不知道该怎样去初始化这个结构。Color的构造函数为:Color( UINT8 nRed, UINT8 nGreen, UINT8 nBlue )用RGB三色值来构造。
CreateStylePreview函数的具体代码:
const Bitmap CreateStylePreview()
{
// initialize the size of bmp
Bitmap aPreviewBmp( Size(70,56), 24,NULL );
BitmapWriteAccess* pAccess = aPreviewBmp.AcquireWriteAccess();

if ( pAccess )
{
// initialize the bkcolor of bmp
pAccess->Erase( Color(COL_LIGHTCYAN) );
}


// draw the Pentagon
Point aPentagon[5];
aPentagon[0] = Point( 5, 33 );
aPentagon[1] = Point( 16, 33 );
aPentagon[2] = Point( 22, 39);
aPentagon[3] = Point( 16, 45 );
aPentagon[4] = Point( 5, 45 );

const Polygon aPenPoly( 5, aPentagon );
pAccess->SetFillColor( Color(COL_BLACK) );
pAccess->FillPolygon( aPenPoly );

// draw the histogram
const Rectangle aRect( 30, 40, 37, 53 );
pAccess->SetFillColor( Color(COL_LIGHTBLUE) );
pAccess->FillRect( aRect );

const Rectangle aaRect( 38, 30, 44, 53 );
pAccess->SetFillColor( Color(COL_GREEN) );
pAccess->FillRect( aaRect );

const Rectangle aaaRect( 45, 35, 51, 53 );
pAccess->SetFillColor( Color(COL_LIGHTRED) );
pAccess->FillRect( aaaRect );

const Rectangle aaaaRect( 52, 42, 58, 53 );
pAccess->SetFillColor( Color(COL_GREEN) );
pAccess->FillRect( aaaaRect );

// draw the line of the graph bottom and left side.
const Point aLPoint( 30, 53 );
const Point aRPoint( 62, 53 );
const Point aTPoint( 30, 29 );
const Point aBPoint( 30, 53 );
pAccess->SetLineColor( Color(COL_BLACK) );
pAccess->DrawLine( aLPoint, aRPoint );
pAccess->DrawLine( aTPoint, aBPoint );

aPreviewBmp.ReleaseAccess( pAccess );

return aPreviewBmp;
}

大家谁能来帮帮忙啊,给点建议!
...全文
221 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
XStack 2008-05-28
  • 打赏
  • 举报
回复
我这张图片需要6种颜色,因为上边有一个五边形,四个矩形,还要初始化图片的背景色。而我又需要6张图片,是这样一种情况。
XStack 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jiazhen 的回复:]
你说的动态颜色值,是指随机生成的颜色值,还是你按照自己的要求而指定呢?
这里给个随机生成的示例:

C/C++ codesrand( (unsigned)time(NULL) );

int nRed, nGreen, nBlue;
for(int i=0; i < 6; i++){
nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].BmpBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 …
[/Quote]
不,颜色是自己给定的。
XStack 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arong1234 的回复:]
给结构定义一个缺省构造函数,他不就自动初始化了,要把复杂问题简单花,减少殚精竭虑的可能性
[/Quote]
可问题的关键就是这个构造函数应该怎么写啊。
  • 打赏
  • 举报
回复
你说的动态颜色值,是指随机生成的颜色值,还是你按照自己的要求而指定呢?
这里给个随机生成的示例:
srand( (unsigned)time(NULL) );

int nRed, nGreen, nBlue;
for(int i=0; i < 6; i++){
nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].BmpBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].penBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].aRectBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].aaRectBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].aaaRectBKColor = Color( nRed, nGreen, nBlue );

nRed = (UINT8 *)( rend() ) % 255;
nGreen = (UINT8 *)( rend() ) % 255;
nBlue = (UINT8 *)( rend() ) % 255;
myColor[i].aaaaRectBKColor = Color( nRed, nGreen, nBlue );
}
  • 打赏
  • 举报
回复
struct BKColor 
{
Color BmpBKColor;
Color penBKColor;
Color aRectBKColor;
Color aaRectBKColor;
Color aaaRectBKColor;
Color aaaaRectBKColor;
}BKColor myColor[6];

void ColorSchemesPane::FillPreviewControl ( void )
{
for ( sal_Int32 nIndex = 0; nIndex < 6; nIndex++ ){
mpVSColorSchemesStyles->InsertItem( static_cast <USHORT>(nIndex+1), Image( CreateStylePreview( myColor )) );
}
}



const Bitmap CreateStylePreview( BKColor myCol)
{
// initialize the size of bmp
Bitmap aPreviewBmp( Size(70,56), 24,NULL );
BitmapWriteAccess* pAccess = aPreviewBmp.AcquireWriteAccess();

if ( pAccess ){
// initialize the bkcolor of bmp
pAccess->Erase( Color(COL_LIGHTCYAN) );
}


// draw the Pentagon
Point aPentagon[5];
aPentagon[0] = Point( 5, 33 );
aPentagon[1] = Point( 16, 33 );
aPentagon[2] = Point( 22, 39);
aPentagon[3] = Point( 16, 45 );
aPentagon[4] = Point( 5, 45 );

const Polygon aPenPoly( 5, aPentagon );
pAccess->SetFillColor( Color(COL_BLACK) );
pAccess->FillPolygon( aPenPoly );

// draw the histogram
const Rectangle aRect( 30, 40, 37, 53 );
pAccess->SetFillColor( Color(COL_LIGHTBLUE) );
pAccess->FillRect( myCol.aRect );

const Rectangle aaRect( 38, 30, 44, 53 );
pAccess->SetFillColor( Color(COL_GREEN) );
pAccess->FillRect( myCol.aaRect );

const Rectangle aaaRect( 45, 35, 51, 53 );
pAccess->SetFillColor( Color(COL_LIGHTRED) );
pAccess->FillRect( myCol.aaaRect );

const Rectangle aaaaRect( 52, 42, 58, 53 );
pAccess->SetFillColor( Color(COL_GREEN) );
pAccess->FillRect( myCol.aaaaRect );

// draw the line of the graph bottom and left side.
const Point aLPoint( 30, 53 );
const Point aRPoint( 62, 53 );
const Point aTPoint( 30, 29 );
const Point aBPoint( 30, 53 );
pAccess->SetLineColor( Color(COL_BLACK) );
pAccess->DrawLine( aLPoint, aRPoint );
pAccess->DrawLine( aTPoint, aBPoint );

aPreviewBmp.ReleaseAccess( pAccess );

return aPreviewBmp;
}
arong1234 2008-05-27
  • 打赏
  • 举报
回复
给结构定义一个缺省构造函数,他不就自动初始化了,要把复杂问题简单花,减少殚精竭虑的可能性

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧