关于VC中一个小程序的一个小问题 跪谢了 好久都没想出来

时间:2022-09-16 08:07:22
谁能帮我看看  当我运行这个程序时。。。。那个东西往右走  当我按键盘上的左键 他为啥不停下来 往左走 而继续往右走 谁能帮帮我看看这个程序哪个地方有问题  跪谢了 。。。。
#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

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_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinProc;
wndclass.lpszClassName="shenhao";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
              CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
         TranslateMessage(&msg);
 DispatchMessage(&msg);
}
return 0 ;
}
LRESULT CALLBACK WinProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)

{   
HDC hdc;
    PAINTSTRUCT ps;
static  i=0;
    static  j=10;
static  k=0;
static  m=10;
switch(uMsg)
{
case WM_CREATE:
SetTimer(hwnd,1,100,NULL);
 
    
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
RECT rt;
        GetClientRect(hwnd,&rt);
if(i==0)
{
  Pie(hdc,100+j,100,150+j,150,150+j,100,150+j,150);
        }
else
{
          Pie(hdc,100+j,100,150+j,150,150+j,125,150+j,125);  
}
i=(i+1)%2;
if(j<rt.right-150)
{
j=(j+10)%1000;
}
 
EndPaint(hwnd,&ps);

break;
case WM_KEYDOWN:
{  
          switch(wParam)
  { 
    case VK_LEFT:
              hdc=GetDC(hwnd);
      RECT rt1;
              GetClientRect(hwnd,&rt1);
         if(k==0)
 {
       Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,150,100+j-m,100);
 }
     else
 {
              Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,125,100+j-m,125);  
 }
 k=(k+1)%2;
 if(100+j-m<rt1.left)
 {
 m=(m+10)%1000;
 }
 ReleaseDC(hwnd,hdc);
  break;
  }
}
break;
case WM_TIMER:
    
        InvalidateRect(hwnd,NULL,TRUE);
break;
 

    case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;

14 个解决方案

#1


在你代码基础上改的
你自己比较一下吧
#include<windows.h>
#include<stdio.h>

int nDirect = 0;

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

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_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinProc;
wndclass.lpszClassName="shenhao";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0 ;
}
LRESULT CALLBACK WinProc(
 HWND hwnd, // handle to window
 UINT uMsg, // message identifier
 WPARAM wParam, // first message parameter
 LPARAM lParam // second message parameter
 )

{   
HDC hdc;
PAINTSTRUCT ps;
static int i=0;
static int j=10;
static int k=0;
static int m=10;
switch(uMsg)
{
case WM_CREATE:
SetTimer(hwnd,1,100,NULL);


case WM_PAINT:
hdc=BeginPaint(hwnd, &ps);
switch(nDirect)
{
case 0:
{
RECT rt;
GetClientRect(hwnd, &rt);
if(i==0)
{
Pie(hdc,100+j,100,150+j,150,150+j,100,150+j,150);
}
else
{
Pie(hdc,100+j,100,150+j,150,150+j,125,150+j,125);   
}
i=(i+1)%2;
if(j<rt.right-150)
{
j=(j+10)%1000;
}
}break;
case 1:
{
RECT rt1;
GetClientRect(hwnd, &rt1);
if(k==0)
{
Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,150,100+j-m,100);
}
else
{
Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,125,100+j-m,125);   
}
k=(k+1)%2;
if(100+j-m>rt1.left)
{
m=(m+10)%1000;
}
}
}

EndPaint(hwnd,&ps);

break;
case WM_KEYDOWN:
{   
switch(wParam)
{  
case VK_LEFT:nDirect = 1;break;
}
}
break;
case WM_TIMER:

InvalidateRect(hwnd,NULL,TRUE);
break;


case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

#2


哦  比我那个好多了 但nDirect的初值是多少啊?

#3


哦 我看到了 初值 谢谢你 你太强了 非常感谢啊 

#4


引用 3 楼 hao95740701 的回复:
哦 我看到了 初值 谢谢你 你太强了 非常感谢啊

客气了
就是给你一个思路
其实代码还可以优化

#5


没看懂,不过确实是高手!!!!

#6


还可以怎么优化啊。。指教 指教啊  高手 谢谢;额 

#7


向力天  高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了 

#8


顶一个!

#9


引用 7 楼 hao95740701 的回复:
向力天  高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

#10


我路过学习一下下!!~~~

#11


引用 9 楼 xianglitian 的回复:
引用 7 楼 hao95740701 的回复:

向力天 高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

我实在想不出来了  给一段代码  谢谢了 大哥 

