Draw line

Hello

I have to draw a line that follows my mouse pointer over an image without changing the picture behind.
Here are my steps:
I stored in a content HBITMAP (nt_getBitmap)
I recovered the position of my mouse
As I did not click my mouse I:
I trace my content (nt_setBitmap)
I trace my line (nt_drwseg)


My problem is that the background does not track and is all my lines remain.
Thank you for your help

CODE ::::


(hBITMAPTMP -> is global variable)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
nt_getBitmap(int *iwin)
{
    HDC hDC,tmpDC;
	HWND hWnd;
	HBITMAP tmpbmap;
	RECT rect;
	int height,width,i,j;
	HGDIOBJ old;

	hWnd =(HWND)(*iwin);
	hDC = GetDC(hWnd);
	GetClientRect (hWnd, &rect);
	height = abs(rect.top-rect.bottom);
	width = abs(rect.right-rect.left);
	//creation DC
	tmpDC = CreateCompatibleDC(hDC);
	hBITMAPTMP = CreateCompatibleBitmap(hDC,height,width);
	SelectObject(tmpDC,hBITMAPTMP);
	BitBlt(tmpDC,0,0,height,width,hDC,0,0,SRCCOPY);
	SelectObject(tmpDC,hBITMAPTMP);
	DeleteDC(tmpDC);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void nt_setBitmap(int *iwin)
{
	HDC hDC;
	HWND hWnd;
	RECT rect;
	BITMAP bm;
	HDC hdcMem;

	int height,width;

	hWnd =(HWND)(*iwin);
	SetFocus(hWnd);
	hDC=GetDC(hWnd);
	GetObject(hBITMAPTMP, sizeof(BITMAP), &bm);
	hdcMem = CreateCompatibleDC(hDC);
	SelectObject(hdcMem, hBITMAPTMP);
 
	//draw the bitmap to the window (bit block transfer)
	BitBlt( 
	hDC, //destination device context
	0, 0, //x,y location on destination
	bm.bmWidth, bm.bmHeight, //width,height of source bitmap
	hdcMem, //source bitmap device context
	0, 0, //start x,y on source bitmap
	SRCCOPY); //blit method
	ReleaseDC(hWnd,hDC);
 	//delete the device context 
	 DeleteDC(hdcMem);

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void nt_drwseg (int *iwin,int *ix,int *iy,int *npts,int *ixy)
{

      int i,flag,couleur;
      HWND hWnd;
      HDC hDC;
      HPEN hOldPen,drawPen;
      POINT Pstart,Pend,oldpoint;

      hWnd =(HWND)(*iwin);
      SetFocus(hWnd);
      hDC=GetDC(hWnd);

      couleur = M_GRAPHIC_mp_ICCOU;
      if(M_GRAPHICSHARED_mp_INVCOU)
      {
        if(couleur == 0) couleur = 1;
        else if(couleur == 1)couleur = 0;
      }
      drawPen = CreatePen(PS_SOLID,M_GRAPHIC_mp_LINEWIDTH,PALETTEINDEX(M_GRAPHIC_mp_LISCOU[couleur]));
      hOldPen = (HPEN)(SelectObject(hDC,drawPen));

      Pstart.x = *ix;
      Pstart.y = *iy;

      for(i=0;i<*npts;i++)
      {
        Pstart.x=*ix+ixy[4 * i    ];
        Pstart.y=*iy+ixy[4 * i + 1];
        Pend.x  =*ix+ixy[4 * i + 2];
        Pend.y  =*iy+ixy[4 * i + 3];
        MoveToEx(hDC,Pstart.x,Pstart.y,&oldpoint);
        LineTo(hDC,Pend.x,Pend.y);
      }
    SelectObject(hDC,hOldPen);
      DeleteObject(drawPen);
      flag = GetROP2(hDC);
      ReleaseDC(hWnd,hDC);
      if (flag == Isy_Xor) return;
}

Can you explain your question differently, I don't understand what your problem is, or what you want these functions to actually do.
Are you needing to invalidate part of the screen so that the picture is redrawn and the line goes away? It sounds like your problem is that the line satys on screen but the background picture is lost, right?

Try making wndclass.hbrBackground = NULL;

If that doesn't work for you, then my suggestion is to pass the HDC That you create during WM_PAINT to your functions so that they are all repainted when InvalidateRect() is used. Generally all painting should be done when processing the WM_PAINT message.
1
2
3
4
5
6
void drawrect(HDC hdc)
{
    Rectangle(hdc, 10,10,200,200);
}


1
2
3
4
5
case WM_PAINT:
    hdc = BeginPaint(hwnd, &paintstruct);
    drawrect(hdc);
    EndPaint(hwnd, &paintstruct);
return 0;


I don't even know if that is really your problem. Please post your Main() function so we can see what is actually going on, and clarify the question.
Last edited on
Topic archived. No new replies allowed.