利用windows timer控制界面换颜色

 

 

#include <Windows.h>
#define ID_TIMER 1

 

LRESULT CALLBACK WindowProc(
 HWND hwnd,      // handle to window
 UINT uMsg,      // message identifier
 WPARAM wParam,  // first message parameter
 LPARAM lParam   // second message parameter
 )

 static BOOL f= false;
 HBRUSH hbrush;
 HDC hdc;
 PAINTSTRUCT ps;
 RECT rect;

 switch(uMsg)
 {
 case WM_CREATE:
  {
   SetTimer(hwnd,ID_TIMER,1000,NULL);
  }break;
 case WM_TIMER:
  {
   MessageBeep(-1);
   f = !f;
   InvalidateRect(hwnd,NULL,FALSE);
  }
 case WM_CHAR:
  {

  }break;
 case WM_PAINT:
  {
   hdc = BeginPaint(hwnd,&ps);
   GetClientRect(hwnd,&rect);
   hbrush = CreateSolidBrush(f?RGB(255,0,0):RGB(0,0,255));
   FillRect(hdc,&rect,hbrush);
   EndPaint(hwnd,&ps);
   DeleteObject(hbrush);
  }break;
 case WM_DESTROY:
  { 
   KillTimer(hwnd,ID_TIMER);
   PostQuitMessage(0);
  }break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 }

 return 0;
}


int WINAPI WinMain(
 HINSTANCE hInstance,      // handle to current instance
 HINSTANCE hPrevInstance,  // handle to previous instance
 LPSTR lpCmdLine,          // command line
 int nCmdShow              // show state
 )
{

 WNDCLASS wndclass;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = 0;
 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wndclass.hCursor = LoadCursor(NULL,IDC_HAND);
 wndclass.hIcon = LoadIcon(NULL,IDI_QUESTION);
 wndclass.hInstance = hInstance;
 wndclass.lpfnWndProc = WindowProc;
 wndclass.lpszClassName = "2013_11_27(2)_demo";
 wndclass.lpszMenuName =NULL;
 wndclass.style = CS_HREDRAW |CS_VREDRAW;
 if(!RegisterClass(&wndclass))
 {
  MessageBox(NULL,"创建窗口失败","失败",MB_OK);
 }
 HWND hwnd = CreateWindow("2013_11_27(2)_demo","time的demo",WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,500,500,NULL,NULL,hInstance,NULL);

 ShowWindow(hwnd,SW_SHOW);
 UpdateWindow(hwnd);
 MSG msg;
 BOOL flag;
 while((flag = GetMessage(&msg,hwnd,0,0))!=0)
 {
  if(flag==-1)
  {
   PostQuitMessage(0);
  }
  else
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值