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. Need help to pan client area of a window using win32 api / mfc

Need help to pan client area of a window using win32 api / mfc

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelp
13 Posts 4 Posters 0 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.
  • U Offline
    U Offline
    User 9307625
    wrote on last edited by
    #1

    HI, I have a application similar to auto cad, in which we can draw line, circle, etc in client area using mouse or command. Now i need to add a feature, so that the users of this application should be able to pan their drawings in client area using mouse left button. (like one in auto cad or pdf reader). I can use win32 API or MFC to achieve this. It well be great of some one can help me in this regard, with some sample code. Thanks in Advance

    L S P 3 Replies Last reply
    0
    • U User 9307625

      HI, I have a application similar to auto cad, in which we can draw line, circle, etc in client area using mouse or command. Now i need to add a feature, so that the users of this application should be able to pan their drawings in client area using mouse left button. (like one in auto cad or pdf reader). I can use win32 API or MFC to achieve this. It well be great of some one can help me in this regard, with some sample code. Thanks in Advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You just need to add code to your mouse handler to capture the amount of movement and use that information to adjust a variable which tells your program how far to the left or right the window should be moved. Then call InvalidateRect()[^] to tell the system that your window needs repainting. In your OnPaint() handler you use the stored variable to repaint that part of your data that falls within the physical window.

      One of these days I'm going to think of a really clever signature.

      U 1 Reply Last reply
      0
      • L Lost User

        You just need to add code to your mouse handler to capture the amount of movement and use that information to adjust a variable which tells your program how far to the left or right the window should be moved. Then call InvalidateRect()[^] to tell the system that your window needs repainting. In your OnPaint() handler you use the stored variable to repaint that part of your data that falls within the physical window.

        One of these days I'm going to think of a really clever signature.

        U Offline
        U Offline
        User 9307625
        wrote on last edited by
        #3

        ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.

        L 2 Replies Last reply
        0
        • U User 9307625

          ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 9353776 wrote:

          after getting x and y coordinates which API should I use to reset the x and y coordinates?

          What do you mean by this? As I said in my reply, your program needs to capture the mouse movement and adjust the x and y values by the number of pixels moved. In your OnPaint() function you will be copying some part of your image onto the application's window. I assume the image (drawing) is larger than the window on which it is being drawn, so you just select the top left point from which to start based on the x and y values previously captured.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • U User 9307625

            ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You may also like to read this section of the GDI reference[^], which helps to visualise how this sort of problem can be addressed.

            One of these days I'm going to think of a really clever signature.

            1 Reply Last reply
            0
            • U User 9307625

              HI, I have a application similar to auto cad, in which we can draw line, circle, etc in client area using mouse or command. Now i need to add a feature, so that the users of this application should be able to pan their drawings in client area using mouse left button. (like one in auto cad or pdf reader). I can use win32 API or MFC to achieve this. It well be great of some one can help me in this regard, with some sample code. Thanks in Advance

              S Offline
              S Offline
              Sunil P V
              wrote on last edited by
              #6

              Hi, We can achieve this. Please understand the below simple concept with respect to CAD or any drafting utility. We have 2 view perspective always. 1. the world window 2. the view port window. (this is what you need to add in your program) The above can be best understood with the this example: Assume you are taking photograph of a flower in a park. Now, we focus only the flower in the view finder of your camera, and the park in this case forms the world around the flower. So in this case the park becomes the world window and the flower becomes the view window. Similarly the whole drawing in your application is the world window whereas the part of the drawing that is shown to the end user is the view window. You have to make use of the SetViewportOrg method in your application. Please read this http://msdn.microsoft.com/en-us/library/46t66w7t.aspx[^]. Let me know if you need more help, I have done this before but unfortunately cannot find the source for it...

              Sunil

              L U 2 Replies Last reply
              0
              • S Sunil P V

                Hi, We can achieve this. Please understand the below simple concept with respect to CAD or any drafting utility. We have 2 view perspective always. 1. the world window 2. the view port window. (this is what you need to add in your program) The above can be best understood with the this example: Assume you are taking photograph of a flower in a park. Now, we focus only the flower in the view finder of your camera, and the park in this case forms the world around the flower. So in this case the park becomes the world window and the flower becomes the view window. Similarly the whole drawing in your application is the world window whereas the part of the drawing that is shown to the end user is the view window. You have to make use of the SetViewportOrg method in your application. Please read this http://msdn.microsoft.com/en-us/library/46t66w7t.aspx[^]. Let me know if you need more help, I have done this before but unfortunately cannot find the source for it...

                Sunil

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                sunilkpv wrote:

                the park becomes the world window and the flower becomes the view window.

                Nice analogy, :thumbsup:

                One of these days I'm going to think of a really clever signature.

                S 1 Reply Last reply
                0
                • U User 9307625

                  HI, I have a application similar to auto cad, in which we can draw line, circle, etc in client area using mouse or command. Now i need to add a feature, so that the users of this application should be able to pan their drawings in client area using mouse left button. (like one in auto cad or pdf reader). I can use win32 API or MFC to achieve this. It well be great of some one can help me in this regard, with some sample code. Thanks in Advance

                  P Offline
                  P Offline
                  pasztorpisti
                  wrote on last edited by
                  #8

                  This question is a duplicate of this: http://www.codeproject.com/Messages/4336758/How-to-pan-client-area-of-a-window-using-Cplusplus.aspx[^] My detailed answer to it: http://www.codeproject.com/Messages/4337474/Re-How-to-pan-a-window-using-Cplusplus-VCplusplus-.aspx[^] I recommended this minimal program to modify to implement your panning: Using SetCapture() and ReleaseCapture() correctly (usually during a drag n' drop operation).[^] And here is the modified version of the recommended source: main.cpp:

                  #include <windows.h>
                  HINSTANCE g_hInstance = (HINSTANCE)GetModuleHandle(NULL);
                  HWND g_hMainWnd = NULL;
                  bool g_MovingMainWnd = false;
                  POINT g_OrigCursorPos;
                  POINT g_PanStartIconPos;
                  HICON g_Icon = LoadIcon(NULL, IDI_EXCLAMATION);
                  POINT g_IconPos = { 0, 0 };
                  LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                  {
                  switch (uMsg)
                  {
                  case WM_LBUTTONDOWN:
                  // here you can add extra check and decide whether to start
                  // the window move or not
                  if (GetCursorPos(&g_OrigCursorPos))
                  {
                  RECT rt;
                  GetWindowRect(hWnd, &rt);
                  g_PanStartIconPos.x = g_IconPos.x;
                  g_PanStartIconPos.y = g_IconPos.y;
                  g_MovingMainWnd = true;
                  SetCapture(hWnd);
                  SetCursor(LoadCursor(NULL, IDC_SIZEALL));
                  }
                  return 0;
                  case WM_LBUTTONUP:
                  ReleaseCapture();
                  return 0;
                  case WM_CAPTURECHANGED:
                  g_MovingMainWnd = (HWND)lParam == hWnd;
                  return 0;
                  case WM_MOUSEMOVE:
                  if (g_MovingMainWnd)
                  {
                  POINT pt;
                  if (GetCursorPos(&pt))
                  {
                  g_IconPos.x = g_PanStartIconPos.x + (pt.x - g_OrigCursorPos.x);
                  g_IconPos.y = g_PanStartIconPos.y + (pt.y - g_OrigCursorPos.y);
                  // forcing a WM_PAINT
                  InvalidateRect(hWnd, NULL, TRUE);
                  }
                  }
                  return 0;
                  case

                  U 1 Reply Last reply
                  0
                  • S Sunil P V

                    Hi, We can achieve this. Please understand the below simple concept with respect to CAD or any drafting utility. We have 2 view perspective always. 1. the world window 2. the view port window. (this is what you need to add in your program) The above can be best understood with the this example: Assume you are taking photograph of a flower in a park. Now, we focus only the flower in the view finder of your camera, and the park in this case forms the world around the flower. So in this case the park becomes the world window and the flower becomes the view window. Similarly the whole drawing in your application is the world window whereas the part of the drawing that is shown to the end user is the view window. You have to make use of the SetViewportOrg method in your application. Please read this http://msdn.microsoft.com/en-us/library/46t66w7t.aspx[^]. Let me know if you need more help, I have done this before but unfortunately cannot find the source for it...

                    Sunil

                    U Offline
                    U Offline
                    User 9307625
                    wrote on last edited by
                    #9

                    Hi Sunil, Thank you very much for your information. It will be a great help if you can help me with sample code. -Maha

                    S 1 Reply Last reply
                    0
                    • P pasztorpisti

                      This question is a duplicate of this: http://www.codeproject.com/Messages/4336758/How-to-pan-client-area-of-a-window-using-Cplusplus.aspx[^] My detailed answer to it: http://www.codeproject.com/Messages/4337474/Re-How-to-pan-a-window-using-Cplusplus-VCplusplus-.aspx[^] I recommended this minimal program to modify to implement your panning: Using SetCapture() and ReleaseCapture() correctly (usually during a drag n' drop operation).[^] And here is the modified version of the recommended source: main.cpp:

                      #include <windows.h>
                      HINSTANCE g_hInstance = (HINSTANCE)GetModuleHandle(NULL);
                      HWND g_hMainWnd = NULL;
                      bool g_MovingMainWnd = false;
                      POINT g_OrigCursorPos;
                      POINT g_PanStartIconPos;
                      HICON g_Icon = LoadIcon(NULL, IDI_EXCLAMATION);
                      POINT g_IconPos = { 0, 0 };
                      LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                      {
                      switch (uMsg)
                      {
                      case WM_LBUTTONDOWN:
                      // here you can add extra check and decide whether to start
                      // the window move or not
                      if (GetCursorPos(&g_OrigCursorPos))
                      {
                      RECT rt;
                      GetWindowRect(hWnd, &rt);
                      g_PanStartIconPos.x = g_IconPos.x;
                      g_PanStartIconPos.y = g_IconPos.y;
                      g_MovingMainWnd = true;
                      SetCapture(hWnd);
                      SetCursor(LoadCursor(NULL, IDC_SIZEALL));
                      }
                      return 0;
                      case WM_LBUTTONUP:
                      ReleaseCapture();
                      return 0;
                      case WM_CAPTURECHANGED:
                      g_MovingMainWnd = (HWND)lParam == hWnd;
                      return 0;
                      case WM_MOUSEMOVE:
                      if (g_MovingMainWnd)
                      {
                      POINT pt;
                      if (GetCursorPos(&pt))
                      {
                      g_IconPos.x = g_PanStartIconPos.x + (pt.x - g_OrigCursorPos.x);
                      g_IconPos.y = g_PanStartIconPos.y + (pt.y - g_OrigCursorPos.y);
                      // forcing a WM_PAINT
                      InvalidateRect(hWnd, NULL, TRUE);
                      }
                      }
                      return 0;
                      case

                      U Offline
                      U Offline
                      User 9307625
                      wrote on last edited by
                      #10

                      Hi, I have tried the logic you have given it is not working as I need. It will be a great help if you can provide me modified sample code to pan the client area of a window.

                      P 1 Reply Last reply
                      0
                      • U User 9307625

                        Hi, I have tried the logic you have given it is not working as I need. It will be a great help if you can provide me modified sample code to pan the client area of a window.

                        P Offline
                        P Offline
                        pasztorpisti
                        wrote on last edited by
                        #11

                        No one can help you if you don't know what you wanna achieve.

                        1 Reply Last reply
                        0
                        • L Lost User

                          sunilkpv wrote:

                          the park becomes the world window and the flower becomes the view window.

                          Nice analogy, :thumbsup:

                          One of these days I'm going to think of a really clever signature.

                          S Offline
                          S Offline
                          Sunil P V
                          wrote on last edited by
                          #12

                          THanks Richard

                          Sunil

                          1 Reply Last reply
                          0
                          • U User 9307625

                            Hi Sunil, Thank you very much for your information. It will be a great help if you can help me with sample code. -Maha

                            S Offline
                            S Offline
                            Sunil P V
                            wrote on last edited by
                            #13

                            On the lookout for the code I had developed. Will send it ASAP :)

                            Sunil

                            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