#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[260] = { 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[260] = { 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;
}