Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. My window easy move bug appears in NT/2000. Help??

My window easy move bug appears in NT/2000. Help??

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++delphijsonquestion
5 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brian
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • B Brian

      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.

      M Offline
      M Offline
      Mike Dunn
      wrote on last edited by
      #2

      There's a one-line way to do this! When you receive a WM_LBUTTONDOWN message, do PostMessage ( hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );

      B 2 Replies Last reply
      0
      • M Mike Dunn

        There's a one-line way to do this! When you receive a WM_LBUTTONDOWN message, do PostMessage ( hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );

        B Offline
        B Offline
        Brian
        wrote on last edited by
        #3

        err... before i try it out, may i ask, what's the effect? does it work like winamp's easy move? thx!!!

        1 Reply Last reply
        0
        • M Mike Dunn

          There's a one-line way to do this! When you receive a WM_LBUTTONDOWN message, do PostMessage ( hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );

          B Offline
          B Offline
          Brian
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • B Brian

            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.

            M Offline
            M Offline
            Mi
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups