473,587 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Move Window w/Mouse

48 New Member
To download source and executable:
https://netfiles.uiuc.e du/qmitche2/www/mouse/

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. //#include <stdio.h>
  3.  
  4. // Variables
  5. HWND hwndMain; // Main window HWND
  6. // Functions
  7. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  8.  
  9. int WINAPI 
  10. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
  11.         int nCmdShow)
  12. {
  13.     MSG msg;
  14.     WNDCLASSEX wcx;
  15.     {
  16.     // Fill in the WNDCLASS Struct
  17.     wcx.cbSize = sizeof(WNDCLASSEX);
  18.     wcx.style = CS_DBLCLKS;
  19.     wcx.lpfnWndProc = MainWndProc;
  20.     wcx.cbClsExtra = 0;
  21.     wcx.cbWndExtra = 0;
  22.     wcx.hInstance = hInstance;
  23.     wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  24.     wcx.hCursor = LoadCursor (NULL, IDC_ARROW);
  25.     wcx.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  26.     wcx.lpszMenuName = NULL;
  27.     wcx.lpszClassName = "Draw";
  28.     wcx.hIconSm = NULL;
  29.     }
  30.     if(!RegisterClassEx(&wcx))
  31.         return 0;
  32.  
  33.     hwndMain = CreateWindowEx(0,
  34.                             "Draw",
  35.                             "Draw Example Application",
  36.                             WS_OVERLAPPEDWINDOW,
  37.                             CW_USEDEFAULT,
  38.                             CW_USEDEFAULT,
  39.                             400,
  40.                             300,
  41.                             NULL,
  42.                             NULL,
  43.                             hInstance,
  44.                             NULL);
  45.  
  46.     ShowWindow(hwndMain,nCmdShow);
  47.  
  48.     while(GetMessage(&msg,NULL,0,0))
  49.     {
  50.         TranslateMessage(&msg);
  51.         DispatchMessage(&msg);
  52.     }
  53.     return msg.wParam;
  54. }
  55.  
  56. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  57. {
  58.     POINT thisPoint; // Stores the current point
  59.     static bool drawing = false;
  60.  
  61.  
  62.     switch(msg)
  63.     {
  64.     case WM_PAINT:
  65.     {
  66.         PAINTSTRUCT Ps;
  67.         HDC hDC=BeginPaint(hwnd, &Ps);
  68.  
  69.         Rectangle(hDC, 0, 0, 100, 100);
  70.  
  71.         EndPaint(hwnd, &Ps);
  72.     }
  73.     break;
  74.     case WM_DESTROY:
  75.         PostQuitMessage(0);
  76.         break;
  77.     case WM_LBUTTONDOWN:
  78.         // Says, We've begun drawing
  79.         drawing = true;
  80.  
  81.         // Capture mouse
  82.         //SetCapture(hwnd);
  83.         break;
  84.     case WM_LBUTTONUP:
  85.         drawing = false;
  86.         //ReleaseCapture();
  87.         break;
  88.     case WM_MOUSEMOVE:
  89.         if (drawing)
  90.         {
  91.             thisPoint.x = LOWORD(lParam);
  92.             thisPoint.y = HIWORD(lParam);
  93.             //printf("x:%d ",thisPoint.x);
  94.             //printf("y:%d\n",thisPoint.y);
  95.  
  96.             SetWindowPos(hwnd,NULL,
  97.                 thisPoint.x,
  98.                 thisPoint.y,
  99.                 0,
  100.                 0,
  101.                 SWP_NOSIZE);
  102.             UpdateWindow(hwnd);
  103.         }
  104.         break;
  105.     default:
  106.         return DefWindowProc(hwnd,msg,wParam,lParam);
  107.     }
  108.     return 0;
  109. }
  110.  
Why does it move the window in two different spots at (almost) the same time?

I want the window to move with my mouse when I hold on the left mouse button.

Any ideas would be nice.

