#include<windows.h>
#include<windowsx.h>
//定义下三个子窗口的id
#define WNDHOUR_ID 0
#define WNDMINUTE_ID 1
#define WNDSECOND_ID 2
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//显示小时的子窗口回调函数
LRESULT CALLBACK WndHourProc(HWND, UINT, WPARAM, LPARAM);
//显示分钟的子窗口回调函数
LRESULT CALLBACK WndMinuteProc(HWND, UINT, WPARAM, LPARAM);
//显示秒的子窗口回调函数
LRESULT CALLBACK WndSecondProc(HWND, UINT, WPARAM, LPARAM);
//剩下仨子窗口类名
//剩下仨子窗口类名
static TCHAR WndClsHourProcName[] = TEXT("WndHorProc");
static TCHAR WndClsMinuteProcName[] = TEXT("WndMinuteProc");
static TCHAR WndClsSecondProcName[] = TEXT("WndSecondProc");
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static PTCHAR szAppName = (PTCHAR)TEXT("ShowTime");
HWND hwnd = NULL;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("Need Windows NT!!!!"), TEXT("Error"), MB_OK);
}
hwnd = CreateWindow(szAppName, TEXT("数字时钟"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//分别再注册剩下的子窗口
wc.lpszClassName = WndClsHourProcName;
wc.lpfnWndProc = WndHourProc;
wc.hIcon = NULL;
RegisterClass(&wc);
wc.lpszClassName = WndClsMinuteProcName;
wc.lpfnWndProc = WndMinuteProc;
wc.hIcon = NULL;
RegisterClass(&wc);
wc.lpszClassName = WndClsSecondProcName;
wc.lpfnWndProc = WndSecondProc;
wc.hIcon = NULL;
RegisterClass(&wc);
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)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
//窗口的宽和高
static int cxClient, cyClient;
//定义三个子窗口的句柄
static HWND hHourWnd, hMinuteWnd, hSecondWnd;
//用到的画笔和画刷
static HPEN hPen = CreatePen(PS_SOLID, 0, RGB(100, 100, 100));
static HBRUSH hBrush = CreateSolidBrush(RGB(100, 100, 100));
switch (message)
{
case WM_CREATE:
//获取窗口宽高
cxClient = LOWORD(lparam);
cyClient = HIWORD(lparam);
//创建那三个子窗口
hHourWnd = CreateWindow(WndClsHourProcName , NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)WNDHOUR_ID, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), 0);
hMinuteWnd = CreateWindow(WndClsMinuteProcName, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)WNDMINUTE_ID, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), 0);
hSecondWnd = CreateWindow(WndClsSecondProcName, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)WNDSECOND_ID, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), 0);
return 0;
case WM_SIZE:
//把窗口移动到指定位置(平均分成三份)
MoveWindow(hHourWnd, 0, 0, cxClient / 3, cyClient, FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY://接受WM_DESTROY消息并发送WM_QUIT消息
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
//显示小时的子窗口过程
LRESULT CALLBACK WndHourProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
//窗口的宽和高
static int cxClient, cyClient;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
cxClient = LOWORD(lparam);
cyClient = HIWORD(lparam);
return 0;
case WM_PAINT:
//用线标记自己
hdc = BeginPaint(hwnd, &ps);
Rectangle(hdc,0,0,cxClient,cyClient);
EndPaint(hwnd, &ps);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
//显示分钟的子窗口过程
LRESULT CALLBACK WndMinuteProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
//窗口的宽和高
static int cxClient, cyClient;
return DefWindowProc(hwnd, message, wparam, lparam);
}
//显示秒的子窗口过程
LRESULT CALLBACK WndSecondProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
//窗口的宽和高
static int cxClient, cyClient;
return DefWindowProc(hwnd, message, wparam, lparam);
}
这段代码的效果应该是在窗口左边1/3处画一个方框,但是为什么实际上什么也没有画?