Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Dragging Window

Status
Not open for further replies.

Mtlca401

Programmer
Mar 18, 2003
42
US
I have a bitmap loaded as the parent window because I am working on skinning a program. Well I am trying to drag the window with the mouse by clicking anywhere on it; eventually I'll put a caption bar on it, but for now I am just dragging from clicking on the window. The problem I am having is that as I am dragging the window, if I drag to fast then the mouse loses the window. I tried using setcapture() on WM_MOUSEMOVE, but that didn't work because it messes with the controls making me have to click a button 3 times before it is actually pressed. I also tried using releasecapture() WM_LBUTTONUP, but that didn't work either.

I can post my code or maybe someone can just tell me how to drag a window.

thanx
 
You should be using SetCapture on WM_LBUTTONDOWN, not WM_MOUSEMOVE. Then use ReleaseCapture on WM_LBUTTONUP and everything should work.
 
actually I did try that and it didn't work. Is there any type of skinning tutorial? No MFC and no VC++. I want to see if I am doing any thing wrong.
 
Well, you are doing something wrong. Here is the code to do what you are trying to accomplish; I tested it and it works.

Code:
#include <windows.h>
#include "resource.h"

POINT LParamToPoint(LPARAM lParam)
{
	POINT pt;
	pt.x = LOWORD(lParam);
	pt.y = HIWORD(lParam);
	return pt;
}

INT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static POINT ptOffset;
	static bool bLButtonDown = false;

	switch(msg)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		break;

	case WM_LBUTTONDOWN:
		bLButtonDown = true;
		ptOffset = LParamToPoint(lParam);
		SetCapture(hwnd);
		break;

	case WM_LBUTTONUP:
		bLButtonDown = false;
		ReleaseCapture();
		break;

	case WM_MOUSEMOVE:
		if(bLButtonDown)
		{
			RECT rc;
			POINT pt = LParamToPoint(lParam);
			GetWindowRect(hwnd, &rc);

			pt.x += rc.left - ptOffset.x;
			pt.y += rc.top - ptOffset.y;

			SetWindowPos(hwnd, NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
		}
		break;
	}

	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, WindowProc);

	return 0;
}
 
Here is a much easier method to do what timmay3141 said:

case WM_LBUTTONDOWN:
SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0); //this is to move the form
break;
 
Some badly drawn ASCII art depicting what I thought when I read the topic title:
Code:
      HELP!
   ___  '
  |_|_|__O/       O    O
  |_|_| /,       '|`  '|`

Sorry... was bored couldn't resist.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top