CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2009
    Posts
    17

    client area problem

    I'm using VC++ 6, in my application there are a main dialog and three nomodal dialogs , the thing is this: in the main dialog there is a function 'drawLine' that draws lines in the client area of the main dialog , and it works fine, BUT when one of these nomodal dialogs is active and calls to the funtion 'drawLine' , the lines aren't drawn in the client area , are drawn in the desktop area. So , how can i do to call to this function 'drawLine' from one of these nomodal dialogs and that lines are drawn in the client area of main dialog?
    this is the first part of code for my function 'drawLines':

    void CCalibracionDlg:rawLines(int Rx,unsigned char D_Ry, unsigned char Vref)
    {
    // Rx en Kohm
    CClientDC pDC(this) ; . . . . . and then i use pDC.SetPixel to draw lines. . .

    I think that the solution is to use CClientDC pDC properly but i don't know how.

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: client area problem

    What do you mean by "it draws on desktop area". Do you mean it draws on the user's desktop (and not on any of your application windows?).

    How did you call drawLine, and what is actual code under drawLine?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    Yes that's what i mean.
    this is the call to drawLine ( in one of the nomodal dialos):
    pCalibracionDlg->drawLine(1000,D_Ry,5); // where pCalibracionDlg is a pointer to the main dialog

    this is the code for drawLine(comments are in spanish) , remember that this function is in the main dialog and his class is CCalibraionDlg, the most important lines are (1) (2) (3)

    void CCalibracionDlg:rawLine(int Rx,unsigned char D_Ry, unsigned char Vref)
    {
    // Rx en Kohm
    CClientDC pDC(this) ; //(1)
    float tgAng=0,Ry=0; // float porque estas variables tienen cocientes
    int x = 0 , y = 0 ,y_=0; // int porque pueden ser mayor q 255
    char multiplo=0;
    unsigned char indice_recta=0, y_RectaVieja[300]; // con char alcanza
    unsigned int pixel=0, PixelRectaVieja[300]; // int porque son de 32bit
    pDC.SetWindowOrg(-168,-165); // seteo el origen de coordenadas a ese
    // aca deberia llamar a BorrarRecta
    Ry=(256-D_Ry)*(10000/256) + 45; // forma de variacion de Ry (AD5204/06)
    tgAng=-(Ry/Rx); //declaro esta tangente que es la pendiente
    indice_recta=0;

    if ((tgAng >= -1) && (tgAng <= 1)) // si la pendiente esta entre 1 y -1 (incluyendo 1 y -1) grafico
    { // ya q entre +-45º a cada punto de x corresponderá un

    for ( x=-150; x<150; x++) // aca grafico en funcion de x
    {
    y=Vref*((Ry+Rx)/Rx) - x * Ry/Rx ;
    y_RectaVieja[indice_recta]=y;
    y_=-y;
    PixelRectaVieja[indice_recta]= pDC.GetPixel(x,y_);
    if ( (y > -149) && (y < 150) ) // -149 porque 150 queda muy en el borde del lado de
    {
    pixel= RGB(0,0,0); // por defecto pixel es color negro
    if ((y==0) || (x==0) ) { pixel = RGB(255,0,0);} // si estamos en los ejes pixel es
    multiplo==-5;
    while(multiplo <=5)
    {
    if ((y==30*multiplo) || (x==30*multiplo)) {pixel=RGB(255,255,255);}//si
    multiplo++;
    if (multiplo==0) {multiplo=1;} //no analizo para cuando multiplo es 0 , ya q a
    }
    pDC.SetPixel( x,y_, pixel); //(2)
    }
    indice_recta++;
    }
    }
    if ((tgAng < -1) || (tgAng > 1)) // si la pendiente es > a 1 o < -1 grafico xf(y),ya q si la
    { // punto de y corresponderá un x q no será mayor q
    //dibujara yf(x) solo a algunos puntos de x

    for ( y=-149; y<150; y++) // aca grafico x en funcion de y
    {
    x=Vref*((Ry+Rx)/Ry) - y * Rx/Ry ;
    y_RectaVieja[indice_recta]=y;
    y_=-y;
    PixelRectaVieja[indice_recta]= pDC.GetPixel(x,y_);
    if ( (x > -150) && (x < 150) )
    {
    pDC.SetPixel( x,y_, /*pixel*/RGB(0,0,255)); // (3)
    }
    indice_recta++;
    }
    }
    }

    thanks for your answer.

  4. #4
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    Besides , if drawLine is called by the main dialog (remember that drawLine it's defined and declared within the main dialog class) , it works fine, let's say, it's draws over client area of main's dialog window, but when it's called from one of these three nomodal dialogs , in this case it's draws over main window of desktop, where the coordenates (0,0) are in top-left corner of monitor, and i want that drawLine draws always over main's dialog window of my application. Sorry by my poor english and thanks in advance.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: client area problem

    First off, please use code tags:

    [code]
    int main(int argc, char *argv[]) //code goes here
    {
    //...
    return ERROR_SUCCESS;
    }
    [/code]

    gives a much easier to read:
    Code:
    int main(int argc, char *argv[])  //code goes here
    {
      //...
      return ERROR_SUCCESS;
    }
    See? Otherwise it is impossible to follow more than a few lines of code and many gurus will ignore it.

    Second, you should always (with very few exceptions) do all your drawing in the OnDraw (for CView derived classes) or OnPaint() (WM_PAINT handler in non-view CWnd derived classes) functions. Otherwise, anything drawn gets smashed as soon as you move your window, drag another window over it, etc.

    Third, when you are initializing your CClientDC() above, make sure the "this" pointer is actually the dialog to which you are drawing. It sounds like maybe your "this" is invalid (not a CWnd, not a valid m_hWnd member, etc.) which may result in getting a DC associated with the desktop window (I'm speculating a bit since CClientDC(NULL) gives a DC for the desktop window).

  6. #6
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    Ok , thanks for your suggestions. About your second suggestion , is true , and i will do that, but now is working because my aplication runs over an indrustial pc , and there isn't posibility of move or drag the window.
    About your 3rd suggestion , i think that you say is true, it's probably that pointer 'this' works fine when drawLine is called by the main dialog , and when it's called by another dialog happens that you said. so , how can i do to be sure that CClientDC takes the DC for the client area of main dialog?

    and here is the code with tags
    Code:
    void CCalibracionDlg::RedibujarRecta(int Rx,unsigned char D_Ry, unsigned char Vref)
    {
    // Rx en Kohm
    CClientDC pDC(this) ;
    float tgAng=0,Ry=0;			// float porque estas variables tienen cocientes		
    int  x = 0 , y = 0 ,y_=0;	// int porque pueden ser mayor q 255
    unsigned char indice_recta=0, y_RectaVieja[300]; // con char alcanza
    unsigned int pixel=0, PixelRectaVieja[300];		// int porque son de 32bit
    pDC.SetWindowOrg(-168,-165);				// seteo el origen de coordenadas a ese punto, q es el centro de la grilla
    // aca deberia llamar a BorrarRecta
    Ry=(256-D_Ry)*(10000/256) + 45;			// forma de variacion de Ry (AD5204/06)
    tgAng=-(Ry/Rx);							//declaro esta tangente que es la pendiente de la recta a graficar
    indice_recta=0;
    
    if ((tgAng >= -1) && (tgAng <= 1)) // si la pendiente esta entre 1 y -1 (incluyendo 1 y -1) grafico yf(x),  
    	{							   // ya q entre +-45º a cada punto de x corresponderá un y q no será mayor q 150,y por lo tanto será dibujado
    								   //si aqui dibujara xf(y) solo a algunos puntos de y corresponderian  puntos x q no sean mayor de 150, con lo q se graficarian menos puntos											
    	for ( x=-150; x<150; x++)	   // aca  grafico en funcion de x
    		{
    		y=Vref*((Ry+Rx)/Rx) - x * Ry/Rx ;
    		y_RectaVieja[indice_recta]=y;
    		y_=-y;
    		PixelRectaVieja[indice_recta]= pDC.GetPixel(x,y_);
    		if ( (y > -149) && (y < 150) ) // -149 porque 150 queda muy en el borde del lado de abajo, por el sombreado del marco
    			{
    			pDC.SetPixel( x,y_, /*pixel*/ RGB(0,255,0));
    			}
    			indice_recta++;
    		}
    	}
    if ((tgAng < -1) || (tgAng > 1))  // si la pendiente es > a 1 o < -1  grafico xf(y),ya q si la pendiente es >45º o <-45º a cada
    	{							  // punto de y corresponderá un x q no será mayor q 150,y por lo tanto será dibujado, si aqui 
    								  //dibujara yf(x) solo a algunos puntos de x corresponderian  puntos 'y' q no sean mayor de 150, con lo q se graficarian menos puntos			
    	for ( y=-149; y<150; y++)	  // aca grafico x en funcion de y
    		{
    		x=Vref*((Ry+Rx)/Ry) - y * Rx/Ry ;
    		y_RectaVieja[indice_recta]=y;
    		y_=-y;
    		PixelRectaVieja[indice_recta]= pDC.GetPixel(x,y_);
    		if ( (x > -150) && (x < 150) )
    			{
    			pDC.SetPixel( x,y_, /*pixel*/RGB(0,0,255));
    			}
    			indice_recta++;
    		}
    	}
    }

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: client area problem

    I'm assuming CCalibracionDlg derives from CDialog and ultimately from CWnd (which is the class that CClientDC cares about), so put a breakpoint at CClientDC pDC(this); and check for valid m_hWnd member (this->m_hWnd).

  8. #8
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    Yes CCalibracionDlg is derived from CDialog, well i already made your instruction, that's what i got:
    when drawLine is called by the main dialog (works fine) i got this at the breakpoint:
    Code:
    +	pDC	{hDC=0x7501145c attrib=0x7501145c}
    	Ry	-1.07374e+008
    	tgAng	-1.07374e+008
    -	this	0x0012fcd8 {CCalibracionDlg hWnd=0x00060814}
    +	CDialog	{CDialog hWnd=0x00060814}
    +	m_Button_mas	{CButton hWnd=0x000108a0}
    +	m_Linea1_On_Off	{CButton hWnd=0x00000000}
    +	m_Almacenar	{CButton hWnd=0x0001087a}
    +	m_Mapa3	{CButton hWnd=0x00010870}
    +	m_Mapa2	{CButton hWnd=0x0001086e}
    +	m_Mapa1	{CButton hWnd=0x0001086c}
    +	m_Prueba	{""}
    +	m_Prueba2	{""}
    +	m_Prueba3	{""}
    +	m_Label_Xpos	{"R+"}
    +	m_Label_Xneg	{"R-"}
    +	m_Label_Yneg	{"V-"}
    +	m_Label_Ypos	{"V+"}
    +	m_hIcon	0x00290687
    +	_messageEntries	0x00417150 struct AFX_MSGMAP_ENTRY const * const CCalibracionDlg::_messageEntries
    +	messageMap	{...}
    and when dragLine is called by one of another dialogs (don't works fine) i got this :
    Code:
    +	pDC	{hDC=0x7501145c attrib=0x7501145c}
    	Ry	-1.07374e+008
    	tgAng	-1.07374e+008
    -	this	0x00000000 {CCalibracionDlg hWnd=???}
    	CDialog	CXX0030: Error: expression cannot be evaluated
    +	m_Button_mas	{CButton hWnd=???}
    +	m_Linea1_On_Off	{CButton hWnd=???}
    +	m_Almacenar	{CButton hWnd=???}
    +	m_Mapa3	{CButton hWnd=???}
    +	m_Mapa2	{CButton hWnd=???}
    +	m_Mapa1	{CButton hWnd=???}
    +	m_Prueba	{???}
    +	m_Prueba2	{???}
    +	m_Prueba3	{???}
    +	m_Label_Xpos	{???}
    +	m_Label_Xneg	{???}
    +	m_Label_Yneg	{???}
    +	m_Label_Ypos	{???}
    	m_hIcon	CXX0030: Error: expression cannot be evaluated
    +	_messageEntries	0x00417150 struct AFX_MSGMAP_ENTRY const * const CCalibracionDlg::_messageEntries
    +	messageMap	{...}
    what does it means? for some reason the handler of the windows lose his content , why?
    sorry by my ignorance , but, how can i fix this?

  9. #9
    Join Date
    Feb 2005
    Posts
    2,160

    Re: client area problem

    In the second case, how come "this" is NULL? How are you calling the function? Are you calling the CCalibracionDlg's class member RedibujarRecta member function from a class that is NOT CCalibracionDlg?

  10. #10
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    first of all, i forgot traduce RedibujarRecta is the spanish word for drawLine, so RedibujarRecta=drawLine.
    And answering your second question , yes I'm calling CCalibracionDlg's member drawLine( or RedibujarRecta) from another class 'CDlgMap1' that's the class for one of nomodal dialogs , is incorrect to do this? both classes are derived from CDialog.

    this is the call to drawLine from CDlgMap1:

    pCalibracionDlg->drawLine(1000,D_Ry,5); //where pCalibracionDlg is a pointer to CCalibracionDlg

  11. #11
    Join Date
    Feb 2005
    Posts
    2,160

    Re: client area problem

    Where to start?!?! It is possible to call other class members from a different class, but there's dozens of caveats. I'll just leave all the OOP nuances alone and suggest that the simplest way to get your program to work the way you want is to just copy the contents of CCalibracionDlg::RedibujarRecta into a function defined in CDlgMap1 with the same name and declaration and call it directly from CDlgMap1 objects.

    I can't can't come up with a very good analogy, but I'll try: suppose you have two cars. Both are Fords, but different models with different sound systems. It's like you are sitting in one car and trying to operate the radio in the other car (say by a remote control or something) and expecting it to play over the speakers in the car your sitting in.

  12. #12
    Join Date
    Oct 2009
    Posts
    17

    Re: client area problem

    Ok, thanks. I'm back to work. as you see I'm newbie .Yes, if I folow your suggestion the problem will disapear , but the lines going to be drawn in client area of each three non-main dialogs. This is what I want to do:
    -when a specific button is pressed in one of non-main dialogs, the associated line must to be drawn in the client area of the main dialog. Any ideas? . Thanks for to be patient.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured