拈花小仙 发表于 2014-10-27 15:43:48

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

本帖最后由 拈花小仙 于 2014-10-27 15:44 编辑

#include <Windows.h>
#include <iostream>

HINSTANCE g_hInst   = NULL;
HANDLE    g_hStdOut = NULL;
int                  g_nXPos   = 0;
int       g_nYPos   = 0;
int                  g_nX1Rect = 0;
int                  g_nY1Rect = 0;
int       g_nX2Rect = 0;
int       g_nY2Rect = 0;

void PrintLog( LPSTR pszLog )
{
      WriteConsole( g_hStdOut,
                pszLog, strlen(pszLog), NULL, NULL );
}

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

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

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

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

      EndPaint( hWnd, &ps );
}

LRESULT CALLBACK WndProc( HWND hWnd,
                                                UINT nMsg,
                                                WPARAM wParam,
                                                LPARAM lParam )
{
      switch( nMsg )
      {
      case WM_PAINT:
                OnPaint( hWnd, nMsg, wParam, lParam );
                break;
      case WM_LBUTTONDOWN:
                {
                        PrintLog( "WM_LBUTTONDOWN\n" );
                        g_nX1Rect = LOWORD( lParam );
                        g_nY1Rect = HIWORD( lParam );
                }
                break;
      case WM_LBUTTONUP:
                {
                        PrintLog( "WM_LBUTTONUP\n" );
                        g_nX2Rect = LOWORD( lParam );
                        g_nY2Rect = HIWORD( lParam );
                        InvalidateRect( hWnd, NULL, TRUE );
                }
                break;
      case WM_RBUTTONDOWN:
                PrintLog( "WM_RBUTTONDOWN\n" );
                break;
      case WM_RBUTTONUP:
                PrintLog( "WM_RBUTTONUP\n" );
                break;
      case WM_MOUSEMOVE:
                {
                        int nX = LOWORD(lParam);
                        int nY = HIWORD(lParam);
                        POINT ptScreen = { 0 };
                        ptScreen.x = nX;
                        ptScreen.y = nY;
                        ClientToScreen( hWnd, &ptScreen );

                        CHAR szText = { 0 };
                        sprintf( szText,
                              "WM_MOUSEMOVE: X=%d(%d),Y=%d(%d)\n",
                              nX, ptScreen.x, nY, ptScreen.y );
                        PrintLog( szText );
                        if( wParam & MK_CONTROL )
                        {
                              PrintLog( "WM_MOUSEMOVE: MK_CONTROL\n" );
                        }
                        if( wParam & MK_LBUTTON )
                        {
                              PrintLog( "WM_MOUSEMOVE: MK_LBUTTON\n" );
                        }

                        g_nXPos = LOWORD(lParam);
                        g_nYPos = HIWORD(lParam);
                        InvalidateRect( hWnd, NULL, TRUE );
                }
                break;
      case WM_LBUTTONDBLCLK:
                PrintLog( "WM_LBUTTONDBLCLK\n" );
                break;
      case WM_RBUTTONDBLCLK:
                PrintLog( "WM_RBUTTONDBLCLK\n" );
                break;
      case WM_MOUSEWHEEL:
                {
                        short nDetla = HIWORD( wParam );
                        int   nX   = LOWORD( lParam );
                        int   nY   = HIWORD( lParam );
                        CHAR szText = { 0 };
                        sprintf( szText,
                              "WM_MOUSEWHEEL: Detla=%d, X=%d,Y=%d\n",
                              nDetla, nX, nY );
                        PrintLog( szText );
                }
                break;
      case WM_DESTROY:
                PostQuitMessage( 0 );
                break;
      }
      return DefWindowProc( hWnd, nMsg,
                wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
      WNDCLASSEX wce = { 0 };
      wce.cbSize                  = sizeof( wce );
      wce.cbClsExtra          = 0;
      wce.cbWndExtra    = 0;
      wce.hbrBackground = HBRUSH(COLOR_WINDOW);
      wce.hCursor                  = NULL;
      wce.hIcon                  = NULL;
      wce.hIconSm       = NULL;
      wce.hInstance          = g_hInst;
      wce.lpfnWndProc   = WndProc;
      wce.lpszClassName = pszClassName;
      wce.lpszMenuName= NULL;
      wce.style         = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
      ATOM nAtom = RegisterClassEx( &wce );
      if( 0 == nAtom )
      {
                return FALSE;
      }
      return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
      HWND hWnd = CreateWindowEx( 0,
                pszClassName, "MyWnd",
                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
                CW_USEDEFAULT, CW_USEDEFAULT,
                CW_USEDEFAULT, NULL, NULL,
                g_hInst, NULL );
      return hWnd;
}

void DisplayWnd( HWND hWnd )
{
      ShowWindow( hWnd, SW_SHOW );
      UpdateWindow( hWnd );
}

void Message( )
{
      MSG msg = { 0 };
      while( GetMessage( &msg, NULL, 0, 0 ) )
      {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
      }
}

void NewConsole( )
{
      AllocConsole( );
      g_hStdOut =
                GetStdHandle( STD_OUTPUT_HANDLE );
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR   lpCmdLine,
                     int       nCmdShow)
{
      NewConsole( );
         g_hInst = hInstance;
      RegisterWnd( "MyWnd" );
      HWND hWnd = CreateWnd( "MyWnd" );
      DisplayWnd( hWnd );
      Message( );

      return 0;
}

仰望天上的光 发表于 2014-10-27 15:43:49

#include <Windows.h>

#include <iostream>


HINSTANCE g_hInst   = NULL;

HANDLE    g_hStdOut = NULL;

HWND          g_hBtn = NULL;//按钮句柄

int                  g_nXPos   = 0;

int       g_nYPos   = 0;

int                  g_nX1Rect = 0;

int                  g_nY1Rect = 0;

int       g_nX2Rect = 0;

int       g_nY2Rect = 0;


void PrintLog( LPSTR pszLog )

{

      WriteConsole( g_hStdOut,

                pszLog, strlen(pszLog), NULL, NULL );

}


void OnPaint( HWND hWnd, UINT nMsg,

          WPARAM wParam, LPARAM lParam )

{

      PAINTSTRUCT ps = { 0 };

      HDC hDC = BeginPaint( hWnd, &ps );


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

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


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


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

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


      EndPaint( hWnd, &ps );

}


LRESULT CALLBACK WndProc( HWND hWnd,

                                                UINT nMsg,

                                                WPARAM wParam,

                                                LPARAM lParam )

{

      switch( nMsg )

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

      case WM_PAINT:

                OnPaint( hWnd, nMsg, wParam, lParam );

                break;

      case WM_LBUTTONDOWN:

                {

                        PrintLog( "WM_LBUTTONDOWN\n" );

                        g_nX1Rect = LOWORD( lParam );

                        g_nY1Rect = HIWORD( lParam );

                }

                break;

      case WM_LBUTTONUP:

                {

                        PrintLog( "WM_LBUTTONUP\n" );

                        g_nX2Rect = LOWORD( lParam );

                        g_nY2Rect = HIWORD( lParam );

                        InvalidateRect( hWnd, NULL, TRUE );

                }

                break;

      case WM_RBUTTONDOWN:

                PrintLog( "WM_RBUTTONDOWN\n" );

                break;

      case WM_RBUTTONUP:

                PrintLog( "WM_RBUTTONUP\n" );

                break;

      case WM_MOUSEMOVE:

                {

                        int nX = LOWORD(lParam);

                        int nY = HIWORD(lParam);

                        POINT ptScreen = { 0 };

                        ptScreen.x = nX;

                        ptScreen.y = nY;

                        ClientToScreen( hWnd, &ptScreen );


                        CHAR szText = { 0 };

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

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

                        PrintLog( szText );

                        if( wParam & MK_CONTROL )

                        {

                              PrintLog( "WM_MOUSEMOVE: MK_CONTROL\n" );

                        }

                        if( wParam & MK_LBUTTON )

                        {

                              PrintLog( "WM_MOUSEMOVE: MK_LBUTTON\n" );

                        }


                        g_nXPos = LOWORD(lParam);

                        g_nYPos = HIWORD(lParam);

                        InvalidateRect( hWnd, NULL, TRUE );

                }

                break;

      case WM_LBUTTONDBLCLK:

                PrintLog( "WM_LBUTTONDBLCLK\n" );

                break;

      case WM_RBUTTONDBLCLK:

                PrintLog( "WM_RBUTTONDBLCLK\n" );

                break;

      case WM_MOUSEWHEEL:

                {

                        short nDetla = HIWORD( wParam );

                        int   nX   = LOWORD( lParam );

                        int   nY   = HIWORD( lParam );

                        CHAR szText = { 0 };

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

                              nDetla, nX, nY );

                        PrintLog( szText );

                }

                break;

      case WM_DESTROY:

                PostQuitMessage( 0 );

                break;

      }

      return DefWindowProc( hWnd, nMsg,

                wParam, lParam );

}


