473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Move Window w/Mouse

48
To download source and executable:
https://netfiles.uiuc.edu/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 3725
qhimq
48
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 100+
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
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...
1
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...
1
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...
3
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...
2
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
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...
5
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...
6
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...
8
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.