C/Visual C - Dibujar Flechas

 
Vista:

Dibujar Flechas

Publicado por Rocío (3 intervenciones) el 04/01/2007 09:07:28
Hola. Estoy utilizando Microsoft Visual C++ y no sé cómo dibujar una línea en pantalla terminada en flecha. Si alguien me puede ayudar se lo agradezco mucho.
Para dibujar la línea las órdenes que utilizo son:

CClientDC dc(this);

dc.MoveTo(punto1,punto2);
dc.LineTo(punto3,punto4);

//punto1,punto2,punto3 y punto4 son valores enteros con las coordenadas de pantalla

GRACIAS POR ADELANTADO
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Dibujar Flechas

Publicado por JuanC (57 intervenciones) el 04/01/2007 11:06:18
Fijate si el siguiente código te sirve (no es mio, yo hice uno pero más complejo
y en Builder C++)

Arrow.cpp

//--------------------------------------------------------------

/*
ARROWSTRUCT a;
// setup arrows
a.nWidth = 10;
a.fTheta = 30.0f;
a.bFill = true;
MoveTo(100, 100);
ArrowTo(m_hDC, 200, 100, &a);
*/
#include "Arrow.h"
#include <math.h>

//-----------------------------------------------------------------------

void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pA)
{
POINT ptTo = {x, y};
ArrowTo(hDC, &ptTo, pA);
}
//-----------------------------------------------------------------------

void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pA)
{
POINT pFrom, pBase, aptPoly[3];
float vecLine[2], vecLeft[2], fLength, th, ta;

MoveToEx(hDC, 0, 0, &pFrom);

aptPoly[0].x = lpTo->x;
aptPoly[0].y = lpTo->y;

vecLine[0] = (float) aptPoly[0].x - pFrom.x;
vecLine[1] = (float) aptPoly[0].y - pFrom.y;

vecLeft[0] = -vecLine[1];
vecLeft[1] = vecLine[0];

fLength = (float) sqrt(vecLine[0] * vecLine[0] + vecLine[1] * vecLine[1]);
th = pA->nWidth / (2.0f * fLength);
ta = pA->nWidth / (2.0f * (tanl(pA->fTheta*57.29578) / 2.0f) * fLength);

pBase.x = (int) (aptPoly[0].x + -ta * vecLine[0]);
pBase.y = (int) (aptPoly[0].y + -ta * vecLine[1]);

aptPoly[1].x = (int) (pBase.x + th * vecLeft[0]);
aptPoly[1].y = (int) (pBase.y + th * vecLeft[1]);
aptPoly[2].x = (int) (pBase.x + -th * vecLeft[0]);
aptPoly[2].y = (int) (pBase.y + -th * vecLeft[1]);

MoveToEx(hDC, pFrom.x, pFrom.y, NULL);

if(pA->bFill){
LineTo(hDC, aptPoly[0].x, aptPoly[0].y);
Polygon(hDC, aptPoly, 3);
}
else {
LineTo(hDC, pBase.x, pBase.y);
LineTo(hDC, aptPoly[1].x, aptPoly[1].y);
LineTo(hDC, aptPoly[0].x, aptPoly[0].y);
LineTo(hDC, aptPoly[2].x, aptPoly[2].y);
LineTo(hDC, pBase.x, pBase.y);
MoveToEx(hDC, aptPoly[0].x, aptPoly[0].y, NULL);
}
}
//---------------------------------------------------------------------------------------------------

Arrow.h

#ifndef _ARROW_H_
#define _ARROW_H_

#include <windows.h>

// ARROWSTRUCT
//
// Defines the attributes of an arrow.
typedef struct tARROWSTRUCT {
int nWidth; // width (in pixels) of the full base of the arrowhead
float fTheta; // angle (in radians) at the arrow tip between the two
// sides of the arrowhead
bool bFill; // flag indicating whether or not the arrowhead should be
// filled
} ARROWSTRUCT;

// ArrowTo()
//
// Draws an arrow, using the current pen and brush, from the current position
// to the passed point using the attributes defined in the ARROWSTRUCT.
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);

#endif

Saludos desde Baires, JuanC
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Dibujar Flechas

Publicado por Nelek (816 intervenciones) el 04/01/2007 13:04:45
Mmmm, la complicidad depende mucho de como lo quieras hacer. Existen diferentes opciones:

Poniendo una felchita de 50 pixels de largo y con la cabeza de 10 de alto y 10 de largo.

1) la dibujas a pelo

CClientDC dc (this);

//la linea horizontal
dc.MoveTo (pto1, pto2);
dc.LineTo (pto3, pto4);

dc.MoveTo (pto3 - 10, pto4); //Te colocas para la cabeza
dc.LineTo (pto3 - 10, pto4 - 5); //tiras hacia arriba
dc.LineTo (pto3, pto4); //vas hasta el vertice
dc.LineTo (pto3 - 10, pto4 + 5) //parte de abajo del triangulo
dc.LineTo (pto3 - 10, pto4) //Cierras el triangulo

Con eso tienes la flecha sin rellenar, luego te creas una region con las coordenadas del triangulo (o un CRec repitiendo las coordenadas de uno de los vertices) y lo rellenas con una CBrush.

Asi la dibujas en tiempo de desarrollo y es estatica. Si quieres que la flecha se vaya moviendo por la pantalla segun los click o sguiendo al puntero... te recomiendo que crees una funcion que pida las coordenadas del puntero y haces el dibujo de la flecha dentro de la funcion,

o que crees una flecha en el editor de recursos dentro de los BitMaps y luego la cargas en las coordenadas que te den la gana.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar