Thread: MoveToEx() & LineTo() functions

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    MoveToEx() & LineTo() functions

    Hi there..

    Trying to draw a line on a frame using the MoveToEx and LineTo functions with no success.. Here's my code

    Code:
    void DrawOnFrame()
    {
       hwnd = GetFrameWindow();
       hdc = GetDC(hwnd);								
       MoveToEx(hdc, 150, 150, NULL);
       LineTo(hdc, 150, 250);
       ReleaseDC (hwnd, hdc);							
    }
    
    // Call the function
    DrawOnFrame();
    Nothing shows on the frame...

    Is the a way to change the color of the line? Maybe it defaults to the same color as the frame background

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to select a pen with a colour that shows up against your background (so not white!).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    You need to select a pen with a colour that shows up against your background (so not white!).

    --
    Mats
    Mats, i ain't using a pen to draw in this case... Am just using two points (draw from point 1 to point 2)... No mouse involved...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, I don't mean a "MS paint" pen, but a GDI object type called PEN.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    No, I don't mean a "MS paint" pen, but a GDI object type called PEN.

    --
    Mats
    Could you explain a little further perhaps... Is PEN Object useful for just drawing a line using two points to change colors of the drawn line? So long i'll Google the use PEN..

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have a look at CreatePen.

    You will need to pass this object to SelectObject to select the pen.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Have a look at CreatePen.

    You will need to pass this object to SelectObject to select the pen.


    --
    Mats
    Actually was busy going through it, but seems it's more for C++ than C, am coding C.. but will try that... Thanks

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    The GDI drawing functions, like lineto, use the default GDI objects ( pen and brush ) to draw. I believe the defaults are black pen and white brush. You can change this if needed. Look at GDI objects in the help. When you create a brush or pen you have to select them using SelectObject for the DC to use them.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not C++ at all in the link I gave.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Not C++ at all in the link I gave.

    --
    Mats
    Sorry, didn't notice the link

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    If you do a board search under 'mouse drawing (or perhaps draw with mouse) you'll find valid examples of this. Not suggesting copying someone else's code, but you could study it and find out why their code works and yours doesn't, along with taking in the advice given within said threads.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    You need to select a pen with a colour that shows up against your background (so not white!).

    --
    Mats
    The code below is my first attempt of using the Pen tool, no success... Nothing shows on the frame... The brackground of my frame is (0,0,128) RGB...

    Anything wrong with this code?

    Code:
    void DrawWithPen()
    {
        PAINTSTRUCT pntS;
        HPEN pen, oldPen;
    
        hwnd = GetFrameWindow();							
        hdc = BeginPaint(hwnd, &pntS);						
        pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));	
        oldPen = (HPEN)SelectObject(hdc, pen);				
    
        MoveToEx(hdc, 150, 150, NULL); 		
        LineTo(hdc, 150, 200); 		
    
    
        SelectObject(hdc, oldPen); 
        DeleteObject(pen);
        EndPaint(hwnd, &pntS);
    }
    
    calls the function...
    
    DrawWithPen();

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    give this a try to see if ti works...

    Code:
    void DrawWithPen()
    {
       // PAINTSTRUCT pntS;
        HPEN pen, oldPen;
    
        hwnd = GetFrameWindow();							
       // hdc = BeginPaint(hwnd, &pntS);
       hdc =  GetDC(hwnd);						
        pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));	
        oldPen = (HPEN)SelectObject(hdc, pen);				
    
        MoveToEx(hdc, 150, 150, NULL); 		
        LineTo(hdc, 150, 200); 		
        SelectObject(hdc, oldPen); 
        DeleteObject(pen);
       // EndPaint(hwnd, &pntS);
    }

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by BobS0327 View Post
    give this a try to see if ti works...

    Code:
    void DrawWithPen()
    {
       // PAINTSTRUCT pntS;
        HPEN pen, oldPen;
    
        hwnd = GetFrameWindow();                            
       // hdc = BeginPaint(hwnd, &pntS);
       hdc =  GetDC(hwnd);                        
        pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));    
        oldPen = (HPEN)SelectObject(hdc, pen);                
    
        MoveToEx(hdc, 150, 150, NULL);         
        LineTo(hdc, 150, 200);         
        SelectObject(hdc, oldPen); 
        DeleteObject(pen);
       // EndPaint(hwnd, &pntS);
    }
    No luck yet Bob, below is the entire code, tried the rectangle, doesn't work too
    Code:
    HDC hMemDC;
    HBITMAP hBmp, hBmpOrig;
    HBITMAP hOldBmp;
    HWND hwnd;
    HDC hdc;
    
    // Function to draw the line
    void DrawWithPen()
    {
    // PAINTSTRUCT pntS; HPEN pen, oldPen; hwnd = GetFrameWindow(); // hdc = BeginPaint(hwnd, &pntS); hdc = GetDC(hwnd); pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0)); oldPen = (HPEN)SelectObject(hdc, pen); MoveToEx(hdc, 150, 150, NULL); LineTo(hdc, 150, 200); SelectObject(hdc, oldPen); DeleteObject(pen); // EndPaint(hwnd, &pntS);
    } // DrawARectangle - draws a red rectangle with a green border // No return value. // hdc - handle to the device context void DrawARectangle() {
    HPEN hpen, hpenOld; HBRUSH hbrush, hbrushOld; PAINTSTRUCT pntS; hwnd = GetFrameWindow(); // Retrieve the frame handle hdc = BeginPaint(hwnd, &pntS); // Prepare the specified window for painting hpen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0)); // Create a red brush. hbrush = CreateSolidBrush(RGB(255, 0, 0)); // Select the new pen and brush, and then draw. hpenOld = SelectObject(hdc, hpen); hbrushOld = SelectObject(hdc, hbrush); Rectangle(hdc, 100,100, 200,200); // Do not forget to clean up. SelectObject(hdc, hpenOld); DeleteObject(hpen); SelectObject(hdc, hbrushOld); DeleteObject(hbrush); EndPaint(hwnd, &pntS);
    } // CALL FUNCTION DrawWithPen(); //DrawARectangle();

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Where are you calling DrawWithPen()? Maybe your window needs to be invalidated so a Paint message is sent. Try running the program, then covering it with another window then bring your window back to the top. Does it draw now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM