BOOL CreatePen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL );最后一个参数可以自定义画笔类型。
1)直线插补算法作为参考 //The DDA algorithm #define ROUND(a) ((int)a+0.5) void lineDDA(int xa, int ya,int xb,int yb,HDC dc) { int dx=xb-xa, dy=yb-ya; int steps,k; float xincrement ,yincrement,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincrement=dx/(float)steps; yincrement=dy/(float)steps; SetPixel(dc,ROUND(x),ROUND(y),RGB(255,0,0)); for(k=0;k<steps;k++) { x+=xincrement; y+=yincrement; SetPixel(dc,ROUND(x),ROUND(y),RGB(255,0,0)); } } //the Bresenham algorithm void lineBres(int xa,int ya,int xb,int yb,HDC dc) { int dx=abs(xa-xb); int dy=abs(ya-yb); int p=2*dy-dx; int twody=2*dy; int twodydx = 2*(dy-dx); int x,y,xend; if(xa>xb) { x=xb; y=yb; xend=xa; } else { x=xa;y=ya; xend=xb; } SetPixel(dc,(x),(y),RGB(255,0,0)); while(x<xend) { x++; if(p<0) p+=twody; else { y++; p+=twodydx; } SetPixel(dc,(x),(y),RGB(255,0,0)); }} 2)可以直接操作dc试试看
ExtCreatePen The ExtCreatePen function creates a logical cosmetic or geometric pen that has the specified style, width, and brush attributes. HPEN ExtCreatePen( DWORD dwPenStyle, // pen style DWORD dwWidth, // pen width CONST LOGBRUSH *lplb, // pointer to structure for brush attributes DWORD dwStyleCount, // length of array containing custom style bits CONST DWORD *lpStyle // optional array of custom style bits );
Parameters dwPenStyle Specifies a combination of type, style, end cap, and join attributes. The values from each category are combined by using the bitwise OR operator (|). The pen type can be one of the following values: Type Description PS_GEOMETRIC Pen is geometric. PS_COSMETIC Pen is cosmetic. The pen style can be any one of the following values: Style Description PS_ALTERNATE Windows NT: Pen sets every other pixel. (This style is applicable only for cosmetic pens.) PS_SOLID Pen is solid. PS_DASH Pen is dashed. Windows 95 : This style is not supported for geometric lines.Windows 98: Not supported.
PS_DOT Pen is dotted. Windows 95 and Windows 98: This style is not supported for geometric lines.
PS_DASHDOT Pen has alternating dashes and dots. Windows 95: This style is not supported for geometric lines.Windows 98: Not supported.
PS_DASHDOTDOT Pen has alternating dashes and double dots. Windows 95: This style is not supported for geometric lines.Windows 98: Not supported.
PS_NULL Pen is invisible. PS_USERSTYLE Windows NT: Pen uses a styling array supplied by the user. PS_INSIDEFRAME Pen is solid. When this pen is used in any graphics device interface (GDI) drawing function that takes a bounding rectangle, the dimensions of the figure are shrunk so that it fits entirely in the bounding rectangle, taking into account the width of the pen. This applies only to geometric pens. The end cap is only specified for geometric pens. The end cap can be one of the following values: End cap Description PS_ENDCAP_ROUND End caps are round. PS_ENDCAP_SQUARE End caps are square. PS_ENDCAP_FLAT End caps are flat. The join is only specified for geometric pens. The join can be one of the following values: Line join Description PS_JOIN_BEVEL Joins are beveled. PS_JOIN_MITER Joins are mitered when they are within the current limit set by the SetMiterLimit function. If it exceeds this limit, the join is beveled. PS_JOIN_ROUND Joins are round. Windows 95 and Windows 98: The PS_ENDCAP_ROUND, PS_ENDCAP_SQUARE, PS_ENDCAP_FLAT, PS_JOIN_BEVEL, PS_JOIN_MITER, and PS_JOIN_ROUND styles are supported only for geometric pens when used to draw paths. dwWidth Specifies the width of the pen. If the dwPenStyle parameter is PS_GEOMETRIC, the width is given in logical units. If dwPenStyle is PS_COSMETIC, the width must be set to 1. lplb Points to a LOGBRUSH structure. If dwPenStyle is PS_COSMETIC, the lbColor member specifies the color of the pen and the lbStyle member must be set to BS_SOLID. If dwPenStyle is PS_GEOMETRIC, all members must be used to specify the brush attributes of the pen. dwStyleCount Specifies the length, in DWORD units, of the lpStyle array. This value must be zero if dwPenStyle is not PS_USERSTYLE. lpStyle Points to an array of DWORD values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL if dwPenStyle is not PS_USERSTYLE. Return Values If the function succeeds, the return value is a handle that identifies a logical pen.If the function fails, the return value is zero. Windows NT: To get extended error information, callGetLastError.Res A geometric pen can have any width and can have any of the attributes of a brush, such as dithers and patterns. A cosmetic pen can only be a single pixel wide and must be a solid color, but cosmetic pens are generally faster than geometric pens. The width of a geometric pen is always specified in world units. The width of a cosmetic pen is always 1. End cap and join are only specified for geometric pens. After an application creates a logical pen, it can select that pen into a device context by calling the SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves. If dwPenStyle is PS_COSMETIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in style units. A style unit is defined by the device where the pen is used to draw a line. If dwPenStyle is PS_GEOMETRIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in logical units. If dwPenStyle is PS_ALTERNATE, the style unit is ignored and every other pixel is set. If the lbStyle member of the LOGBRUSH structure pointed to by lplb is BS_PATTERN, the bitmap pointed to by the lbHatch member of that structure cannot be a dib section. A dib section is a bitmap created by CreateDibSection. If that bitmap is a dib section, the ExtCreatePen function fails.When an application no longer requires a specified pen, it should call the DeleteObject function to delete the pen. ICM: No color management is done at pen creation. However, color management will be performed when the pen is selected into an ICM-enabled device context. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in wingdi.h. Import Library: Use gdi32.lib.See Also Pens Overview, Pen Functions, CreatePen, CreateDibSection, CreatePenIndirect, DeleteObject, GetObject, LOGBRUSH, SelectObject, SetMiterLimit
To jennyvenus(脑白金,你再不倒闭对不起人民对不起党啊): 我知道可以自己写,但是SetPixel的效率太低了 To Rigel(猎户座-参宿七)、bojinyu(沙鱼): ExtCreatePen没用过,能不能写个例子 To 8(): C/C++语法早就学了 VC的SDK编程也比较熟悉 就是MFC没学(对我而言,现在弄懂原理最重要)
CreateDIBSection()后直接操作像素?
我程序要考虑1、2、4、8、16、24、32位六种DIBSection的格式自己写太麻烦了
up --------------------------------------ExtCreatePen没用过,谁能写个例子
up --------------------------------------ExtCreatePen没用过,谁能写个例子
//The DDA algorithm
#define ROUND(a) ((int)a+0.5)
void lineDDA(int xa, int ya,int xb,int yb,HDC dc)
{
int dx=xb-xa, dy=yb-ya;
int steps,k;
float xincrement ,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
SetPixel(dc,ROUND(x),ROUND(y),RGB(255,0,0));
for(k=0;k<steps;k++)
{
x+=xincrement;
y+=yincrement;
SetPixel(dc,ROUND(x),ROUND(y),RGB(255,0,0));
}
}
//the Bresenham algorithm
void lineBres(int xa,int ya,int xb,int yb,HDC dc)
{
int dx=abs(xa-xb);
int dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy;
int twodydx = 2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb; y=yb;
xend=xa; }
else
{
x=xa;y=ya;
xend=xb;
}
SetPixel(dc,(x),(y),RGB(255,0,0));
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p+=twodydx;
}
SetPixel(dc,(x),(y),RGB(255,0,0));
}}
2)可以直接操作dc试试看
The ExtCreatePen function creates a logical cosmetic or geometric pen that has the specified style, width, and brush attributes. HPEN ExtCreatePen(
DWORD dwPenStyle, // pen style
DWORD dwWidth, // pen width
CONST LOGBRUSH *lplb, // pointer to structure for brush attributes
DWORD dwStyleCount, // length of array containing custom style bits
CONST DWORD *lpStyle // optional array of custom style bits
);
Parameters
dwPenStyle
Specifies a combination of type, style, end cap, and join attributes. The values from each category are combined by using the bitwise OR operator (|).
The pen type can be one of the following values: Type Description
PS_GEOMETRIC Pen is geometric.
PS_COSMETIC Pen is cosmetic. The pen style can be any one of the following values: Style Description
PS_ALTERNATE Windows NT: Pen sets every other pixel. (This style is applicable only for cosmetic pens.)
PS_SOLID Pen is solid.
PS_DASH Pen is dashed.
Windows 95 : This style is not supported for geometric lines.Windows 98: Not supported.
PS_DOT Pen is dotted.
Windows 95 and Windows 98: This style is not supported for geometric lines.
PS_DASHDOT Pen has alternating dashes and dots.
Windows 95: This style is not supported for geometric lines.Windows 98: Not supported.
PS_DASHDOTDOT Pen has alternating dashes and double dots.
Windows 95: This style is not supported for geometric lines.Windows 98: Not supported.
PS_NULL Pen is invisible.
PS_USERSTYLE Windows NT: Pen uses a styling array supplied by the user.
PS_INSIDEFRAME Pen is solid. When this pen is used in any graphics device interface (GDI) drawing function that takes a bounding rectangle, the dimensions of the figure are shrunk so that it fits entirely in the bounding rectangle, taking into account the width of the pen. This applies only to geometric pens. The end cap is only specified for geometric pens. The end cap can be one of the following values: End cap Description
PS_ENDCAP_ROUND End caps are round.
PS_ENDCAP_SQUARE End caps are square.
PS_ENDCAP_FLAT End caps are flat. The join is only specified for geometric pens. The join can be one of the following values: Line join Description
PS_JOIN_BEVEL Joins are beveled.
PS_JOIN_MITER Joins are mitered when they are within the current limit set by the SetMiterLimit function. If it exceeds this limit, the join is beveled.
PS_JOIN_ROUND Joins are round. Windows 95 and Windows 98: The PS_ENDCAP_ROUND, PS_ENDCAP_SQUARE, PS_ENDCAP_FLAT, PS_JOIN_BEVEL, PS_JOIN_MITER, and PS_JOIN_ROUND styles are supported only for geometric pens when used to draw paths. dwWidth
Specifies the width of the pen. If the dwPenStyle parameter is PS_GEOMETRIC, the width is given in logical units. If dwPenStyle is PS_COSMETIC, the width must be set to 1.
lplb
Points to a LOGBRUSH structure. If dwPenStyle is PS_COSMETIC, the lbColor member specifies the color of the pen and the lbStyle member must be set to BS_SOLID. If dwPenStyle is PS_GEOMETRIC, all members must be used to specify the brush attributes of the pen.
dwStyleCount
Specifies the length, in DWORD units, of the lpStyle array. This value must be zero if dwPenStyle is not PS_USERSTYLE.
lpStyle
Points to an array of DWORD values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL if dwPenStyle is not PS_USERSTYLE.
Return Values
If the function succeeds, the return value is a handle that identifies a logical pen.If the function fails, the return value is zero. Windows NT: To get extended error information, callGetLastError.Res
A geometric pen can have any width and can have any of the attributes of a brush, such as dithers and patterns. A cosmetic pen can only be a single pixel wide and must be a solid color, but cosmetic pens are generally faster than geometric pens. The width of a geometric pen is always specified in world units. The width of a cosmetic pen is always 1. End cap and join are only specified for geometric pens. After an application creates a logical pen, it can select that pen into a device context by calling the SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves. If dwPenStyle is PS_COSMETIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in style units. A style unit is defined by the device where the pen is used to draw a line. If dwPenStyle is PS_GEOMETRIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in logical units. If dwPenStyle is PS_ALTERNATE, the style unit is ignored and every other pixel is set. If the lbStyle member of the LOGBRUSH structure pointed to by lplb is BS_PATTERN, the bitmap pointed to by the lbHatch member of that structure cannot be a dib section. A dib section is a bitmap created by CreateDibSection. If that bitmap is a dib section, the ExtCreatePen function fails.When an application no longer requires a specified pen, it should call the DeleteObject function to delete the pen. ICM: No color management is done at pen creation. However, color management will be performed when the pen is selected into an ICM-enabled device context. QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in wingdi.h.
Import Library: Use gdi32.lib.See Also
Pens Overview, Pen Functions, CreatePen, CreateDibSection, CreatePenIndirect, DeleteObject, GetObject, LOGBRUSH, SelectObject, SetMiterLimit
我知道可以自己写,但是SetPixel的效率太低了
To Rigel(猎户座-参宿七)、bojinyu(沙鱼):
ExtCreatePen没用过,能不能写个例子
To 8():
C/C++语法早就学了
VC的SDK编程也比较熟悉
就是MFC没学(对我而言,现在弄懂原理最重要)
--------------------------------------ExtCreatePen没用过,谁能写个例子
--------------------------------------ExtCreatePen没用过,谁能写个例子
EXTcreatepen是GDI绘图函数,生成的是GDI对象,就像使用一般的HPEN一样就可以了。类型用PS_DOT试试看吧。SetPixel这个函数只有偶尔用用,速度太慢了,GETPIXEL速度好像更慢,不过对于DDB来说只有这个办法了。