BOOL RegisterWnd( LPSTR pszClassName )

{

      WNDCLASSEX wce = { 0 };

      wce.cbSize                  = sizeof( wce );

      wce.cbClsExtra          = 0;

      wce.cbWndExtra    = 0;

      wce.hbrBackground = HBRUSH(COLOR_WINDOW);

      wce.hCursor                  = NULL;

      wce.hIcon                  = NULL;

      wce.hIconSm       = NULL;

      wce.hInstance          = g_hInst;

      wce.lpfnWndProc   = WndProc;

      wce.lpszClassName = pszClassName;

      wce.lpszMenuName= NULL;

      wce.style         = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;

      ATOM nAtom = RegisterClassEx( &wce );

      if( 0 == nAtom )

      {

                return FALSE;

      }

      return TRUE;

}


HWND CreateWnd( LPSTR pszClassName )

{

      HWND hWnd = CreateWindowEx( 0,

                pszClassName, "MyWnd",
                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,

                CW_USEDEFAULT, CW_USEDEFAULT,
                CW_USEDEFAULT, NULL, NULL,
                g_hInst, NULL );

      return hWnd;

}


void DisplayWnd( HWND hWnd )

{

      ShowWindow( hWnd, SW_SHOW );

      UpdateWindow( hWnd );

}


