鱼C论坛

 找回密码
 立即注册
查看: 4032|回复: 12

如果通过鼠标拖动在客户区上画按钮?

[复制链接]
发表于 2014-10-27 15:43:48 | 显示全部楼层 |阅读模式
100鱼币
本帖最后由 拈花小仙 于 2014-10-27 15:44 编辑
  1. #include <Windows.h>
  2. #include <iostream>

  3. HINSTANCE g_hInst   = NULL;
  4. HANDLE    g_hStdOut = NULL;
  5. int                  g_nXPos   = 0;
  6. int       g_nYPos   = 0;
  7. int                  g_nX1Rect = 0;
  8. int                  g_nY1Rect = 0;
  9. int       g_nX2Rect = 0;
  10. int       g_nY2Rect = 0;

  11. void PrintLog( LPSTR pszLog )
  12. {
  13.         WriteConsole( g_hStdOut,
  14.                 pszLog, strlen(pszLog), NULL, NULL );
  15. }

  16. void OnPaint( HWND hWnd, UINT nMsg,
  17.           WPARAM wParam, LPARAM lParam )
  18. {
  19.         PAINTSTRUCT ps = { 0 };
  20.         HDC hDC = BeginPaint( hWnd, &ps );

  21.         CHAR szText[] = "Hello Mouse";
  22.         TextOut( hDC, g_nXPos, g_nYPos, szText, strlen(szText) );

  23.         Rectangle( hDC, g_nX1Rect, g_nY1Rect,g_nX2Rect, g_nY2Rect );

  24.     //CreateWindowEx(0,TEXT("BUTTON"),TEXT("button"),WS_CHILD|WS_VISIBLE,g_nX1Rect,g_nY1Rect,
  25.     //g_nX2Rect,g_nY2Rect,hWnd,NULL,g_hInst,NULL); </p><p>  //现在能矩形,如何在客户名画按钮呢?

  26.         EndPaint( hWnd, &ps );
  27. }

  28. LRESULT CALLBACK WndProc( HWND hWnd,
  29.                                                   UINT nMsg,
  30.                                                   WPARAM wParam,
  31.                                                   LPARAM lParam )
  32. {
  33.         switch( nMsg )
  34.         {
  35.         case WM_PAINT:
  36.                 OnPaint( hWnd, nMsg, wParam, lParam );
  37.                 break;
  38.         case WM_LBUTTONDOWN:
  39.                 {
  40.                         PrintLog( "WM_LBUTTONDOWN\n" );
  41.                         g_nX1Rect = LOWORD( lParam );
  42.                         g_nY1Rect = HIWORD( lParam );
  43.                 }
  44.                 break;
  45.         case WM_LBUTTONUP:
  46.                 {
  47.                         PrintLog( "WM_LBUTTONUP\n" );
  48.                         g_nX2Rect = LOWORD( lParam );
  49.                         g_nY2Rect = HIWORD( lParam );
  50.                         InvalidateRect( hWnd, NULL, TRUE );
  51.                 }
  52.                 break;
  53.         case WM_RBUTTONDOWN:
  54.                 PrintLog( "WM_RBUTTONDOWN\n" );
  55.                 break;
  56.         case WM_RBUTTONUP:
  57.                 PrintLog( "WM_RBUTTONUP\n" );
  58.                 break;
  59.         case WM_MOUSEMOVE:
  60.                 {
  61.                         int nX = LOWORD(lParam);
  62.                         int nY = HIWORD(lParam);
  63.                         POINT ptScreen = { 0 };
  64.                         ptScreen.x = nX;
  65.                         ptScreen.y = nY;
  66.                         ClientToScreen( hWnd, &ptScreen );

  67.                         CHAR szText[260] = { 0 };
  68.                         sprintf( szText,
  69.                                 "WM_MOUSEMOVE: X=%d(%d),Y=%d(%d)\n",
  70.                                 nX, ptScreen.x, nY, ptScreen.y );
  71.                         PrintLog( szText );
  72.                         if( wParam & MK_CONTROL )
  73.                         {
  74.                                 PrintLog( "WM_MOUSEMOVE: MK_CONTROL\n" );
  75.                         }
  76.                         if( wParam & MK_LBUTTON )
  77.                         {
  78.                                 PrintLog( "WM_MOUSEMOVE: MK_LBUTTON\n" );
  79.                         }

  80.                         g_nXPos = LOWORD(lParam);
  81.                         g_nYPos = HIWORD(lParam);
  82.                         InvalidateRect( hWnd, NULL, TRUE );
  83.                 }
  84.                 break;
  85.         case WM_LBUTTONDBLCLK:
  86.                 PrintLog( "WM_LBUTTONDBLCLK\n" );
  87.                 break;
  88.         case WM_RBUTTONDBLCLK:
  89.                 PrintLog( "WM_RBUTTONDBLCLK\n" );
  90.                 break;
  91.         case WM_MOUSEWHEEL:
  92.                 {
  93.                         short nDetla = HIWORD( wParam );
  94.                         int   nX     = LOWORD( lParam );
  95.                         int   nY     = HIWORD( lParam );
  96.                         CHAR szText[260] = { 0 };
  97.                         sprintf( szText,
  98.                                 "WM_MOUSEWHEEL: Detla=%d, X=%d,Y=%d\n",
  99.                                 nDetla, nX, nY );
  100.                         PrintLog( szText );
  101.                 }
  102.                 break;
  103.         case WM_DESTROY:
  104.                 PostQuitMessage( 0 );
  105.                 break;
  106.         }
  107.         return DefWindowProc( hWnd, nMsg,
  108.                 wParam, lParam );
  109. }

  110. BOOL RegisterWnd( LPSTR pszClassName )
  111. {
  112.         WNDCLASSEX wce = { 0 };
  113.         wce.cbSize                  = sizeof( wce );
  114.         wce.cbClsExtra          = 0;
  115.         wce.cbWndExtra    = 0;
  116.         wce.hbrBackground = HBRUSH(COLOR_WINDOW);
  117.         wce.hCursor                  = NULL;
  118.         wce.hIcon                  = NULL;
  119.         wce.hIconSm       = NULL;
  120.         wce.hInstance          = g_hInst;
  121.         wce.lpfnWndProc   = WndProc;
  122.         wce.lpszClassName = pszClassName;
  123.         wce.lpszMenuName  = NULL;
  124.         wce.style         = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
  125.         ATOM nAtom = RegisterClassEx( &wce );
  126.         if( 0 == nAtom )
  127.         {
  128.                 return FALSE;
  129.         }
  130.         return TRUE;
  131. }

  132. HWND CreateWnd( LPSTR pszClassName )
  133. {
  134.         HWND hWnd = CreateWindowEx( 0,
  135.                 pszClassName, "MyWnd",
  136.                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  137.                 CW_USEDEFAULT, CW_USEDEFAULT,
  138.                 CW_USEDEFAULT, NULL, NULL,
  139.                 g_hInst, NULL );
  140.         return hWnd;
  141. }

  142. void DisplayWnd( HWND hWnd )
  143. {
  144.         ShowWindow( hWnd, SW_SHOW );
  145.         UpdateWindow( hWnd );
  146. }

  147. void Message( )
  148. {
  149.         MSG msg = { 0 };
  150.         while( GetMessage( &msg, NULL, 0, 0 ) )
  151.         {
  152.                 TranslateMessage( &msg );
  153.                 DispatchMessage( &msg );
  154.         }
  155. }

  156. void NewConsole( )
  157. {
  158.         AllocConsole( );
  159.         g_hStdOut =
  160.                 GetStdHandle( STD_OUTPUT_HANDLE );
  161. }

  162. int APIENTRY WinMain(HINSTANCE hInstance,
  163.                      HINSTANCE hPrevInstance,
  164.                      LPSTR     lpCmdLine,
  165.                      int       nCmdShow)
  166. {
  167.         NewConsole( );
  168.          g_hInst = hInstance;
  169.         RegisterWnd( "MyWnd" );
  170.         HWND hWnd = CreateWnd( "MyWnd" );
  171.         DisplayWnd( hWnd );
  172.         Message( );

  173.         return 0;
  174. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-27 15:43:49 | 显示全部楼层
  1. #include <Windows.h>

  2. #include <iostream>


  3. HINSTANCE g_hInst   = NULL;

  4. HANDLE    g_hStdOut = NULL;

  5. HWND          g_hBtn = NULL;//按钮句柄

  6. int                  g_nXPos   = 0;

  7. int       g_nYPos   = 0;

  8. int                  g_nX1Rect = 0;

  9. int                  g_nY1Rect = 0;

  10. int       g_nX2Rect = 0;

  11. int       g_nY2Rect = 0;


  12. void PrintLog( LPSTR pszLog )

  13. {

  14.         WriteConsole( g_hStdOut,

  15.                 pszLog, strlen(pszLog), NULL, NULL );

  16. }


  17. void OnPaint( HWND hWnd, UINT nMsg,

  18.           WPARAM wParam, LPARAM lParam )

  19. {

  20.         PAINTSTRUCT ps = { 0 };

  21.         HDC hDC = BeginPaint( hWnd, &ps );


  22.         //CHAR szText[] = "Hello Mouse";

  23.         //TextOut( hDC, g_nXPos, g_nYPos, szText, strlen(szText) );


  24.         //Rectangle( hDC, g_nX1Rect, g_nY1Rect,g_nX2Rect, g_nY2Rect );


  25.     //CreateWindowEx(0,TEXT("BUTTON"),TEXT("button"),WS_CHILD|WS_VISIBLE,g_nX1Rect,g_nY1Rect,

  26.     //g_nX2Rect,g_nY2Rect,hWnd,NULL,g_hInst,NULL); </p><p>  //现在能矩形,如何在客户名画按钮呢?
  27.                 MoveWindow( g_hBtn, g_nX1Rect, g_nY1Rect, g_nX2Rect - g_nX1Rect, g_nY2Rect - g_nY1Rect, TRUE );


  28.         EndPaint( hWnd, &ps );

  29. }


  30. LRESULT CALLBACK WndProc( HWND hWnd,

  31.                                                   UINT nMsg,

  32.                                                   WPARAM wParam,

  33.                                                   LPARAM lParam )

  34. {

  35.         switch( nMsg )

  36.         {
  37.                 //在这里创建按钮
  38.                 case WM_CREATE:
  39.                         g_hBtn = CreateWindowEx(0,TEXT("BUTTON"),TEXT("button"),WS_CHILD|WS_VISIBLE,g_nX1Rect,g_nY1Rect,
  40.                                         g_nX2Rect,g_nY2Rect,hWnd,NULL,g_hInst,NULL);
  41.                         break;

  42.         case WM_PAINT:

  43.                 OnPaint( hWnd, nMsg, wParam, lParam );

  44.                 break;

  45.         case WM_LBUTTONDOWN:

  46.                 {

  47.                         PrintLog( "WM_LBUTTONDOWN\n" );

  48.                         g_nX1Rect = LOWORD( lParam );

  49.                         g_nY1Rect = HIWORD( lParam );

  50.                 }

  51.                 break;

  52.         case WM_LBUTTONUP:

  53.                 {

  54.                         PrintLog( "WM_LBUTTONUP\n" );

  55.                         g_nX2Rect = LOWORD( lParam );

  56.                         g_nY2Rect = HIWORD( lParam );

  57.                         InvalidateRect( hWnd, NULL, TRUE );

  58.                 }

  59.                 break;

  60.         case WM_RBUTTONDOWN:

  61.                 PrintLog( "WM_RBUTTONDOWN\n" );

  62.                 break;

  63.         case WM_RBUTTONUP:

  64.                 PrintLog( "WM_RBUTTONUP\n" );

  65.                 break;

  66.         case WM_MOUSEMOVE:

  67.                 {

  68.                         int nX = LOWORD(lParam);

  69.                         int nY = HIWORD(lParam);

  70.                         POINT ptScreen = { 0 };

  71.                         ptScreen.x = nX;

  72.                         ptScreen.y = nY;

  73.                         ClientToScreen( hWnd, &ptScreen );


  74.                         CHAR szText[260] = { 0 };

  75.                         sprintf( szText,
  76.                                 "WM_MOUSEMOVE: X=%d(%d),Y=%d(%d)\n",

  77.                                 nX, ptScreen.x, nY, ptScreen.y );

  78.                         PrintLog( szText );

  79.                         if( wParam & MK_CONTROL )

  80.                         {

  81.                                 PrintLog( "WM_MOUSEMOVE: MK_CONTROL\n" );

  82.                         }

  83.                         if( wParam & MK_LBUTTON )

  84.                         {

  85.                                 PrintLog( "WM_MOUSEMOVE: MK_LBUTTON\n" );

  86.                         }


  87.                         g_nXPos = LOWORD(lParam);

  88.                         g_nYPos = HIWORD(lParam);

  89.                         InvalidateRect( hWnd, NULL, TRUE );

  90.                 }

  91.                 break;

  92.         case WM_LBUTTONDBLCLK:

  93.                 PrintLog( "WM_LBUTTONDBLCLK\n" );

  94.                 break;

  95.         case WM_RBUTTONDBLCLK:

  96.                 PrintLog( "WM_RBUTTONDBLCLK\n" );

  97.                 break;

  98.         case WM_MOUSEWHEEL:

  99.                 {

  100.                         short nDetla = HIWORD( wParam );

  101.                         int   nX     = LOWORD( lParam );

  102.                         int   nY     = HIWORD( lParam );

  103.                         CHAR szText[260] = { 0 };

  104.                         sprintf( szText,
  105.                                 "WM_MOUSEWHEEL: Detla=%d, X=%d,Y=%d\n",

  106.                                 nDetla, nX, nY );

  107.                         PrintLog( szText );

  108.                 }

  109.                 break;

  110.         case WM_DESTROY:

  111.                 PostQuitMessage( 0 );

  112.                 break;

  113.         }

  114.         return DefWindowProc( hWnd, nMsg,

  115.                 wParam, lParam );

  116. }


  117. BOOL RegisterWnd( LPSTR pszClassName )

  118. {

  119.         WNDCLASSEX wce = { 0 };

  120.         wce.cbSize                  = sizeof( wce );

  121.         wce.cbClsExtra          = 0;

  122.         wce.cbWndExtra    = 0;

  123.         wce.hbrBackground = HBRUSH(COLOR_WINDOW);

  124.         wce.hCursor                  = NULL;

  125.         wce.hIcon                  = NULL;

  126.         wce.hIconSm       = NULL;

  127.         wce.hInstance          = g_hInst;

  128.         wce.lpfnWndProc   = WndProc;

  129.         wce.lpszClassName = pszClassName;

  130.         wce.lpszMenuName  = NULL;

  131.         wce.style         = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;

  132.         ATOM nAtom = RegisterClassEx( &wce );

  133.         if( 0 == nAtom )

  134.         {

  135.                 return FALSE;

  136.         }

  137.         return TRUE;

  138. }


  139. HWND CreateWnd( LPSTR pszClassName )

  140. {

  141.         HWND hWnd = CreateWindowEx( 0,

  142.                 pszClassName, "MyWnd",
  143.                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,

  144.                 CW_USEDEFAULT, CW_USEDEFAULT,
  145.                 CW_USEDEFAULT, NULL, NULL,
  146.                 g_hInst, NULL );

  147.         return hWnd;

  148. }


  149. void DisplayWnd( HWND hWnd )

  150. {

  151.         ShowWindow( hWnd, SW_SHOW );

  152.         UpdateWindow( hWnd );

  153. }


  154. void Message( )

  155. {

  156.         MSG msg = { 0 };

  157.         while( GetMessage( &msg, NULL, 0, 0 ) )

  158.         {

  159.                 TranslateMessage( &msg );

  160.                 DispatchMessage( &msg );

  161.         }

  162. }


  163. void NewConsole( )

  164. {

  165.         AllocConsole( );

  166.         g_hStdOut =
  167.                 GetStdHandle( STD_OUTPUT_HANDLE );

  168. }


  169. int APIENTRY WinMain(HINSTANCE hInstance,

  170.                      HINSTANCE hPrevInstance,

  171.                      LPSTR     lpCmdLine,

  172.                      int       nCmdShow)

  173. {

  174.         NewConsole( );

  175.          g_hInst = hInstance;

  176.         RegisterWnd( "MyWnd" );

  177.         HWND hWnd = CreateWnd( "MyWnd" );

  178.         DisplayWnd( hWnd );

  179.         Message( );


  180.         return 0;

  181. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
拈花小仙 + 5 + 5 谢谢导师 ^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-27 17:49:48 | 显示全部楼层
这是我从书上抄的例子,不知道对你有没有帮助

Win32Project10.rar

14.09 KB, 下载次数: 1

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
拈花小仙 + 5 + 5 热爱鱼C^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-27 19:37:12 | 显示全部楼层
流行语 发表于 2014-10-27 17:49
这是我从书上抄的例子,不知道对你有没有帮助

语语这个解决我另外一个问题哈,不过我想画的是按钮,BUTTON,就像那些集成的IDE一样,可以靠拖动画出按钮来。帮帮我吧~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-27 19:41:20 | 显示全部楼层
就是在WM_CREATE中创建按钮,在原来画矩形的地方移动按钮
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-28 08:02:53 | 显示全部楼层
我现在还没有思路啊,让我想想!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2014-10-30 15:45:46 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-30 21:20:03 | 显示全部楼层
拈花小仙 发表于 2014-10-27 19:37
语语这个解决我另外一个问题哈,不过我想画的是按钮,BUTTON,就像那些集成的IDE一样,可以靠拖 ...

我没有做出来啊:cry
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-30 21:26:44 | 显示全部楼层

语语~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-30 21:28:06 | 显示全部楼层

不好意思啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-30 21:33:54 | 显示全部楼层

我才不好意思呢,你帮我这么多,可是画的不是画钮,要不早采纳啦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-27 11:26:14 | 显示全部楼层
学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-9-2 09:25:44 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 17:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表