How to draw a rectangle around the current cursor's position
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to draw a rectangle around the current cursor's position

  1. #1
    Join Date
    Dec 2017
    Posts
    4

    Question How to draw a rectangle around the current cursor's position

    I am trying to draw a rectangle around the current cursor's position. It works when I move the mouse but the rectangle is disappeared when I stop moving the mouse.

    Code:
    void CView1::OnMouseMove(UINT nFlags, CPoint point)
    {
            if (!m_mouse_tracking)
    	{
    		TRACKMOUSEEVENT tme;
    		tme.cbSize = sizeof(TRACKMOUSEEVENT);
    		tme.dwFlags = TME_HOVER;
    		tme.hwndTrack = this->m_hWnd;
    		tme.dwHoverTime = HOVER_DEFAULT;
    
    		if (::_TrackMouseEvent(&tme))
    		{
    			m_mouse_tracking = true;
    
    			// Draw the 1st rect
    			draw_rect_(m_pDC);
    		}
    	}
    	else
    	{
    		// Draw new rect and erase old rect
    		RedrawWindow(NULL, NULL, RDW_INVALIDATE);
    		draw_rect_(m_pDC);
    	}
    }
    
    void CView1::OnMouseHover(UINT nFlags, CPoint point)
    {
    	m_mouse_tracking = false;
    	
    	draw_rect_(m_pDC);
    }
    Is there something wrong with my source code? Please, help me.
    Last edited by pnt1614; January 3rd, 2018 at 09:22 PM.

  2. #2
    Join Date
    Mar 2017
    Posts
    98

    Re: How to draw a rectangle around the current cursor's position

    Does this help you .?
    Code:
    #pragma once
    
    //The libs
    #include <iostream>
    #include <Windows.h>
    
    /*
     *This method sets the cursor position.
     *Usage:
     *setxy(1,1);
     */
    BOOL setxy(int x, int y)
    {
    	COORD c = {x,y};
    	return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
    }
    
    /*
     *This method draws the rectangle.
     *Usage:
     *DrawRect(1,1,10,10);
     *or
     *DrawRect(1,1,10,10,20,5);
     */
    void DrawRect(int x, int y, int width, int height, int curPosX=0, int curPosY=0)
    {
    	setxy(x,y);cout << char(201);
    	for(int i = 1; i < width; i++)cout << char(205);
    	cout << char(187);
    	setxy(x,height + y);cout << char(200);
    	for(int i = 1; i < width; i++)cout << char(205);
    	cout << char(188);
    	for(int i = y + 1; i < height + y; i++)
    	{
    		setxy(x,i);cout << char(186);
    		setxy(x + width,i);cout << char(186);
    	}
    	setxy(curPosX,curPosY);
    }
    It is enough for an idea, isn’t it.
    If you find my answer helpful, do rate my post.
    Last edited by A_Singh; January 3rd, 2018 at 10:21 PM.

  3. #3
    Join Date
    Mar 2017
    Posts
    98

    Re: How to draw a rectangle around the current cursor's position

    You can use use the setxy function to set your position, you can make a header file out of this code to help you.(DrawRect.h)
    All these were just suggestions, the code is up to you.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)