Click to See Complete Forum and Search --> : Dashed and Dotted Lines


wdhough
September 2nd, 2005, 07:06 AM
Hello all!

I am attempting to draw dashed and/ or dotted lines using a pen. I would like to be able to set a pen style, width and color and then select it into a DC. I can do this fine but the problem comes when i want to create a dashed/dotted pen with a width greater than 1. The pen comes out solid! Any help here would be great but please bear in mind it needs to be a pen that can be selected into a dc,

Many thanks


Will

Hobson
September 2nd, 2005, 07:12 AM
There is a lot of articles here at CG and at CodeProject about what you need. Just search / google for them. I. e. one of them could be found here: http://codeguru.com/Cpp/G-M/gdi/article.php/c153/

Hob

wdhough
September 2nd, 2005, 07:19 AM
I checked around at a few of the articles on here already and they do not have what i am looking for, As you probably read on that article that you gave me the link to, that approach draws lots of small lines to create an overall, dashed or dotted effect, as well as using two pens to do it! Thats no use to me.


Will

g_gili
September 2nd, 2005, 03:59 PM
I think you can't do it without any trick because PS_DOT and PS_DASH and some other styles can draw only when the pen width is 1 or less, in device units. For more info see MSDN.
here MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pens_9wha.asp)

andytim
September 5th, 2005, 03:02 AM
The following codes can generate pen style:



void GenerateLineStyle(int nPenWidth,int nPenStyle,DWORD *output, int &nSize)
{
DWORD dwDot[2]={1,3};
DWORD dwDash[2] = {2,4};
DWORD dwDashdot[4]={2,4,1,4};
DWORD dwDashdotdot[6] = {2,3,1,3,1,3};
int i;
switch (nPenStyle)
{
case PS_DASH:
for (i = 0; i < 2; i++)
{
if (dwDash[i] ==1)
{
output[i] = 1;
}
else
{
output[i] = dwDash[i] * nPenWidth;
}
}
nSize = 2;
break;

case PS_DOT:
for (i = 0; i < 2; i++)
{
if (dwDot[i] == 1)
{
output[i] = 1;
}
else
{
output[i] = dwDot[i] * nPenWidth;
}
}
nSize=2;
break;

case PS_DASHDOT:
for (i = 0; i < 4; i++)
{
if (dwDashdot[i] == 1)
{
output[i] = 1;
}
else
{
output[i] = dwDashdot[i] * nPenWidth;
}
}
nSize=4;
break;

case PS_DASHDOTDOT:
for (i = 0; i < 6; i++)
{
if(dwDashdotdot[i]==1)
{
output[i] = 1;
}
else
{
output[i] = dwDashdotdot[i] * nPenWidth;
}
}
nSize=6;
break;
}

}


Then

LOGBRUSH lb;
lb.lbStyle = BS_SOLID;
lb.lbColor = m_crLine;
lb.lbHatch = 0;
DWORD array[6] = {0,0,0,0,0,0};
int nArrayLen = 0;
CFODrawHelper::GenerateLineStyle(nWidth,m_nPenStyle,array,nArrayLen);

pPen->CreatePen(PS_GEOMETRIC | PS_USERSTYLE | PS_ENDCAP_SQUARE,
nWidth, &lb, nArrayLen,array);


For the screen shot about what this kind of line looks like,you can visit:
http://www.********.net/Products/Form2/EXDEntprise.htm

Hope it is useful to you.

Jack

wdhough
September 5th, 2005, 04:35 AM
Hi,

Thanks for your reply, i'm afraid i still cant get it to work. I needed to edit a little of the code, for example my width was a UINT and not int, but i dont think these changes would have affected the problem. Thanks for your help all the same, I get the impression that some one up there doesnt like me! This is all very frustrating though, because it can clearly be done, in any half decent image package, not to mention the drawing toolbar of microsoft word!

Thanks for your help


Will