#12


引用 9 楼 xianglitian 的回复:
引用 7 楼 hao95740701 的回复:

向力天 高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

我按你给指导的 做下去 就是这样 但我感觉太复杂了 能给点意见吗 谢谢了 
#include<windows.h>
#include<stdio.h>



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

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_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WinProc;
    wndclass.lpszClassName="shenhao";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);
    HWND hwnd;
    hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0 ;
}
LRESULT CALLBACK WinProc(
                         HWND hwnd, // handle to window
                         UINT uMsg, // message identifier
                         WPARAM wParam, // first message parameter
                         LPARAM lParam // second message parameter
                         )

{   
    HDC hdc;
    PAINTSTRUCT ps;
    static int    i=0;
    static int    j=10;
    static int    k=0;
    static int    m=10;
static int    nDirect=0;
static int    a = 10;
static int    b=0;
RECT rt;
    switch(uMsg)
    {
    case WM_CREATE:
        SetTimer(hwnd,1,100,NULL);


    case WM_PAINT:
        hdc=BeginPaint(hwnd, &ps);
        switch(nDirect)
        {
        case 0:
            {
                 
                GetClientRect(hwnd, &rt);
                if(i==0)
                {
                    Pie(hdc,100+j,100,150+j,150,150+j,100,150+j,150);
                }
                else
                {
                    Pie(hdc,100+j,100,150+j,150,150+j,125,150+j,125);   
                }
                i=(i+1)%2;
                if(j<rt.right-150)
                {
                    j=(j+10)%1000;
                }
            }break;
        case 1:
            {
                
                GetClientRect(hwnd, &rt);
                if(k==0)
                {
                    Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,150,100+j-m,100);
                }
                else
                {
                    Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,125,100+j-m,125);   
                }
                k=(k+1)%2;
                if(100+j-m>rt.left)
                {
                    m=(m+10)%1000;
                }
            }break;
case 2:
            {
                
                GetClientRect(hwnd, &rt);
                if(b==0)
                {
                    Pie(hdc,100+j-m,100-a,150+j-m,150-a,100+j-m,100-a,150+j-m,100-a);
                }
                else
                {
                    Pie(hdc,100+j-m,100-a,150+j-m,150-a,125+j-m,100-a,125+j-m,100-a);   
                }
                b=(b+1)%2;
                if(100-a>rt.top)
                {
                    a=(a+10)%1000;
                }
            }break;
        }

        EndPaint(hwnd,&ps);

        break;
    case WM_KEYDOWN:
        {   
            switch(wParam)
            {  
            case VK_LEFT:
nDirect = 1;
break;
case VK_UP:
nDirect = 2;
break;
case VK_DOWN:
nDirect = 3;
break;
case VK_RIGHT:
break;
nDirect = 4;
            }
        }
        break;
    case WM_TIMER:

        InvalidateRect(hwnd,NULL,TRUE);
        break;


    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

#13



#include<windows.h>
#include<stdio.h>

int    nDirect    = 2;

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

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_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WinProc;
    wndclass.lpszClassName="shenhao";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);
    HWND hwnd;
    hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0 ;
}
LRESULT CALLBACK WinProc(
                         HWND hwnd, // handle to window
                         UINT uMsg, // message identifier
                         WPARAM wParam, // first message parameter
                         LPARAM lParam // second message parameter
                         )

{   
    HDC hdc;
    PAINTSTRUCT ps;
    static int    i=0;
    static int    X=100;
    static int    Y=100;
    switch(uMsg)
    {
    case WM_CREATE:
        SetTimer(hwnd,1,100,NULL);


    case WM_PAINT:
        hdc=BeginPaint(hwnd, &ps);
RECT rt;
        GetClientRect(hwnd, &rt);
        switch(nDirect)
        {
case 0:
{
 if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y-25,X+25,Y-25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X,Y-25,X,Y-25);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }
 break;
}
case 1:
{
 if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y+25,X-25,Y+25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X,Y+25,X,Y+25);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    Y=(Y+10)%1000;
                }
 break;
}
        case 2:
            {
                
                if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y-25,X+25,Y+25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y,X+25,Y);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    X=(X+10)%1000;
                }

            }break;
        case 3:
            {
           
               if(i==0)
                {
                    Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y+25,X-25,Y-25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y,X-25,Y);   
                }

                i=(i+1)%2;


                 if(X<rt.right-150)
                {
                    X=(X-10)%1000;
                }
            }
