谁能告诉我一下 VC中的win32 application 工程中到底怎么添加定时器,一定是要具体步骤啊?急用啊!!!!

谁能告诉我一下 VC中的win32 application 工程中到底怎么添加定时器,一定是要具体步骤啊?急用啊!!!!,第1张

1.在添加WM_CREATE消息

 在这个消息添加case WM_CREATE:

SetTimer(hWnd,0,1000,NULL)

//创建窗口时设置定时器

2.添加WM_TIMER消息

//---------------------------------------------------------------

case WM_TIMER: //添加定时器处理消息

// 定时器响应函数

OnTimer()

break

3.声明定时器消息响应函数 void

OnTimer()

全部代码看下面

//

// 定时器.cpp : Defines the entry point for the application.

//

#include "stdafx.h"

#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:

HINSTANCE hInst // current instance

TCHAR szTitle[MAX_LOADSTRING] // The title bar text

TCHAR szWindowClass[MAX_LOADSTRING] // The title bar text

// Foward declarations of functions included in this code module:

ATOM MyRegisterClass(HINSTANCE hInstance)

BOOL InitInstance(HINSTANCE, int)

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)

LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM)

//定义定时器消息响应函数

void OnTimer()

//声明一个全局句柄获取当前程序实例句柄

HWND g_hWnd

int APIENTRY WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

// TODO: Place code here.

MSG msg

HACCEL hAccelTable

// Initialize global strings

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING)

LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING)

MyRegisterClass(hInstance)

// Perform application initialization:

if (!InitInstance (hInstance, nCmdShow))

{

return FALSE

}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY)

// Main message loop:

while (GetMessage(&msg, NULL, 0, 0))

{

if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

{

TranslateMessage(&msg)

DispatchMessage(&msg)

}

}

return msg.wParam

}

//

// FUNCTION: MyRegisterClass()

//

// PURPOSE: Registers the window class.

//

// COMMENTS:

//

//This function and its usage is only necessary if you want this code

//to be compatible with Win32 systems prior to the 'RegisterClassEx'

//function that was added to Windows 95. It is important to call this function

//so that the application will get 'well formed' small icons associated

//with it.

//

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex

wcex.cbSize = sizeof(WNDCLASSEX)

wcex.style = CS_HREDRAW | CS_VREDRAW

wcex.lpfnWndProc = (WNDPROC)WndProc

wcex.cbClsExtra = 0

wcex.cbWndExtra = 0

wcex.hInstance = hInstance

wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY)

wcex.hCursor = LoadCursor(NULL, IDC_ARROW)

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1)

wcex.lpszMenuName = (LPCSTR)IDC_MY

wcex.lpszClassName = szWindowClass

wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL)

return RegisterClassEx(&wcex)

}

//

// FUNCTION: InitInstance(HANDLE, int)

//

// PURPOSE: Saves instance handle and creates main window

//

// COMMENTS:

//

//In this function, we save the instance handle in a global variable and

//create and display the main program window.

//

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HWND hWnd

hInst = hInstance// Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL)

if (!hWnd)

{

return FALSE

}

g_hWnd=hWnd//保存全局句柄 为测试函数绘图使用

ShowWindow(hWnd, nCmdShow)

UpdateWindow(hWnd)

return TRUE

}

//

// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)

//

// PURPOSE: Processes messages for the main window.

//

// WM_COMMAND - process the application menu

// WM_PAINT - Paint the main window

// WM_DESTROY - post a quit message and return

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

int wmId, wmEvent

PAINTSTRUCT ps

HDC hdc

TCHAR szHello[MAX_LOADSTRING]

LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING)

switch (message)

{

case WM_CREATE:

SetTimer(hWnd,0,1000,NULL)

//创建窗口时设置定时器

break

case WM_COMMAND:

wmId= LOWORD(wParam)

wmEvent = HIWORD(wParam)

// Parse the menu selections:

switch (wmId)

{

case IDM_ABOUT:

DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About)

break

case IDM_EXIT:

DestroyWindow(hWnd)

break

default:

return DefWindowProc(hWnd, message, wParam, lParam)

}

break

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps)

// TODO: Add any drawing code here...

RECT rt

GetClientRect(hWnd, &rt)

DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER)

EndPaint(hWnd, &ps)

break

//---------------------------------------------------------------

case WM_TIMER: //添加定时器处理消息

// 定时器响应函数

OnTimer()

break

//---------------------------------------------------------------

case WM_DESTROY:

PostQuitMessage(0)

break

default:

return DefWindowProc(hWnd, message, wParam, lParam)

}

return 0

}

