Overcome Window Flicker While Dragging


Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

While I use the CImageList control for dragging, I found it flickers in the owner window while draging and redraw the client region. I tried Google to find a solution, but there were no result, so I try to solve it myself.

Here's my solution, which I'm very glad share with you!

First, you should register a new window class and create a new top most tool window, and move it's position far off screen(I take 99000), so it's not visible. The code would look like this:

BOOL CImageDragWrapper::Initialize()
      return TRUE;

   wcex.cbSize = sizeof(WNDCLASSEX); 

   if(!::GetClassInfoEx(AfxGetInstanceHandle(), POPWINDOWCLASSNAM, &wcex))
      wcex.style           = CS_HREDRAW | CS_VREDRAW;
      //specifies default window procedure 
      wcex.lpfnWndProc     = (WNDPROC)DefWindowProc;
      wcex.cbClsExtra      = 0;
      wcex.cbWndExtra      = 0;
      wcex.hInstance       = AfxGetInstanceHandle();
      wcex.hIcon           = LoadIcon(0, IDI_INFORMATION);
      wcex.hCursor         = LoadCursor(NULL, IDC_ARROW);
      wcex.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
      wcex.lpszMenuName    = 0;
      wcex.lpszClassName   = POPWINDOWCLASSNAM;
      wcex.hIconSm         = LoadIcon(0, IDI_INFORMATION);   

                        _T(""),WS_POPUP | WS_DISABLED |  WS_CLIPSIBLINGS,99000, 
                        0, 6, 6, 0,   0,
                        AfxGetInstanceHandle(),   0   );

      SetLayeredWindowAttributes(RGB(255,255,255), 100,   /*0x00000001|*/0x0000002);

      ShowWindow(m_hDragWnd, SW_SHOW);
   return m_hDragWnd != 0;

Second, move the window to properly position and paint it's client. The code would look like this:

BOOL CImageDragWrapper::DragBegin(HWND hDragWndOwner,int cxWnd, int cyWnd, 
                int cxHotSpotOffset, int cyHotSpotOffset,
                const POINT& ptCurrentMousePos, HBITMAP hBack, 
				int cxBackOffset, int cyBackOffset)
      m_hDragWndOwner = hDragWndOwner;
      m_cxHotSpotOffset = cxHotSpotOffset;
      m_cyHotSpotOffset = cyHotSpotOffset;

      POINT ptDest = ptCurrentMousePos;
      ClientToScreen(m_hDragWndOwner, &ptDest);

                ptDest.x - m_cxHotSpotOffset, 
                ptDest.y - m_cyHotSpotOffset, 
                cyWnd, TRUE);

      //Draw background image for dragging support window
         HDC hDC = ::GetDC(m_hDragWnd);
         HDC hMemDC = ::CreateCompatibleDC(hDC);
         HBITMAP hbmpOldMem = (HBITMAP)::SelectObject(hMemDC, hBack);
         ::BitBlt(hDC, 0, 

         ::SelectObject(hMemDC, hbmpOldMem);

         ::ReleaseDC(m_hDragWnd, hDC);
   return m_hDragWnd != 0;

Third, changes the position and dimensions for dragging support window by calling MoveWindow

Forth, move dragging to support the window's postion far to screen. The code would look like this:

BOOL CImageDragWrapper::DragEnd()
   if(m_hDragWnd && m_hDragWndOwner)
      m_hDragWndOwner = 0;
                   6, TRUE);
   return m_hDragWnd != 0;

Using the Code:

First, in the InitInstance method of CWinApp's subclass add code like this:

   CTestDragDialogDlg dlg;
   m_pMainWnd = &dlg;
   int nResponse = dlg.DoModal();
   if (nResponse == IDOK)
      // TODO: Place code here to handle when the dialog is
      //  dismissed with OK
   else if (nResponse == IDCANCEL)
      // TODO: Place code here to handle when the dialog is
      //  dismissed with Cancel


Then in dragging the owner window's OnMouseMove, OnLButtonUp method calls DragBegin, DragMove, and DragEnd method of CImageDragWrapper, It's very simple.

Note: you can compare CImageDragWrapper with CImageList by using my sample code!

About the Author

Jehhry23 Lv

I'm come from china, very glad to make friend with you!



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Using Internet of Things (IoT) technologies, colleges and universities are linking anything and everything to create new applications that elevate the campus experience. It's called a Smart Campus. And it's transforming campus life for students, faculty and visitors. When everything is connected, devices and applications can interact with users and with each other in ways that weren't possible before: digital classrooms, connected campus ID cards, connected lighting and building environmental systems and …

  • A CRM solution holds a wealth of information and document generation tools allow users to take that information and create documents with both visual appeal and function. Document generation is the process of automatically producing a file and document generation applications save companies time, mistakes, and money. You bought Salesforce to be more efficient — why are you still manually creating proposals, contracts, invoices, and account plans? Read this eBook to learn how you can automate virtually …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date