break;
        }

        EndPaint(hwnd,&ps);

        break;
    case WM_KEYDOWN:
        {   
            switch(wParam)
            {
            case VK_UP:nDirect = 0;break;
            case VK_DOWN:nDirect = 1;break;
case VK_RIGHT:nDirect = 2;break;
            case VK_LEFT:nDirect = 3;break;
            }
        }
        break;
    case WM_TIMER:

        InvalidateRect(hwnd,NULL,TRUE);
        break;


    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

稍微改了下,其中X,Y表示矩形的中点,根据中点就可以推算出pie的每个参数,至于越界处理就没做了,楼主自己改下吧

#14


有2个地方复制的时候忘了改
if(X<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }

             应该是
if(Y<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }

              

#1


在你代码基础上改的
你自己比较一下吧
#include<windows.h>
#include<stdio.h>

int nDirect = 0;

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

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_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinProc;
wndclass.lpszClassName="shenhao";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0 ;
}
LRESULT CALLBACK WinProc(
 HWND hwnd, // handle to window
 UINT uMsg, // message identifier
 WPARAM wParam, // first message parameter
 LPARAM lParam // second message parameter
 )

{   
HDC hdc;
PAINTSTRUCT ps;
static int i=0;
static int j=10;
static int k=0;
static int m=10;
switch(uMsg)
{
case WM_CREATE:
SetTimer(hwnd,1,100,NULL);


case WM_PAINT:
hdc=BeginPaint(hwnd, &ps);
switch(nDirect)
{
case 0:
{
RECT rt;
GetClientRect(hwnd, &rt);
if(i==0)
{
Pie(hdc,100+j,100,150+j,150,150+j,100,150+j,150);
}
else
{
Pie(hdc,100+j,100,150+j,150,150+j,125,150+j,125);   
}
i=(i+1)%2;
if(j<rt.right-150)
{
j=(j+10)%1000;
}
}break;
case 1:
{
RECT rt1;
GetClientRect(hwnd, &rt1);
if(k==0)
{
Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,150,100+j-m,100);
}
else
{
Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,125,100+j-m,125);   
}
k=(k+1)%2;
if(100+j-m>rt1.left)
{
m=(m+10)%1000;
}
}
}

EndPaint(hwnd,&ps);

break;
case WM_KEYDOWN:
{   
switch(wParam)
{  
case VK_LEFT:nDirect = 1;break;
}
}
break;
case WM_TIMER:

InvalidateRect(hwnd,NULL,TRUE);
break;


case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

#2


哦  比我那个好多了 但nDirect的初值是多少啊?

#3


哦 我看到了 初值 谢谢你 你太强了 非常感谢啊 

#4


引用 3 楼 hao95740701 的回复:
哦 我看到了 初值 谢谢你 你太强了 非常感谢啊

客气了
就是给你一个思路
其实代码还可以优化

#5


没看懂,不过确实是高手!!!!

#6


还可以怎么优化啊。。指教 指教啊  高手 谢谢;额 

#7


向力天  高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了 

#8


顶一个!

#9


引用 7 楼 hao95740701 的回复:
向力天  高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

#10


我路过学习一下下!!~~~

#11


引用 9 楼 xianglitian 的回复:
引用 7 楼 hao95740701 的回复:

向力天 高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

我实在想不出来了  给一段代码  谢谢了 大哥 

#12


引用 9 楼 xianglitian 的回复:
引用 7 楼 hao95740701 的回复:

向力天 高手 给一个 你认为简单的思路被 我认为我这个 比较麻烦 要是再加上 向上 向下 向右 就麻烦了

其实上下左右无非就是方向的问题
绘图操作几乎是一样的
从这个角度去考虑
不需要每个方向都单独绘制
只需要控制好绘制变量即可

我按你给指导的 做下去 就是这样 但我感觉太复杂了 能给点意见吗 谢谢了 
#include<windows.h>
#include<stdio.h>



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

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_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WinProc;
    wndclass.lpszClassName="shenhao";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);
    HWND hwnd;
    hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0 ;
}
LRESULT CALLBACK WinProc(
                         HWND hwnd, // handle to window
                         UINT uMsg, // message identifier
                         WPARAM wParam, // first message parameter
                         LPARAM lParam // second message parameter
                         )

