FillRect C++
-
Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:
HINSTANCE hInst = ::GetModuleHandle(NULL); WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH ); wc.hCursor = LoadCursor( NULL, IDC\_CROSS ); wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION ); wc.hInstance = hInst; wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc; wc.lpszClassName = L"Test"; wc.lpszMenuName = 0; wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.hIconSm = NULL; m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:
case WM\_MOUSEMOVE: { if (pClass->m\_MouseDown==1) //to indicate mouse is pressed before drawing { HDC hdc; hdc = ::GetWindowDC(hwnd); RECT re; SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY); HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH); if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y) //if mouse move into different position { static int count = 0; TRACE(L"Test %d\\n", count++); FillRect(hdc,&re,brush); pClass->m\_tempPoint=p; } CPen mypen(PS\_DOT, 1, RGB(0,0,0)); HPEN pen = (HPEN)mypen; SelectObject(hdc, pen); SelectObject(hdc, brush); ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y); } break; }
Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~
Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.
-
Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:
HINSTANCE hInst = ::GetModuleHandle(NULL); WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH ); wc.hCursor = LoadCursor( NULL, IDC\_CROSS ); wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION ); wc.hInstance = hInst; wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc; wc.lpszClassName = L"Test"; wc.lpszMenuName = 0; wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.hIconSm = NULL; m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:
case WM\_MOUSEMOVE: { if (pClass->m\_MouseDown==1) //to indicate mouse is pressed before drawing { HDC hdc; hdc = ::GetWindowDC(hwnd); RECT re; SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY); HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH); if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y) //if mouse move into different position { static int count = 0; TRACE(L"Test %d\\n", count++); FillRect(hdc,&re,brush); pClass->m\_tempPoint=p; } CPen mypen(PS\_DOT, 1, RGB(0,0,0)); HPEN pen = (HPEN)mypen; SelectObject(hdc, pen); SelectObject(hdc, brush); ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y); } break; }
Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~
Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.
You should not be drawing to your window in the message handler for the mousemove. The mouse move code should set the limits and other details of your shape, but the actual drawing should be done in your
OnDraw()
orOnPaint()
handler. There are lots of samples on MSDN[^].It's time for a new signature.
-
Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:
HINSTANCE hInst = ::GetModuleHandle(NULL); WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH ); wc.hCursor = LoadCursor( NULL, IDC\_CROSS ); wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION ); wc.hInstance = hInst; wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc; wc.lpszClassName = L"Test"; wc.lpszMenuName = 0; wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.hIconSm = NULL; m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:
case WM\_MOUSEMOVE: { if (pClass->m\_MouseDown==1) //to indicate mouse is pressed before drawing { HDC hdc; hdc = ::GetWindowDC(hwnd); RECT re; SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY); HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH); if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y) //if mouse move into different position { static int count = 0; TRACE(L"Test %d\\n", count++); FillRect(hdc,&re,brush); pClass->m\_tempPoint=p; } CPen mypen(PS\_DOT, 1, RGB(0,0,0)); HPEN pen = (HPEN)mypen; SelectObject(hdc, pen); SelectObject(hdc, brush); ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y); } break; }
Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~
Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.
Additional to the words of Richard this "dotted rectangle"[^] could be used... :)
virtual void BeHappy() = 0;
-
You should not be drawing to your window in the message handler for the mousemove. The mouse move code should set the limits and other details of your shape, but the actual drawing should be done in your
OnDraw()
orOnPaint()
handler. There are lots of samples on MSDN[^].It's time for a new signature.
what if i use bitblt to draw the rectangle? However, there is a problem i faced, after i've bitblt, the background turns to be black color. I just don't why? "BitBlt(Hdc, 0, 0, pClass->m_ComputerResolutionX, pClass->m_ComputerResolutionY, pClass->m_MemDC, 0, 0, SRCCOPY);" The bitblt was done in WM_PAINT. Anyone has any ideas?
-
what if i use bitblt to draw the rectangle? However, there is a problem i faced, after i've bitblt, the background turns to be black color. I just don't why? "BitBlt(Hdc, 0, 0, pClass->m_ComputerResolutionX, pClass->m_ComputerResolutionY, pClass->m_MemDC, 0, 0, SRCCOPY);" The bitblt was done in WM_PAINT. Anyone has any ideas?