Need help to pan client area of a window using win32 api / mfc
-
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
-
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
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 yourOnPaint()
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.
-
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 yourOnPaint()
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.
ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.
-
ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.
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.
-
ok, after getting x and y coordinates which API should I use to reset the x and y coordinates? Thank you.
-
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
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
-
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
-
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
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 -
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
Hi Sunil, Thank you very much for your information. It will be a great help if you can help me with sample code. -Maha
-
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;
caseHi, 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.
-
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.
No one can help you if you don't know what you wanna achieve.
-
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.
-
Hi Sunil, Thank you very much for your information. It will be a great help if you can help me with sample code. -Maha