{   
    HDC hdc;
    PAINTSTRUCT ps;
    static int    i=0;
    static int    j=10;
    static int    k=0;
    static int    m=10;
static int    nDirect=0;
static int    a = 10;
static int    b=0;
RECT rt;
    switch(uMsg)
    {
    case WM_CREATE:
        SetTimer(hwnd,1,100,NULL);


    case WM_PAINT:
        hdc=BeginPaint(hwnd, &ps);
        switch(nDirect)
        {
        case 0:
            {
                 
                GetClientRect(hwnd, &rt);
                if(i==0)
                {
                    Pie(hdc,100+j,100,150+j,150,150+j,100,150+j,150);
                }
                else
                {
                    Pie(hdc,100+j,100,150+j,150,150+j,125,150+j,125);   
                }
                i=(i+1)%2;
                if(j<rt.right-150)
                {
                    j=(j+10)%1000;
                }
            }break;
        case 1:
            {
                
                GetClientRect(hwnd, &rt);
                if(k==0)
                {
                    Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,150,100+j-m,100);
                }
                else
                {
                    Pie(hdc,100+j-m,100,150+j-m,150,100+j-m,125,100+j-m,125);   
                }
                k=(k+1)%2;
                if(100+j-m>rt.left)
                {
                    m=(m+10)%1000;
                }
            }break;
case 2:
            {
                
                GetClientRect(hwnd, &rt);
                if(b==0)
                {
                    Pie(hdc,100+j-m,100-a,150+j-m,150-a,100+j-m,100-a,150+j-m,100-a);
                }
                else
                {
                    Pie(hdc,100+j-m,100-a,150+j-m,150-a,125+j-m,100-a,125+j-m,100-a);   
                }
                b=(b+1)%2;
                if(100-a>rt.top)
                {
                    a=(a+10)%1000;
                }
            }break;
        }

        EndPaint(hwnd,&ps);

        break;
    case WM_KEYDOWN:
        {   
            switch(wParam)
            {  
            case VK_LEFT:
nDirect = 1;
break;
case VK_UP:
nDirect = 2;
break;
case VK_DOWN:
nDirect = 3;
break;
case VK_RIGHT:
break;
nDirect = 4;
            }
        }
        break;
    case WM_TIMER:

        InvalidateRect(hwnd,NULL,TRUE);
        break;


    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

#13



#include<windows.h>
#include<stdio.h>

int    nDirect    = 2;

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

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_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WinProc;
    wndclass.lpszClassName="shenhao";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);
    HWND hwnd;
    hwnd=CreateWindow("shenhao","加油",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0 ;
}
LRESULT CALLBACK WinProc(
                         HWND hwnd, // handle to window
                         UINT uMsg, // message identifier
                         WPARAM wParam, // first message parameter
                         LPARAM lParam // second message parameter
                         )

{   
    HDC hdc;
    PAINTSTRUCT ps;
    static int    i=0;
    static int    X=100;
    static int    Y=100;
    switch(uMsg)
    {
    case WM_CREATE:
        SetTimer(hwnd,1,100,NULL);


    case WM_PAINT:
        hdc=BeginPaint(hwnd, &ps);
RECT rt;
        GetClientRect(hwnd, &rt);
        switch(nDirect)
        {
case 0:
{
 if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y-25,X+25,Y-25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X,Y-25,X,Y-25);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }
 break;
}
case 1:
{
 if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y+25,X-25,Y+25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X,Y+25,X,Y+25);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    Y=(Y+10)%1000;
                }
 break;
}
        case 2:
            {
                
                if(i==0)
                {
                    
Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y-25,X+25,Y+25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X+25,Y,X+25,Y);   
                }

                i=(i+1)%2;
                 if(X<rt.right-150)
                {
                    X=(X+10)%1000;
                }

            }break;
        case 3:
            {
           
               if(i==0)
                {
                    Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y+25,X-25,Y-25);
                }
                else
                {
                     Pie(hdc,X-25,Y-25,X+25,Y+25,X-25,Y,X-25,Y);   
                }

                i=(i+1)%2;


                 if(X<rt.right-150)
                {
                    X=(X-10)%1000;
                }
            }
break;
        }

        EndPaint(hwnd,&ps);

        break;
    case WM_KEYDOWN:
        {   
            switch(wParam)
            {
            case VK_UP:nDirect = 0;break;
            case VK_DOWN:nDirect = 1;break;
case VK_RIGHT:nDirect = 2;break;
            case VK_LEFT:nDirect = 3;break;
            }
        }
        break;
    case WM_TIMER:

        InvalidateRect(hwnd,NULL,TRUE);
        break;


    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

稍微改了下,其中X,Y表示矩形的中点,根据中点就可以推算出pie的每个参数,至于越界处理就没做了,楼主自己改下吧

#14


有2个地方复制的时候忘了改
if(X<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }

             应该是
if(Y<rt.right-150)
                {
                    Y=(Y-10)%1000;
                }