首页
社区
课程
招聘
[原创]windows程序设计:哆啦A梦
发表于: 2011-8-11 20:09 25322

[原创]windows程序设计:哆啦A梦

2011-8-11 20:09
25322
程序运行时效果:


程序写得比较麻烦,在学习《windows程序设计》GDI章解的练习,给大家娱乐,下面是源代码:

/*--------------------------------------------
                                        机器猫.C
---------------------------------------------*/

#include <windows.h>
#include <math.h>

#define NUM                1000
#define TWOPI        (2 * 3.1415926)

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT ("HelloWin") ;
        HWND         hwnd ;
        MSG          msg ;
    WNDCLASS     wndclass ;
        int cx, cy ;
        cx = GetSystemMetrics(SM_CXSCREEN) / 2 - 200;
        cy = GetSystemMetrics(SM_CYSCREEN) / 2 - 200;

        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 (/*GRAY*/WHITE_BRUSH) ;
        wndclass.lpszMenuName  = NULL ;
        wndclass.lpszClassName = szAppName ;
       
        if (!RegisterClass (&wndclass))
        {
                MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                        szAppName, MB_ICONERROR) ;
                return 0 ;
        }
       
        RegisterClass (&wndclass);
       
        hwnd = CreateWindow (szAppName,                  // window class name
                TEXT ("The Hello Program"), // window caption
                WS_OVERLAPPEDWINDOW,        // window style
                cx,                                                         // initial x position
                cy,                                                         // initial y position
                400,                                                        // initial x size
                400,                                                        // 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 ;
        static int cxClient, cyClient ;
        HBRUSH hBrush ;
        int i ;
        POINT apt[NUM],
                leftHand[] = {
                                           cxClient / 2 - 60, cyClient / 2 - 20,
                                           cxClient / 2 - 100, cyClient / 2 + 10,
                                           cxClient / 2 - 100, cyClient / 2 + 30,
                                           cxClient / 2 - 60, cyClient / 2 + 10
                                        },
                rightHand[] = {
                                                cxClient / 2 + 60, cyClient / 2 - 20,
                                                cxClient / 2 + 100, cyClient / 2 + 10,
                                                cxClient / 2 + 100, cyClient / 2 + 30,
                                                cxClient / 2 + 60, cyClient / 2 + 10
                } ;

        switch (message)
        {
        case WM_SIZE:
                cxClient = LOWORD (lParam) ;
                cyClient = HIWORD (lParam) + 120;
                return 0 ;

        case WM_PAINT:
                hdc = BeginPaint (hwnd, &ps) ;

                Ellipse (hdc, cxClient / 2 - 70, cyClient / 2 + 55, cxClient / 2, cyClient / 2 + 85) ;                        //脚
                Ellipse (hdc, cxClient / 2, cyClient / 2 + 55, cxClient / 2 + 70, cyClient / 2 + 85) ;

                hBrush = CreateSolidBrush (RGB (100, 150, 255)) ;
                SelectObject (hdc, hBrush) ;
               
//                Rectangle (hdc, cxClient / 2 - 60, cyClient / 2 - 60, cxClient / 2 +60, cyClient / 2 + 70);                //身体
                RoundRect (hdc, cxClient / 2 - 60, cyClient / 2 - 60, cxClient / 2 + 60, cyClient / 2 + 70, 50, 20);

                DeleteObject (hBrush) ;

                hBrush = GetStockObject (WHITE_BRUSH) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 50, cyClient / 2 - 50, cxClient / 2 + 50, cyClient / 2 + 50) ;                //肚子部分
               
                hBrush = CreateSolidBrush (RGB (100, 150, 255)) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 100, cyClient / 2 - 220, cxClient / 2 +100, cyClient / 2 - 20) ;        //头部

                DeleteObject (hBrush) ;
               
                hBrush = GetStockObject (WHITE_BRUSH) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 85, cyClient / 2 - 170, cxClient / 2 + 85, cyClient / 2 - 20) ;        //脸部
                Ellipse (hdc, cxClient / 2 - 40, cyClient / 2 - 190, cxClient / 2, cyClient / 2 - 140 ) ;                //眼眶左
                Ellipse (hdc, cxClient / 2, cyClient / 2 - 190, cxClient / 2 + 40, cyClient /2 - 140) ;                        //眼眶右

                hBrush = GetStockObject (BLACK_BRUSH) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 15, cyClient / 2 - 155, cxClient / 2 -10, cyClient / 2 - 150) ;        //眼睛左
                Ellipse (hdc, cxClient / 2 + 10, cyClient / 2 - 155, cxClient / 2 + 15, cyClient / 2 -150) ;        //眼睛右
               
                DeleteObject (hBrush) ;

                hBrush = CreateSolidBrush (RGB (255, 0, 0)) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 10, cyClient / 2 - 150, cxClient / 2 +10, cyClient / 2 -130) ;                //鼻子

                MoveToEx (hdc, cxClient / 2, cyClient / 2 -130, NULL) ;
                LineTo (hdc, cxClient / 2, cyClient / 2 - 60) ;

                MoveToEx (hdc, cxClient / 2 - 40, cyClient / 2 - 75, NULL);                                        //嘴
                for (i = 0; i < NUM / 2; i++)
                {
                        apt[i].x = cxClient / 2 - 40 + i * 160 / NUM ;
                        apt[i].y = cyClient / 2 - 75 +(int) (30 * sin (TWOPI * i / NUM)) / 2 ;
                        LineTo(hdc, apt[i].x, apt[i].y) ;
                }
               
                MoveToEx (hdc, cxClient / 2 - 60, cyClient / 2 - 140, NULL) ;                                //胡须
                LineTo (hdc, cxClient / 2 - 20, cyClient / 2 - 120) ;

                MoveToEx (hdc, cxClient / 2 - 60, cyClient / 2 - 110, NULL) ;
                LineTo (hdc, cxClient / 2 - 20, cyClient / 2 - 110) ;

                MoveToEx (hdc, cxClient / 2 - 60, cyClient / 2 - 80, NULL);
                LineTo (hdc, cxClient / 2 - 20, cyClient / 2 - 100) ;

                MoveToEx (hdc, cxClient / 2 + 60, cyClient / 2 -140, NULL) ;
                LineTo (hdc, cxClient / 2 + 20, cyClient / 2 - 120) ;

                MoveToEx (hdc, cxClient / 2 + 60, cyClient / 2 - 110, NULL) ;
                LineTo (hdc, cxClient / 2 +20, cyClient / 2 - 110) ;

                MoveToEx (hdc, cxClient / 2 +60, cyClient / 2 - 80, NULL) ;
                LineTo (hdc, cxClient / 2 + 20, cyClient / 2 - 100) ;

                hBrush = GetStockObject (WHITE_BRUSH) ;                                                                                //口袋
                SelectObject (hdc, hBrush) ;

                Chord (hdc, cxClient / 2 - 40, cyClient / 2 - 40, cxClient / 2 + 40, cyClient / 2 +40,
                        cxClient / 2 - 40, cyClient / 2 + 10, cxClient / 2 +40, cyClient / 2 + 10) ;

               

                hBrush = CreateSolidBrush (RGB (255, 0, 0));                                                                //脖子上的套圈
                SelectObject (hdc, hBrush) ;

                RoundRect (hdc, cxClient / 2 - 70, cyClient / 2 - 40, cxClient / 2 + 70, cyClient / 2 - 20, 20, 20);

                DeleteObject (hBrush) ;

                hBrush = CreateSolidBrush (RGB (100, 150, 255)) ;                                                        //手臂
                SelectObject (hdc, hBrush) ;

                SetPolyFillMode (hdc, WINDING) ;
                Polygon (hdc, leftHand, 4) ;

                SetPolyFillMode (hdc, WINDING) ;
                Polygon (hdc, rightHand, 4) ;

                DeleteObject (hBrush) ;

                hBrush = GetStockObject (WHITE_BRUSH) ;                                                                                //手
                SelectObject (hdc, hBrush) ;
               
                Ellipse (hdc, cxClient / 2 - 115, cyClient / 2 + 5, cxClient / 2 - 85, cyClient / 2 + 35) ;
                Ellipse (hdc, cxClient / 2 + 115, cyClient / 2 + 5, cxClient / 2 + 85, cyClient / 2 + 35) ;

                hBrush = CreateSolidBrush (RGB (250, 255, 150)) ;                                                        //铃铛
                SelectObject (hdc, hBrush) ;
               
                Ellipse (hdc, cxClient / 2 - 10, cyClient / 2 - 35, cxClient / 2 + 10, cyClient / 2 - 15) ;

                DeleteObject (hBrush) ;

                hBrush = GetStockObject (BLACK_BRUSH) ;
                SelectObject (hdc, hBrush) ;

                Ellipse (hdc, cxClient / 2 - 4, cyClient / 2 - 29, cxClient / 2 + 4, cyClient / 2 - 21) ;

                MoveToEx (hdc, cxClient / 2, cyClient / 2 - 25, NULL) ;
                LineTo (hdc, cxClient / 2, cyClient / 2 - 15) ;

                EndPaint (hwnd, &ps) ;
                return 0 ;

        case WM_DESTROY:
                PostQuitMessage (0) ;
                return 0 ;

        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 7
