My window easy move bug appears in NT/2000. Help??
-
The following code works fine with Windows9x, but not with NT/2000. Can someone help me fix it? Or is there a better approach for moving the window rather than tracking the mouse position and use SetWindowPos(...)? Thanks! p.s: I know it's a Delphi code, but it's using Win32 API, not the VCL. After all, I'm converting the code to VC++ :) program testwin; {$i-} // for loggin option uses windows, messages; const szAppName = 'test window'; szClassName = 'test window class'; var WndClass: TWNDCLASS; Msg: TMSG; hInst: HWND; hMainWnd: HWND; bDragging: Boolean; nOldX, nOldY: Integer; logf: Text; function WndProc(hMainWnd: HWND; Msg, wParam, lParam: LongInt): LongInt; stdcall; var r: TRECT; begin case Msg of WM_CLOSE:; WM_DESTROY: PostQuitMessage(0); WM_LBUTTONDOWN: begin SetCapture(hMainWnd); bDragging := TRUE; nOldX := LoWord(lParam); nOldY := HiWord(lParam); end; WM_LBUTTONUP: begin bDragging := FALSE; ReleaseCapture; end; WM_MOUSEMOVE: begin if ((wParam = MK_LBUTTON) and (bDragging = TRUE)) then begin GetWindowRect(hMainWnd, r); r.left := r.left + (LoWord(lParam) - nOldX); r.top := r.top + (HiWord(lParam) - nOldY); SetWindowPos(hMainWnd, 0, r.left, r.top, 0, 0, SWP_NOSIZE); writeln(logf, 'r.left = ', r.left); writeln(logf, 'r.top = ', r.top); end; end; end; result := DefWindowProc(hMainWnd, Msg, wParam, lParam); end; procedure PreWindow; begin hMainWnd := CreateWindow(szClassName, szAppName, WS_VISIBLE or WS_POPUP or WS_SYSMENU, 0, 0, 200, 100, 0, 0, hInst, nil); ShowWindow(hMainWnd, SW_SHOWNORMAL); UpdateWindow(hMainWnd); end; procedure PreClass; begin hInst := GetModuleHandle(nil); WndClass.style := CS_DBLCLKS; WndClass.lpfnWndProc := @WndProc; WndClass.lpszClassName := szClassName; WndClass.hCursor := LoadCursor(0, IDC_ARROW); WndClass.hInstance := hInst; WndClass.hbrBackground := COLOR_BTNFACE + 1; RegisterClass(WndClass); end; begin PreClass; PreWindow; AssignFile(logf, 'testwin.log'); Rewrite(logf); while (GetMessage(Msg, 0, 0, 0)) do begin TranslateMessage(Msg); DispatchMessage(Msg); end; closefile(logf); end.
-
The following code works fine with Windows9x, but not with NT/2000. Can someone help me fix it? Or is there a better approach for moving the window rather than tracking the mouse position and use SetWindowPos(...)? Thanks! p.s: I know it's a Delphi code, but it's using Win32 API, not the VCL. After all, I'm converting the code to VC++ :) program testwin; {$i-} // for loggin option uses windows, messages; const szAppName = 'test window'; szClassName = 'test window class'; var WndClass: TWNDCLASS; Msg: TMSG; hInst: HWND; hMainWnd: HWND; bDragging: Boolean; nOldX, nOldY: Integer; logf: Text; function WndProc(hMainWnd: HWND; Msg, wParam, lParam: LongInt): LongInt; stdcall; var r: TRECT; begin case Msg of WM_CLOSE:; WM_DESTROY: PostQuitMessage(0); WM_LBUTTONDOWN: begin SetCapture(hMainWnd); bDragging := TRUE; nOldX := LoWord(lParam); nOldY := HiWord(lParam); end; WM_LBUTTONUP: begin bDragging := FALSE; ReleaseCapture; end; WM_MOUSEMOVE: begin if ((wParam = MK_LBUTTON) and (bDragging = TRUE)) then begin GetWindowRect(hMainWnd, r); r.left := r.left + (LoWord(lParam) - nOldX); r.top := r.top + (HiWord(lParam) - nOldY); SetWindowPos(hMainWnd, 0, r.left, r.top, 0, 0, SWP_NOSIZE); writeln(logf, 'r.left = ', r.left); writeln(logf, 'r.top = ', r.top); end; end; end; result := DefWindowProc(hMainWnd, Msg, wParam, lParam); end; procedure PreWindow; begin hMainWnd := CreateWindow(szClassName, szAppName, WS_VISIBLE or WS_POPUP or WS_SYSMENU, 0, 0, 200, 100, 0, 0, hInst, nil); ShowWindow(hMainWnd, SW_SHOWNORMAL); UpdateWindow(hMainWnd); end; procedure PreClass; begin hInst := GetModuleHandle(nil); WndClass.style := CS_DBLCLKS; WndClass.lpfnWndProc := @WndProc; WndClass.lpszClassName := szClassName; WndClass.hCursor := LoadCursor(0, IDC_ARROW); WndClass.hInstance := hInst; WndClass.hbrBackground := COLOR_BTNFACE + 1; RegisterClass(WndClass); end; begin PreClass; PreWindow; AssignFile(logf, 'testwin.log'); Rewrite(logf); while (GetMessage(Msg, 0, 0, 0)) do begin TranslateMessage(Msg); DispatchMessage(Msg); end; closefile(logf); end.
-
There's a one-line way to do this! When you receive a WM_LBUTTONDOWN message, do PostMessage ( hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );
-
There's a one-line way to do this! When you receive a WM_LBUTTONDOWN message, do PostMessage ( hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );
-
well, i tried it. but umm, that's now what i want. i'll give you the sample code if you feel like to play with it. it's in vc++ code. just e-mail me.
OK, I thought you were going for an effect like WinAmp. (That is, you can move the window by clicking and dragging in any open part of the dialog.) The code in your original post didn't come out very well (not your fault) so I couldn't really tell what you were trying to do.