想做一个无窗口类的定时器,特别是要求不使用任何MFC库,在参考了下面两篇文章以后,用刘辉提供的代码,在使用MFC的情况下调试通过。现在我想不使用MFC,将project->setting->general,选择“no using MFC”,就不知道该怎么调试通过了,程序代码如下,每次连接时都会提示:Test.cpp(46) : error C2664: 'SetTimer' : cannot convert parameter 4 from 'void (struct HWND__ *,unsigned int,unsigned int,unsigned long)' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'
        None of the functions with this name in scope match the target type请大家帮忙调试,需要注意上面的设置,即不需要MFC。谢谢!http://expert.csdn.net/Expert/topic/1663/1663692.xml?temp=.1654932
http://www.vckbase.com/document/viewdoc.asp?id=594
1.stdafx.h:// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#include <windows.h>// TODO: reference additional headers your program requires here//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)2.stdafx.cpp:// stdafx.cpp : source file that includes just the standard includes
// Test.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information#include "stdafx.h"// TODO: reference any additional headers you need in STDAFX.H
// and not in this file3.test.h://#include <afxtempl.h>//class CMyTimer;
//用模板类中的映射表类定义一种数据类型
//typedef CMap<UINT,UINT,CMyTimer*,CMyTimer*> CTimerMap;class CMyTimer  
{
public:
//设置定时器,nElapse表示时间间隔,sz表示要提示的内容
void SetMyTimer(UINT nElapse,char* sz);
//销毁该实例的定时器
void KillMyTimer();
//保存该实例的定时器标志值
UINT m_nTimerID;
//静态数据成员要提示的内容
char* szContent;
//声明静态数据成员,映射表类,用于保存所有的定时器信息
// static CTimerMap m_sTimeMap;
//静态成员函数,用于处理定时器的消息
// static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
CMyTimer();
virtual ~CMyTimer();
};4.Test.cpp:// Test.cpp : Defines the entry point for the application.
//#include "stdafx.h"
#include <stdio.h>
#include "test.h"//必须要在外部定义一下静态数据成员
//CTimerMap CMyTimer::m_sTimeMap;int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
CMyTimer m_myTimer;
m_myTimer.SetMyTimer(2000,"hello!"); MessageBox(0,"定时器开始工作(先不要按确定,要结束程序时按确定)","你好",MB_OK); m_myTimer.KillMyTimer(); return 0;
}CMyTimer::CMyTimer()
{
m_nTimerID = 0;
}CMyTimer::~CMyTimer()
{
}void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
char sz[200];
// sprintf(sz,"%d号定时器:%s",idEvent,m_sTimeMap[idEvent]->szContent);
sprintf(sz,"%d号定时器:%s",idEvent,szContent);
MessageBox(0,sz,"你好",MB_OK);
}void CMyTimer::SetMyTimer(UINT nElapse,char* sz)
{
szContent = sz;
m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
// m_sTimeMap[m_nTimerID] = this;
}void CMyTimer::KillMyTimer()
{
KillTimer(NULL,m_nTimerID);
// m_sTimeMap.RemoveKey(m_nTimerID);
}

解决方案 »

  1.   

    你不用的话,
    应该是保证这4个参数都全
    UINT_PTR SetTimer(
      HWND hWnd,              // handle to window
      UINT_PTR nIDEvent,      // timer identifier
      UINT uElapse,           // time-out value
      TIMERPROC lpTimerFunc   // timer procedure
    );
    看你应该是缺了第一个,
    可以这样:
    SetTimer(NULL, 0, 200, MyTimerProc);
      

  2.   

    回调函数MyTimerProc不要定义为类成员既可
      

  3.   

    谢谢cjhsh(化聿),已经调试通过了,不过不明白道理,为什么不能在类里呢?在刘辉的代码里,回调函数MyTimerProc可是在类里面的,因为有时确实是在类里面方便一些。
      

  4.   

    不用settimer(),试试gettickcount();
      

  5.   

    用gettickcount()怎么做一个定时器啊?要建立一个循环不断调用它吗?能详细介绍一下用法吗?
      

  6.   

    谢谢FAICHEN,我弄明白了。发现一个问题:即当 WinMain 有一个运行时间比较长的循环体时,用settimer()指定的定时器函数一直不能运行,只有当这个循环运行结束以后才开始MyTimerProc(),这是怎么回事呢?不过这时候倒真是用的着gettickcount()了。
      

  7.   

    为什么不能在类里呢?在刘辉的代码里,回调函数MyTimerProc可是在类里面的,因为有时确实是在类里面方便一些。===关键在于是不是静态成员函数。请参考SetTimer和TimerProc的说明。 //静态成员函数,用于处理定时器的消息
    //static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
    void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
      

  8.   

    use TIMERPROC force convertexample ..// tttttt.cpp : Defines the entry point for the application.
    //#include "stdafx.h"
    #include <windows.h>
    HWND hwnd;LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    void CALLBACK MyTimerProc( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 );
    void
    CALLBACK MyTimerProc( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 )
    {
    ::MessageBox( hwnd, "NULL", "NULL", MB_OK );
    }int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow )
    {
    MSG Msg;
    WNDCLASS wndclass;
    char lpszClassName[]="Class";
    char lpszTitle[]="窗口示例"; wndclass.style=0;
    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 = ( HBRUSH )GetStockObject( WHITE_BRUSH );
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=lpszClassName;
    if(!RegisterClass(&wndclass))
    {
    MessageBeep(0);
    return FALSE;
    }
    hwnd=CreateWindow(lpszClassName,
    lpszTitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    NULL,
    NULL,
    hInstance,
    NULL);
    ::SetTimer( hwnd, 8888, 1000, ( TIMERPROC )MyTimerProc );
    ShowWindow( hwnd,nCmdShow);
    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)
    {
    switch(message)
    {
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
      

  9.   

    谢谢楼上的回应!
    to Mackz: 我都是试了,静态或非静态的都不行。to JennyVenus: 当
    void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)是一个类成员的时候,强制转换不起作用,连接的时候提示:
    error C2440: 'type cast' : cannot convert from '' to 'void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)'
            None of the functions with this name in scope match the target type