支持
分享
最新回复 (47)
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
2
不错,嘿嘿,敢问楼主右上角的最大最小关闭安全你是贴图实现的?我看不是标准的按钮啊。。。。
2011-8-11 20:58
0
雪    币: 49
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
挺有意思的。不错,支持一下。
2011-8-11 20:58
0
雪    币: 53
活跃值: (56)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
4
那一个是我桌面主题的原因。
2011-8-11 21:03
0
雪    币: 34
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
哈哈很有意思....有耐力...
2011-8-11 21:43
0
雪    币: 128
活跃值: (111)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
有毅力,GDI这部分我看了看就略过了,关于窗口重绘那几个消息函数还是模模糊糊。
2011-8-11 22:20
0
雪    币: 793
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
楼主有想法 很好
2011-8-12 08:04
0
雪    币: 232
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
呵呵,不错,我都不会画图的,汗,
2011-8-13 21:19
0
雪    币: 321
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
呵呵 好玩。。。
2011-8-15 19:13
0
雪    币: 38
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
以前看过用C语言话太极图、玫瑰花的  ,、
  现在又开眼界了,顶!!
2011-8-15 19:40
0
雪    币: 113
活跃值: (100)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
12
good idea, i learned this book before but then i didn't have this idea to draw an interesting figure by using GDI.
2011-8-15 21:34
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
13
我插,厉害,学习了,Windows使用的最烂的就是画画了.谢谢你
2011-8-15 22:16
0
雪    币: 203
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
学了C,但却又没学C,教育的悲哀。
个人努力最重要。
2011-8-15 22:45
0
雪    币: 878
活跃值: (496)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
15
哈哈,用笔也不见得画的出来
2011-8-16 11:43
0
雪    币: 27
活跃值: (90)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
16
很好玩 :)
2011-8-16 12:52
0
雪    币: 322
活跃值: (113)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
楼主厉害!!!
2011-8-16 12:55
0
雪    币: 1040
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
强。。。。。。
2011-8-16 14:19
0
雪    币: 270
活跃值: (97)
能力值: ( LV8,RANK:140 )
在线值:
发帖
回帖
粉丝
19
非常赞啊,不知道楼主算这些坐标点算了多久啊
2011-8-16 19:35
0
雪    币: 57
活跃值: (169)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
20
楼主厉害!!!!佩服!!
2011-8-16 19:38
0
雪    币: 53
活跃值: (56)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
21
一共写了一下午,坐标就是估计的……没去好好算,感觉差不多就写上去了……
2011-8-16 21:29
0
雪    币: 47
活跃值: (36)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
22
类似汤姆猫么
2011-8-16 21:42
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
23
下载来学习,对GDI不太熟悉,多谢了!
2011-8-16 23:14
0
雪    币: 166
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
有创意!我还是自己研究下!GDI我很在行的!哈哈
2011-8-17 09:15
0
雪    币: 1660
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
25
最头疼画画,坐标总是算不好,头疼死了。
2011-8-17 11:21
0
游客
登录 | 注册 方可回帖
返回
//