高手请进
#include <windows.h>
#include <stdlib.h>
#define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "Typer" ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, "键盘输入示例", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
static char *pBuffer = NULL ;
static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,xCaret, yCaret ,xLPos,yLPos,xRPos,yRPos,X,Y,bPos;
HDC hdc ;
int x, y, i ;
static RECT rect;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
switch (iMsg)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_NCLBUTTONDOWN:
bPos=0;
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
case WM_SIZE :
cxClient = LOWORD (lParam) ; // 获取窗口大小
cyClient = HIWORD (lParam) ;
cxBuffer = max (1, cxClient / cxChar) ; //计算窗口所能容纳的字符数
cyBuffer = max (1, cyClient / cyChar) ;
if (pBuffer != NULL)
free (pBuffer) ;
if ((pBuffer = (char *) malloc (cxBuffer * cyBuffer)) == NULL)
MessageBox (hwnd, "窗口太大,没有足够的内存", "Typer",
MB_ICONEXCLAMATION | MB_OK) ;
else
for (y = 0 ; y < cyBuffer ; y++)
for (x = 0 ; x < cxBuffer ; x++)
BUFFER(x,y) = ' ' ;
xCaret = 0 ;
yCaret = 0 ;
if (hwnd == GetFocus ( ))
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;
case WM_SETFOCUS :
CreateCaret (hwnd, NULL, cxChar, cyChar) ;
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
ShowCaret (hwnd) ;
bPos=1;
return 0 ;
case WM_KILLFOCUS :
HideCaret (hwnd) ;
DestroyCaret ( ) ;
return 0 ;
case WM_MOVE:
GetWindowRect(hwnd,&rect);
xLPos=rect.left;
yLPos=rect.top;
xRPos=rect.right;
yRPos=rect.bottom;
X=xRPos-xLPos;
Y=yRPos-yLPos;
return 0;
case WM_LBUTTONDOWN:
SendMessage(hwnd,WM_SETFOCUS,1L,1L);
return 0;
case WM_KEYDOWN :
switch (wParam)
{ case VK_HOME :
xCaret = 0 ;
break ;
case VK_END :
xCaret = cxBuffer - 1 ;
break ;
case VK_PRIOR :
yCaret = 0 ;
break ;
case VK_NEXT :
yCaret = cyBuffer - 1 ;
break ;
case VK_LEFT :
if(bPos==1)
xCaret = max (xCaret - 1, 0) ;
else
MoveWindow(hwnd,xLPos-10,yLPos,X,Y,1) ;
break ;
case VK_RIGHT :
if(bPos==1)
xCaret = min (xCaret + 1, cxBuffer - 1) ;
else
MoveWindow(hwnd,xLPos+10,yLPos,X,Y,1);
break ;
case VK_UP :
if(bPos==1)
yCaret = max (yCaret - 1, 0) ;
else
MoveWindow(hwnd,xLPos,yLPos-10,X,Y,1);
break ;
case VK_DOWN :
if(bPos==1)
yCaret = min (yCaret + 1, cyBuffer - 1) ;
else
MoveWindow(hwnd,xLPos,10+yLPos,X,Y,1);
break ;
break ;
}
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
for (y = 0 ; y < cyBuffer ; y++)
TextOut (hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
本人想实现的目的是:当鼠标点击标题兰后,移动上下左右键时移动窗口,点击客户区后,移动上下左右键移动光标。在WM_SETFOCUS 里我把bPos设置成1,当点击标题栏后bPos设置成0。但是为什么点击标题栏后,当再次点击客户区时,WM_SETFOCUS没有处理呀。这里我只能在WM_LBUTTONDOWN里SendMessage(hwnd,WM_SETFOCUS,1L,1L);它才执行。为什么呀!!我用SPY++看了一下,当在客户区里点击鼠标时它自动会发送出WM_SETFOCUS呀,为什么不执行呀!!!!!