// Mesage handler for about box.

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_INITDIALOG:

return TRUE

case WM_COMMAND:

if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)

{

EndDialog(hDlg, LOWORD(wParam))

return TRUE

}

break

}

return FALSE

}

void OnTimer()

{

// MessageBox(NULL,_T("定时器启动"),_T("定时器"),MB_YESNO)

//测试定时器是否启动成功

HDC dc=GetDC(g_hWnd) //g_hWnd是定义的全局变量

TextOut(dc,50,50,_T("定时器已经启动"),14)

ReleaseDC(g_hWnd,dc)

}

在/doc/">程序中我们经常要使用定时刷新的功能,典型的应用是在信息管理系统中表单要跟着数据库中的数据变动。MFC提供了定时器来完成这个功能。

在MFC中和定时器相关的有三个函数:

UINT SetTimer( UINT nIDEvent, UINT nElapse,

void (CALLBACK EXPORT* lpfnTimer)( HWND, UINT, UINT, DWORD) )

afx_msg void OnTimer( UINT nIDEvent )

BOOL KillTimer( int nIDEvent )

参数说明:

UINT nIDEvent:定时器的ID,给定时器唯一的身份验证,如果在一个/doc/">程序中有多个定时器可以用这个ID来确定是那个定时器发送的消息。

UINT nElapse: 定义刷新时间,即间隔多长时间刷新一次,单位是毫秒。

void (CALLBACK EXPORT* lpfnTimer)( HWND, UINT, UINT, DWORD):

这个回调函数中实现刷新时所做的 *** 作,如在数据库中读取数据。但是我们大多数时候不在这里实现,而是在OnTimer中。

函数功能:

SetTimer用来定义一个定时器的属性,如改定时器的ID,刷新时间,处理函数。

OnTimer实际时系统定义消息用来响应WM_TIMER消息,在这里可以实现对多定时器中的各个定时器分别响应,这里才时定时/doc/">程序大展宏图的地方。 字串2

KillTimer用来结束一个定时器。 字串1

下面我们用一个例子来说明定时器的使用:

这个例子用来实现一个简单的功能,就是在一个单/doc/">文档/doc/">程序中,每间隔5秒d出一个消息框提示“定时器1”,每隔7秒d出一个消息框提示“定时器2”。

建立单/doc/">文档/doc/">程序略,一路Next。

(1)在resource.h中定义两个定时器的ID

#define IDTIMER1 1

#define IDTIMER2 2

(2)在CMainFrame的OnCreate函数中定义两个定时器的属性。

SetTimer(TIMEID1,5000,0)

SetTimer(TIMEID2,7000,0)

(3) CMainFrame中对WM_TIMER进行响应。

void CMainFrame::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

switch(nIDEvent) {

case TIMEID1:

{

AfxMessageBox("定时器1!")

break

} 字串1

case TIMEID2:

{

AfxMessageBox("定时器2!")

break

}

default:

}

CFrameWnd::OnTimer(nIDEvent)

}

(4)在CMainFrame的析构函数中添加

KillTimer(IDTIMER1)

KillTimer(IDTIMER2)

你打算做2次测定工作。第一个2秒内拍入了哪些字符,第二个2秒内拍入了哪些字符。

如果你要判断输入的是否是 B, D, 可以自己改一下程序:

去掉 printf("B: %c\n",p)

用:

if ( p == 'B') printf("C\n") 或 if ( (0xff &p) == 'B') printf("C\n") 同你的键盘有关

去掉 printf("E: %c\n",p)

用:

if ( p == 'D') printf("E\n") 或 if ( (0xff &p) == 'D') printf("E\n") 同你的键盘有关

下面 是完整程序。 MS VC++ 6.0 编译器:

#include <stdio.h>

#include <conio.h>

#include <time.h>

int main(){

clock_t t0,dt

int flag

int p

t0 = clock()

printf("=======A========\n")

Lab:

flag = 1

while( !_kbhit() ) {

dt = clock() - t0

if (dt >= 2 * CLOCKS_PER_SEC) flag =0

}

if (flag==0) { printf("2 seconds -- time is over !\n")

} else {

p = _getch()printf("B: %c\n",p)

goto Lab

}

printf("\n=======C=======\n")

t0=clock()

Lab2:

flag = 1

while( !_kbhit() ) {

dt = clock() - t0

if (dt >= 2 * CLOCKS_PER_SEC) flag =0

}

if (flag==0) { printf("2 seconds -- time is over !\n")

} else {

p = _getch()printf("E: %c\n",p)

goto Lab2

}

printf("\n=======D=======\n")

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11397716.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存