I tried the setcapture function, and i didn't see it affect anything.
(compile with gdi32 )
Jul 2 '07 #1
2 3745
qhimq
48 New Member
I figured it out:

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. // Variables
  5. HWND hwndMain; // Main window HWND
  6. // Functions
  7. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  8.  
  9. int WINAPI 
  10. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
  11.         int nCmdShow)
  12. {
  13.     MSG msg;
  14.     WNDCLASSEX wcx;
  15.     {
  16.     // Fill in the WNDCLASS Struct
  17.     wcx.cbSize = sizeof(WNDCLASSEX);
  18.     wcx.style = CS_DBLCLKS;
  19.     wcx.lpfnWndProc = MainWndProc;
  20.     wcx.cbClsExtra = 0;
  21.     wcx.cbWndExtra = 0;
  22.     wcx.hInstance = hInstance;
  23.     wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  24.     wcx.hCursor = LoadCursor (NULL, IDC_ARROW);
  25.     wcx.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  26.     wcx.lpszMenuName = NULL;
  27.     wcx.lpszClassName = "Draw";
  28.     wcx.hIconSm = NULL;
  29.     }
  30.     if(!RegisterClassEx(&wcx))
  31.         return 0;
  32.  
  33.     hwndMain = CreateWindowEx(0,
  34.                             "Draw",
  35.                             "Draw Example Application",
  36.                             WS_OVERLAPPEDWINDOW,
  37.                             CW_USEDEFAULT,
  38.                             CW_USEDEFAULT,
  39.                             400,
  40.                             300,
  41.                             NULL,
  42.                             NULL,
  43.                             hInstance,
  44.                             NULL);
  45.  
  46.     ShowWindow(hwndMain,nCmdShow);
  47.  
  48.     while(GetMessage(&msg,NULL,0,0))
  49.     {
  50.         TranslateMessage(&msg);
  51.         DispatchMessage(&msg);
  52.     }
  53.     return msg.wParam;
  54. }
  55.  
  56. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  57. {
  58.     POINT thisPoint; // Stores the current point
  59.     POINT oldPoint;
  60.     static bool drawing = false;
  61.  
  62.  
  63.     switch(msg)
  64.     {
  65.     case WM_PAINT:
  66.     {
  67.         PAINTSTRUCT Ps;
  68.         HDC hDC=BeginPaint(hwnd, &Ps);
  69.  
  70.         Rectangle(hDC, 0, 0, 100, 100);
  71.  
  72.         EndPaint(hwnd, &Ps);
  73.     }
  74.     break;
  75.     case WM_DESTROY:
  76.         PostQuitMessage(0);
  77.         break;
  78.     case WM_LBUTTONDOWN:
  79.         // Says, We've begun drawing
  80.         drawing = true;
  81.         oldPoint = thisPoint;
  82.         // Capture mouse
  83.         //SetCapture(hwnd);
  84.         break;
  85.     case WM_LBUTTONUP:
  86.         drawing = false;
  87.         //ReleaseCapture();
  88.         break;
  89.     case WM_MOUSEMOVE:
  90.         if (drawing && ((MK_LBUTTON & wParam) == MK_LBUTTON))
  91.         {
  92.             thisPoint.x = LOWORD(lParam);
  93.             thisPoint.y = HIWORD(lParam);
  94.  
  95.             printf("x:%d ",thisPoint.x);
  96.             printf("y:%d\n",thisPoint.y);
  97.  
  98.             RECT rc;
  99.             GetClientRect(hwnd,&rc);
  100.  
  101.             POINT pt;
  102.             pt.x=rc.left-4+(thisPoint.x-oldPoint.x);
  103.             pt.y=rc.top-23+(thisPoint.y-oldPoint.y);
  104.             ClientToScreen(hwnd, &pt);
  105.  
  106.             POINT half;
  107.             half.x=(rc.right-rc.left)/2;
  108.             half.y=(rc.bottom-rc.top)/2;
  109.             SetWindowPos(hwnd,NULL,
  110.                 pt.x-half.x,
  111.                 pt.y-half.y,
  112.                 0,
  113.                 0,
  114.                 SWP_NOSIZE);
  115.             UpdateWindow(hwnd);
  116.  
  117.         }
  118.         break;
  119.     default:
  120.         return DefWindowProc(hwnd,msg,wParam,lParam);
  121.     }
  122.     return 0;
  123. }
  124.  
  125.  
Jul 2 '07 #2
niskin
109 New Member
Well done, it's always great to figure out things ourselves, for both the learning experience and the sense of achievement.
Jul 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6453
by: meganrobertson22 | last post by:
Hi- I'd like to try to have text displayed when I move my mouse over a command button. For example: let's say I have a form with 3 different command buttons. Each command button launches a different report. For example:
1
7646
by: suhas | last post by:
Hi, I have an MDI app that allows user to create flowcharts. User can drag flowchart objects (squares, rectangles, elipses, etc) from the toolbar and put on a form. They should be able to move the objects around on the form. This is where the problem is, I have implemented this using the mouse_down, mouse_up and mouse_move events of the...
1
2898
by: Jay | last post by:
I need to be able to move a Panel on a windows Form at run time using the mouse. I have tried adding a MouseDown event handler to the Panel and then within the MouseDown handler adding a MouseMove event to the Panel and then setting the Panel's Location to a new Point(x, y) with x and y set to the mouse's x and y, but this does not work...
3
9976
by: Logan Mckinley | last post by:
I need to be able to detect mouse movement even when it is not over my application. I can get the mouse cords using MousePosition but I am not sure if there is an event that hits my program when the mouse is not over my program. One idea i had was make a child form that was transparent and use the MouseMove event to keep that transparent...
2
6295
by: Jurate | last post by:
I want my window not to have title bar, but then the problem arises, how to move such a window. Is it possible to make window move by moving mouse anywhere on that window? -- Jurate
5
1635
by: Boni | last post by:
Dear all, picturebox mouse move seems to start new thread each time it is called. At least I see in debuger many threads. ---Non user code-- Picturebox.MouseMove This seems to spoil my algorithm. SyncLock Me Do() EndSyncloc
5
4726
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event gets triggered and the double click is not identified at all. Is there any way I can invoke the double click when the mouse move also exists?
6
7253
by: Rob | last post by:
This is a curious problem. It seems like it should be quite easy. Of course a timer is used to determine when form should be closed, but how do you consistently reset the timer when the mouse is moved over the form. Normally I would use the MouseMove event to know when the mouse moves over the form and use that to reset the timer. ...
8
1888
by: Doron Farber | last post by:
Hi All, How can I Move the Server explorer window to the left. When ever I double click on the Server explorer top window it goes to the bottom of the IDE. Thanks, Doron
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.