您的位置:首页 > 其它

中科院-杨力祥视频教程 07课程

2014-03-13 17:03 447 查看
1.资源的使用方法 主要是 rc文件 resource.h文件

2.定时器SetTimer的使用的两种方法。

①定义ID号 #define ID_TIMERTWOSEC

②创建SetTimer 使用WM_CREATE.

③WM_TIMER

④KillTImer(hwnd,ID_TIMERTWOSEC)

3.扫雷程序的需求。

以下是测试程序SetTimer的使用方法:

/*------------------------------------------------------------
本程序主要用来测试 SetTimer函数的用法
------------------------------------------------------------*/

#include <windows.h>
#define ID_TIMERONESEC 1
#define ID_TIMERTWOSEC 2
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID CALLBACK TimerProc(HWND, UINT, UINT, DWORD);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SetTimer") ;
HWND         hwnd ;
MSG          msg ;
WNDCLASS     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 = NULL;//(HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName  = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName,                  // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW,        // window style
CW_USEDEFAULT,              // initial x position
CW_USEDEFAULT,              // initial y position
CW_USEDEFAULT,              // initial x size
CW_USEDEFAULT,              // initial y size
NULL,                       // parent window handle
NULL,                       // window menu handle
hInstance,                  // program instance handle
NULL) ;                     // creation parameters

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 RECT		reBrush;
static int cxClient,cyClient;
static bool bColorRed = true;
static HBRUSH hBrush;
static int i = 0;
switch (message)
{
case WM_CREATE:
reBrush.bottom = 400;
reBrush.left = 50;
reBrush.right= 400;
reBrush.top  = 50;
SetTimer(hwnd,ID_TIMERONESEC,1000,NULL);
SetTimer(hwnd,ID_TIMERTWOSEC,2000,TimerProc);

return 0 ;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_TIMER:
i++;
if (i > 5)
{
KillTimer(hwnd,ID_TIMERONESEC);
}

bColorRed = !bColorRed;
InvalidateRect(hwnd,&reBrush,FALSE);
return 0;
case WM_PAINT:
MessageBeep(-1);
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect(hwnd,&rect);
hBrush = CreateSolidBrush(bColorRed ? RGB(255,0,0):RGB(0,0,255));
FillRect(hdc,&rect,hBrush);
EndPaint (hwnd, &ps) ;
DeleteObject(hBrush);
return 0 ;

case WM_DESTROY:
KillTimer(hwnd,ID_TIMERONESEC);
KillTimer(hwnd,ID_TIMERTWOSEC);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT iTimerID, DWORD dwTime)
{
static bool bColorBlue = true;

bColorBlue = !bColorBlue;

RECT rect;
rect.bottom = 300;
rect.left = 500;
rect.right = 700;
rect.top = 200;

HDC hdc = GetDC(hwnd);

HBRUSH brush = CreateSolidBrush(bColorBlue ? RGB(0,225,0):RGB(0,0,255));
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc,brush);
FillRect(hdc,&rect,brush);
SelectObject(hdc,hOldBrush);

ReleaseDC(hwnd,hdc);
DeleteObject(brush);

return ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息