16,472
社区成员
发帖
与我相关
我的任务
分享
void DrawBoxOutline(HWND hwnd,POINT ptBeg,POINT ptEnd)
{
HDC hdc;
hdc=GetDC(hwnd);
SetROP2(hdc,R2_NOT);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Rectangle(hdc,ptBeg.x,ptBeg.y,ptEnd.x,ptEnd.y);
ReleaseDC(hwnd,hdc);
}
//窗口过程
case WM_LBUTTONDOWN:
fBlocking=TRUE;
ptBeg.x=ptEnd.x=LOWORD(lParam);
ptBeg.y=ptEnd.y=HIWORD(lParam);
DrawBoxOutline(hwnd,ptBeg,ptEnd);
return 0;
case WM_MOUSEMOVE:
if (fBlocking)
{
DrawBoxOutline(hwnd,ptBeg,ptEnd);
ptEnd.x=LOWORD(lParam);
ptEnd.y=HIWORD(lParam);
DrawBoxOutline(hwnd,ptBeg,ptEnd);
}
return 0;
case WM_LBUTTONUP:
if (fBlocking)
{
fBlocking=FALSE;
DrawBoxOutline(hwnd,ptBeg,ptEnd);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
DrawBoxOutline(hwnd,ptBeg,ptEnd);
EndPaint(hwnd,&ps);
return 0;
#include <Windows.h>
#define DIVISION 5
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int idFocus=0;
TCHAR szChildClass[]=TEXT("ChildClass");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevHinstance,PSTR szCmdLine,int iCmdShow)
{
TCHAR szAppName[]=TEXT("Class");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.lpfnWndProc=WndProc;
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.hInstance=hInstance;
if (!RegisterClass(&wndclass))
{
MessageBox(hwnd,TEXT("ERROR"),TEXT("ERROR"),MB_OK);
return 0;
}
wndclass.cbWndExtra=sizeof(long);
wndclass.lpfnWndProc=ChildWndProc;
wndclass.hIcon=NULL;
wndclass.lpszClassName=szChildClass;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,TEXT("First Program"),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 message,WPARAM wParam,LPARAM lParam)
{
int x,y,cxBlock,cyBlock;
static HWND hwndChild[DIVISION][DIVISION];
switch (message)
{
case WM_CREATE:
for (x=0;x<DIVISION;x++)
{
for (y=0;y<DIVISION;y++)
{
hwndChild[x][y]=CreateWindow(szChildClass,NULL,WS_CHILD|WS_VISIBLE,0,0,0,0,hwnd,NULL,NULL,NULL);
}
}
return 0;
case WM_SIZE:
cxBlock=LOWORD(lParam)/DIVISION;
cyBlock=HIWORD(lParam)/DIVISION;
for (x=0;x<DIVISION;x++)
{
for (y=0;y<DIVISION;y++)
{
MoveWindow(hwndChild[x][y],x*cxBlock,y*cyBlock,cxBlock,cyBlock,TRUE);
}
}
return 0;
case WM_LBUTTONDOWN:
OutputDebugString(TEXT("..LBParent"));
MessageBeep(0);
return 0;
case WM_SETFOCUS: //On set-focus message,set focus to child window
SetFocus(GetDlgItem(hwnd,idFocus));
OutputDebugString(TEXT("..Parent_SetFocus.."));
return 0;
case WM_KEYDOWN: //On key-down message,possibly change the focus window
x=idFocus&0xFF;
y=idFocus>>8;
switch(wParam)
{
case VK_UP:
y--;
break;
case VK_DOWN:
y++;
break;
case VK_LEFT:
x--;
break;
case VK_RIGHT:
x++;
break;
case VK_PRIOR:
x=y=0;
break;
case VK_NEXT:
x=y=DIVISION-1;
break;
default:
return 0;
}
x=(x+DIVISION)%DIVISION;
y=(y+DIVISION)%DIVISION;
idFocus=y<<8|x;
SetFocus(GetDlgItem(hwnd,idFocus));
OutputDebugString(TEXT("..KeyParent"));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
SetWindowLong(hwnd,0,0);
return 0;
case WM_KEYDOWN: //send most key presses to the parent window
if (wParam!=VK_RETURN&&wParam!=VK_SPACE)
{
SendMessage(GetParent(hwnd),message,wParam,lParam);
return 0;
}
case WM_LBUTTONDOWN: //For Return and Space,fall through to toggle the square
SetWindowLong(hwnd,0,1^GetWindowLong(hwnd,0));
OutputDebugString(TEXT("..LBChild 1.."));
SetFocus(hwnd);
OutputDebugString(TEXT("..LBChild 2.."));
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SETFOCUS: //For focus messages,invalidate the window for repaint
idFocus=GetWindowLong(hwnd,GWL_ID);
OutputDebugString(TEXT("..Child_SetFocus.."));
case WM_KILLFOCUS: //For through
InvalidateRect(hwnd,NULL,TRUE);
OutputDebugString(TEXT("..Child_KillFocus.."));
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
if (GetWindowLong(hwnd,0))
{
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,rect.right,rect.bottom);
MoveToEx(hdc,rect.right,rect.top,NULL);
LineTo(hdc,rect.left,rect.bottom);
}
if (hwnd==GetFocus())
{
rect.left+=rect.right/10;
rect.right-=rect.left;
rect.top+=rect.bottom/10;
rect.bottom-=rect.top;
SelectObject(hdc,GetStockObject(NULL_BRUSH));
SelectObject(hdc,CreatePen(PS_DASH,0,0));
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
DeleteObject(SelectObject(hdc,GetStockObject(BLACK_PEN)));
}
EndPaint(hwnd,&ps);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}