用windows api实现   不用mfc
 绘制一个窗口  窗口中有一个按钮和一个指针  指针能摆动的  摆动的幅度是随着你单击按钮时间的长短而变化的    谢谢  
就像一个压力表似的!!我的代码里没有实现指针的摆动:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HINSTANCE hInst ;
#define TIMER_SEC 1
#define TIMER_ID 1
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT ("test") ;
        HWND        hwnd ;
        MSG         msg ;
        WNDCLASS    wndclass ;
 hInst = hInstance ;        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              = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName                = NULL ;
        wndclass.lpszClassName               = szAppName ;
        
if (!RegisterClass (&wndclass))
        {
            MessageBox (NULL,TEXT ("RegisterClass wrong!"),szAppName, MB_ICONERROR) ;
            return 0 ;
        }
        hwnd = CreateWindow (szAppName, TEXT ("test"),
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 
CW_USEDEFAULT,CW_USEDEFAULT, 
CW_USEDEFAULT,NULL, NULL, hInstance, NULL) ;
        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)
{
        static HWND           hwndButton;
static BOOL   status=false ;
static int next=0;
        static RECT           rect ;
        static int x1=400,y1=250,x2=400,y2=100;
        static int            cxChar, cyChar ;
        HDC                   hdc ;
        PAINTSTRUCT           ps ;
        int    i=1;
        switch (message)
        {
        
           case   WM_CREATE :
cxChar = LOWORD (GetDialogBaseUnits ()) ;
                    cyChar = HIWORD (GetDialogBaseUnits ()) ;
        
                   hwndButton =CreateWindow ( "button","按钮",
        BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,
10,40,160,30,
        hwnd, (HMENU) i,
        ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
                    return 0 ;        case   WM_SIZE :
 return 0 ;
        case   WM_PAINT :

hdc = BeginPaint (hwnd, &ps) ; MoveToEx (hdc,x1, y1, NULL) ;
        
                LineTo (hdc, x2+next, y2+next) ;
next=next+20; EndPaint (hwnd, &ps) ;
return 0 ;
       case   WM_COMMAND :

hdc = GetDC (hwnd) ;
  
TextOut (hdc, 24 * cxChar, cyChar, "nihao", lstrlen ("nihao")) ; MoveToEx (hdc,x1, y1, NULL) ;
        
                LineTo (hdc, 550, 200) ; ReleaseDC (hwnd, hdc) ;
ValidateRect (hwnd, &rect) ;
break ;
case   WM_LBUTTONDOWN:
status=true;
SetTimer(hwnd,TIMER_ID,2000,NULL) ; 

break;
case   WM_TIMER:
//On Timer:
    if(status)
{

TextOut (hdc,520,330, "hao", lstrlen ("hao")) ; }
InvalidateRect(hwnd,NULL,TRUE);

return 0 ; 
case   WM_LBUTTONUP:
status=false;
KillTimer(hwnd,TIMER_ID) ;  break;
case   WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
    }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
}

解决方案 »

  1.   

    你的  WM_LBUTTONUP   WM_TIMER  WM_LBUTTONDOWN  WM_PAINT  好像都没有响应啊   只有cmmand响应了 你自己看看 
      

  2.   

    你这个必须子类化按钮,在按钮的窗口过程中处理WM_LBUTTONDOWN,在父窗口的窗口过程是无法知道在按钮上按下鼠标的。
      

  3.   

    子类化 按钮WNDPROC old=GetWindowLong(hbutton1,GWL_WNDPROC);   
      SetWindowLong(hbutton1,GWL_WNDPROC,(LONG)MyButton);   
      LRESULT   CALLBACK   MyButton(HWND   hwnd,UINT   msg,WPARAM   wParam,LPARAM   lParam)   
      {   
      switch(msg)   
      {   
      case   WM_LBUTTONDOWN:   
      MessageBox(hwnd,"你点击了我","子类化按钮单击通知",MB_OK);   
      return   0;   
      default:   
      return   CallWindowProc(old,   hwnd,   msg,   wParam,   lParam);   
      }   
      return   CallWindowProc(old,   hwnd,   msg,   wParam,   lParam);//偶通常不加(hehe)   
      }
      

  4.   

    你这里首先有一个问题:按钮控件处理了鼠标消息,所以在按钮上点击不会触发父窗口的WM_LBUTTONDOWN等鼠标消息,最好子类化按钮来处理。
      

  5.   

    如何子类化按钮啊   能不能帮我弄弄代码实现那个功能啊   please!
      

  6.   

    不就是画一个箭头么。。
    #define M_PI 3.1415926
    void DrawArrow(HDC hdc, POINT   StartPoint,   POINT   EndPoint) {   
        double   X1   =   StartPoint.x;   
        double   Y1   =   StartPoint.y;   
        double   X2   =   EndPoint.x;   
        double   Y2   =   EndPoint.y;   
        double   xl,   yl; //   左边点   
        double   xr,   yr; //   右边点   
        int   RelaX,   RelaY;   
        int   Length   =   12;//   箭头线的长度   
        int   Angle;   
    if   (X1   !=   X2) {   
    Angle   =   atan((Y2   -   Y1)   /   (X2   -   X1))   *   180   /   M_PI;   
    } else {   
    Angle   =   atan((Y2   -   Y1))   *   180   /   M_PI;
    }
        RelaX = X1 > X2 ? 1 : -1;
    RelaY = Y1 > Y2 ? 1 : -1;
    MoveToEx(hdc, X1, Y1, 0);
    LineTo(hdc, X2, Y2);
    MoveToEx(hdc, X2, Y2, 0);
    xl   =   X2   +   RelaX   *   Length   *   cos(M_PI   *   (30   +   Angle)   /   180);   
        yl   =   Y2   +   RelaX   *   Length   *   sin(M_PI   *   (30   +   Angle)   /   180); 
    LineTo(hdc, xl, yl);
    MoveToEx(hdc, X2, Y2, 0);
    xr   =   X2   +   RelaX   *   Length   *   cos(M_PI   *   ((-30)   +   Angle)   /   180);   
        yr   =   Y2   +   RelaX   *   Length   *   sin(M_PI   *   ((-30)   +   Angle)   /   180);
    LineTo(hdc, xr, yr);
    }
    这个是根据初始点和结束点画箭头的函数,你的初始点固定的,把参数改成角度就ok
    void DrawArrow(HDC hdc, int angle) 
    case  WM_TIMER: 
    //On Timer: 
        if(status) 
    { angle++; } else {
    angle--;
    }
    DrawArrow(hdc, angle) ;return 0 ; 
      

  7.   

    简单写了下啊,我是用GDI+,原理一样:
    #include <math.h>
    const double pi = 3.1415926535;
    const UINT_PTR nTimer = 1;
    int nCount = 0;
    LONG_PTR pOldButtonProc = 0;
    BOOL bAdd = FALSE;LRESULT CALLBACK ButtonProc(HWND hWndButton, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_LBUTTONDOWN:
    bAdd = TRUE;
    SetTimer(GetParent(hWndButton), nTimer, 10, NULL);
    SetCapture(hWndButton);
    //return 0;
    break;
    case WM_LBUTTONUP:
    bAdd = FALSE;
    ReleaseCapture();
    //return 0;
    break;
    }
    return CallWindowProc((WNDPROC)pOldButtonProc, hWndButton, message, wParam, lParam);
    }VOID Example_DrawGauge(Graphics& graphic, REAL x, REAL y)
    {
    Pen penBoard(Color(0, 0, 255));
    graphic.DrawEllipse(&penBoard, x, y, 100.0, 100.0);
    PointF center(x + 50, y + 50);
    PointF end(x + 50 + 45 * (float)sin(nCount / 180. * pi), y + 50 - 45 * (float)cos(nCount / 180. * pi));
    Pen penPointer(Color(0, 255, 0));
    graphic.DrawLine(&penPointer, center, end);
    }HWND hwndButton = NULL;
    //
    //  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  目的: 处理主窗口的消息。
    //
    //  WM_COMMAND - 处理应用程序菜单
    //  WM_PAINT - 绘制主窗口
    //  WM_DESTROY - 发送退出消息并返回
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc; switch (message)
    {
    case WM_CREATE:
    hwndButton = CreateWindow(_T("button"), _T("按钮"), BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, 10,40,160,30, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
    if (hwndButton)
    pOldButtonProc = SetWindowLongPtr(hwndButton, GWLP_WNDPROC, (LONG_PTR)ButtonProc);
    break;
    case WM_TIMER:
    if (bAdd || nCount < 0)
    nCount++;
    else if (nCount > 0)
    nCount -= (int)sqrt((double)nCount);
    else if (nCount == 0)
    {
    KillTimer(hWnd, nTimer);
    }
    InvalidateRect(hWnd, NULL, TRUE);
    break;
    case WM_PAINT:
    {
    hdc = BeginPaint(hWnd, &ps);
    // TODO: 在此添加任意绘图代码...
    Graphics graphic(hdc);
    Example_DrawGauge(graphic, 200, 200);
    EndPaint(hWnd, &ps);
    }
    break;
      

  8.   

    to: Mackz   
    绘图代码中如何填写才能实现如题的要求啊
      

  9.   

    如何用纯sdk实现啊    我要代码   谢啦 
      

  10.   

    我都已经写好了……用Elippse画圆,Line画线,这都要写好?
      

  11.   

    算了呢,都给你:
    VOID Example_DrawGauge(HDC hdc, int x, int y)
    {
    HPEN hPenBorder = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
    HPEN hOldPen = (HPEN)SelectObject(hdc, hPenBorder);
    Ellipse(hdc, x, y, x + 100, y + 100);
    int xCenter = x + 50;
    int yCenter = y + 50;
    int xEnd = (int)(xCenter + 45 * sin(nCount / 180. * pi));
    int yEnd= (int)(yCenter - 45 * cos(nCount / 180. * pi));
    HPEN hPenPointer = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
    SelectObject(hdc, hPenPointer);
    MoveToEx(hdc, xCenter, yCenter, NULL);
    LineTo(hdc, xEnd, yEnd);
    SelectObject(hdc, hOldPen);
    DeleteObject(hPenBorder);
    DeleteObject(hPenPointer);
    }HWND hwndButton = NULL;
    //
    //  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  目的: 处理主窗口的消息。
    //
    //  WM_COMMAND - 处理应用程序菜单
    //  WM_PAINT - 绘制主窗口
    //  WM_DESTROY - 发送退出消息并返回
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc; switch (message)
    {
    case WM_CREATE:
    hwndButton = CreateWindow(_T("button"), _T("按钮"), BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, 10,40,160,30, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
    if (hwndButton)
    pOldButtonProc = SetWindowLongPtr(hwndButton, GWLP_WNDPROC, (LONG_PTR)ButtonProc);
    break;
    case WM_TIMER:
    if (bAdd || nCount < 0)
    nCount++;
    else if (nCount > 0)
    nCount -= (int)sqrt((double)nCount);
    else if (nCount == 0)
    {
    KillTimer(hWnd, nTimer);
    }
    RECT rc;
    rc.left = (int)rectGauge.X;
    rc.top = (int)rectGauge.Y;
    rc.right = rc.left + (int)rectGauge.Width;
    rc.bottom = rc.top + (int)rectGauge.Height;
    InvalidateRect(hWnd, &rc, FALSE);
    break;
    case WM_PAINT:
    {
    hdc = BeginPaint(hWnd, &ps);
    // TODO: 在此添加任意绘图代码...
    Example_DrawGauge(hdc, 200, 200);
    //Graphics graphic(hdc);
    //Example_DrawGauge(graphic);
    //Example_DrawCurve(graphic);
    //Example_Bitmap(graphic);
    EndPaint(hWnd, &ps);
    }
    break;
      

  12.   

    我的意思是 不用GDI啊    用sdk实现啊    void Example_DrawGauge(Graphics & graphic, REAL x, REAL y)在这个函数里不是用得sdk啊    帮帮忙吧  谢谢你啦
      

  13.   

    呵呵  刚接触windows编程一周多点   老您费心啦       谢啦~