wind32时钟

#include<windows.h>
#include<stdio.h>


LRESULT CALLBACK WinProc(// WinProc这个名字可以随便改的WindowProc
						 HWND hwnd,      // handle to window
						 UINT uMsg,      // message identifier
						 WPARAM wParam,  // first message parameter
						 LPARAM lParam   // second message parameter
						 );

int WINAPI WinMain(
				   HINSTANCE hInstance,  // handle to current instance
				   HINSTANCE hPrevInstance,  // handle to previous instance
				   LPSTR lpCmdLine,      // pointer to command line
				   int nCmdShow          // show state of window
				   )
{
	//1.注册窗口类
	WNDCLASS wc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	//	wc.hbrBackground=::CreateSolidBrush(0X000000ff);//也可以这样写wc.hbrBackground=::CreateSolidBrush(RGB(255,0,0));
	//可以用CreateSolidBrush也可以用GetStockObject
	wc.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);//里面的颜色是系统默认的一些值,BLACK_BRUSH也是其中一个
	wc.hCursor=::LoadCursor(NULL,IDC_ARROW);// 这里不可以用HINSTANCE hInstance, 主要是我们现在还没有做,所以就没有
	wc.hIcon=::LoadIcon(NULL,IDI_HAND) ;
	wc.hInstance= hInstance;
	wc.lpfnWndProc=WinProc;//指向命令行的指针
	wc.lpszClassName = "wuciqiu2015";
	wc.lpszMenuName = NULL;//菜单名为空
	wc.style = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;//窗口在水平方向上发生变化时会发生同会
	
	
	ATOM wu= ::RegisterClass(&wc);
	if(0 == wu)//失败了
		
	{
		return 0;
	}
	
	//2.创建窗口类
	HWND hWnd = ::CreateWindow(wc.lpszClassName,"wuciqiu first win",WS_OVERLAPPEDWINDOW,10,10,800,600,NULL,NULL,hInstance,NULL);//会发送一个WM_CREATE消息到window procedure
	
	if(0 == hWnd) return 0;
	//3.显示窗口
	::ShowWindow(hWnd,SW_SHOW);
	//4.更新窗口
	::UpdateWindow(hWnd);
	//5.消息循环
	MSG msg;//这里的msg不要付初值,因为就是通过getmessage来获取信息的
	
	while(BOOL bok = ::GetMessage(&msg,NULL,0,0))
	{
		::TranslateMessage(&msg);//WM_CHAR
		::DispatchMessage(&msg);
	}
	
	
	//	MSG msg;
	//	return msg.wParam;
	return 0;
}

LRESULT CALLBACK WinProc(// WinProc这个名字可以随便改的WindowProc
						 HWND hWnd,      // handle to window
						 UINT uMsg,      // message identifier
						 WPARAM wParam,  // first message parameter
						 LPARAM lParam   // second message parameter
						 )
{ 
	
	switch(uMsg)
	{
	case WM_CREATE:
		{
			::SetTimer(hWnd,123,1000,NULL);
		}
		break;
	case WM_CLOSE:
		{
			::DestroyWindow(hWnd);// 它运行完将发送WM_DESTROY信息
			//	::SendMessage(hWnd,WM_QUIT,0,0);//思路是对的,要向信息队列里面发送信息,但是不能这样发对于WM_QUIT
			// ::PostQuitMessage(0);//而是用它来退出,也可以用	case WM_DESTROY:来完成退出
		}
		
		break;
	case WM_DESTROY:
		{   ::KillTimer(hWnd,123);
			::PostQuitMessage(0);
		}
		break;
	case WM_CHAR:
		{
			char szMsg[10]={0};
			sprintf(szMsg,"%c",wParam);//字符串格式化
			
			HDC hdc = GetDC(hWnd);//写字绘图的三步曲
			::TextOut(hdc,10,10,szMsg,strlen(szMsg));
			::ReleaseDC(hWnd,hdc);
		} 
		break;
	case WM_RBUTTONDOWN:
		{  
			HDC hdc;
			hdc = ::GetDC(hWnd);
		
			::TextOut(hdc,LOWORD(lParam),HIWORD(lParam),"hello",5);
			::ReleaseDC(hWnd,hdc);
		}
		break;
		
	case WM_PAINT://永恒绘图
		{  
			HDC hdc;
			PAINTSTRUCT ps;
				SYSTEMTIME st;
			::GetLocalTime(&st);
			char str[] = "13:45:12";
			sprintf(str,"%d:%d:%d",st.wHour,st.wMinute,st.wSecond);
			hdc = ::BeginPaint(hWnd,&ps);//WM_PAINT的hdc要用Bedinpaint
			::TextOut(hdc,LOWORD(lParam),HIWORD(lParam),str,strlen(str));
			::Sleep(1000);//这样做浪费cpu的利用率
			::InvalidateRect(hWnd,NULL,true);
			::EndPaint(hWnd,&ps);
		}
		break;
	case WM_KEYDOWN:
		{
			char szMsg[10]={0};
			sprintf(szMsg,"%d",wParam);//字符串格式化
			
			HDC hdc = GetDC(hWnd);//写字绘图的三步曲
			::TextOut(hdc,10,50,szMsg,strlen(szMsg));
			::ReleaseDC(hWnd,hdc);
		}
		break;
	case WM_ERASEBKGND:
	{
		HDC hdc=::GetDC(hWnd);
		//RECT rt={5,5,100,100};这样刷只能刷一部分
		RECT rt;
		::GetClientRect(hWnd,&rt);//全部的
		HBRUSH hbr= ::CreateSolidBrush(0X0000ff);
		::FillRect((HDC)wParam,&rt,hbr);
		::ReleaseDC(hWnd,hdc);
	}
		break;
		
	default:
		return ::DefWindowProc(hWnd,uMsg,wParam,lParam);
		
					
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值