void Message( )

{

      MSG msg = { 0 };

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

      {

                TranslateMessage( &msg );

                DispatchMessage( &msg );

      }

}


void NewConsole( )

{

      AllocConsole( );

      g_hStdOut =
                GetStdHandle( STD_OUTPUT_HANDLE );

}


int APIENTRY WinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR   lpCmdLine,

                     int       nCmdShow)

{

      NewConsole( );

         g_hInst = hInstance;

      RegisterWnd( "MyWnd" );

      HWND hWnd = CreateWnd( "MyWnd" );

      DisplayWnd( hWnd );

      Message( );


      return 0;

}

流行语 发表于 2014-10-27 17:49:48

这是我从书上抄的例子,不知道对你有没有帮助

拈花小仙 发表于 2014-10-27 19:37:12

流行语 发表于 2014-10-27 17:49
这是我从书上抄的例子,不知道对你有没有帮助

{:7_139:}语语这个解决我另外一个问题哈,不过我想画的是按钮,BUTTON,就像那些集成的IDE一样,可以靠拖动画出按钮来。帮帮我吧~

仰望天上的光 发表于 2014-10-27 19:41:20

就是在WM_CREATE中创建按钮,在原来画矩形的地方移动按钮

流行语 发表于 2014-10-28 08:02:53

我现在还没有思路啊,让我想想!

大个的糖果 发表于 2014-10-30 15:45:46

流行语 发表于 2014-10-30 21:20:03

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

我没有做出来啊:cry

拈花小仙 发表于 2014-10-30 21:26:44

流行语 发表于 2014-10-30 21:20
我没有做出来啊

{:7_136:}语语~

流行语 发表于 2014-10-30 21:28:06

拈花小仙 发表于 2014-10-30 21:26
语语~

不好意思啊!

拈花小仙 发表于 2014-10-30 21:33:54

流行语 发表于 2014-10-30 21:28
不好意思啊!

我才不好意思呢,你帮我这么多,可是画的不是画钮,要不早采纳啦

lyjztz 发表于 2014-11-27 11:26:14

学习学习

智商是硬伤 发表于 2015-9-2 09:25:44

{:7_146:}
页: [1]
查看完整版本: 如果通过鼠标拖动在客户区上画按钮?