鱼C论坛

 找回密码
 立即注册
查看: 4319|回复: 2

求win32 api一条直线或者一个方格的画法,不胜感激!!!

[复制链接]
发表于 2012-8-12 11:14:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面是一个树根图形,我实在是看不懂啊,求哪位好心人把它改成一条直线或方格,让我知道是怎样实现的,或许我知道画方块就可以编写方块的移动,再就可以编写小游戏了,谢谢了!!!
#include <windows.h>
#include <math.h>

#define AD 3.14159265*45/180//相邻树枝的旋转角度
#define Gold 0.618//相邻树枝的长度比例 范围0-1
#define Len 100 //树根的长度

HDC hdc;
struct Node
{
        double x;//节点坐标x
        double y;//节点坐标y
        double AM;//节点的左右分支的中线方向
        double Length;//节点的原支长度
        struct Node *LeftNode;//左分支
        struct Node *RightNode;//右分支
};

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void DL(double x1,double y1,double x2,double y2);
void DrawNode(struct Node *ThisNode);
void DrawTree (void);

//画一条直线
void DL(double x1,double y1,double x2,double y2)
{
        MoveToEx (hdc, x1, y1, NULL) ;
        LineTo (hdc, x2, y2) ;
}

void DrawNode(struct Node *ThisNode)
{
        //左分支的地址、角度、长度、坐标
        ThisNode->LeftNode=(struct Node *)malloc(sizeof(struct Node));
        ThisNode->LeftNode->AM =ThisNode->AM +AD;
        ThisNode->LeftNode->Length =ThisNode->Length *Gold;
        ThisNode->LeftNode->x=ThisNode->x+cos(ThisNode->LeftNode->AM)*ThisNode->LeftNode->Length;
        ThisNode->LeftNode->y=ThisNode->y+sin(ThisNode->LeftNode->AM)*ThisNode->LeftNode->Length;
        //右分支的地址、角度、长度、坐标
        ThisNode->RightNode=(struct Node *)malloc(sizeof(struct Node));
        ThisNode->RightNode->AM =ThisNode->AM -AD;
        ThisNode->RightNode->Length =ThisNode->Length *Gold;
        ThisNode->RightNode->x=ThisNode->x+cos(ThisNode->RightNode->AM)*ThisNode->RightNode->Length;
        ThisNode->RightNode->y=ThisNode->y+sin(ThisNode->RightNode->AM)*ThisNode->RightNode->Length;

        //画图

        DL(ThisNode->x,ThisNode->y,ThisNode->LeftNode->x,ThisNode->LeftNode->y);
        DL(ThisNode->x,ThisNode->y,ThisNode->RightNode->x,ThisNode->RightNode->y);
        //递归终点
        if(ThisNode->Length>5 )
        {
                DrawNode(ThisNode->LeftNode);
                DrawNode(ThisNode->RightNode);//
        }
}

void DrawTree (void)
{
        struct Node TreeRoot;

        TreeRoot.x =500;//树根的横坐标
        TreeRoot.y =Len;
        TreeRoot.AM =3.14159265/2;
        TreeRoot.Length =Len;

        DrawNode(&TreeRoot);
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

        PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT ("SineWave") ;
        HWND hwnd ;
        MSG msg ;
        WNDCLASS wndclass ;

        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 ("Program requires Windows NT!"),
                        szAppName, MB_ICONERROR) ;
                return 0 ;
        }

        hwnd = CreateWindow ( szAppName, TEXT ("Sine Wave Using Polyline"),
                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)
{
        PAINTSTRUCT ps ;
        switch (message)
        {
        case WM_SIZE:

                return 0 ;

        case WM_PAINT:

                hdc = BeginPaint (hwnd, &ps) ;

                DrawTree();
                return 0 ;

        case WM_DESTROY:
                PostQuitMessage (0) ;
                return 0 ;
        }

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

}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-12 14:23:32 | 显示全部楼层
#include   <windows.h>
#include   "stdio.h"
LRESULT   CALLBACK   WndProc(HWND,UINT,WPARAM,LPARAM);
int   WINAPI   WinMain(HINSTANCE   hInstance,                  //WinMain函数
                       HINSTANCE   hPrevInstance,
                       PSTR   szCmdLine,
                       int   iCmdShow)
{               static char szappname[]="9-1";                 //窗口类名
                HWND   hwnd;                                   //窗口句柄
                MSG msg;                                       //windows消息
                WNDCLASSEX   cs;                               //窗口类,可以使WNDCLASS   
                cs.cbSize=    sizeof(cs);                      //窗口数据大小
                cs.style   =CS_HREDRAW|CS_VREDRAW;             //窗口类型         
                cs.lpfnWndProc = WndProc;                      //窗口处理函数为WndProc
                cs.cbClsExtra   =0;                            //窗口类无扩展
                cs.cbWndExtra   =0;                            //窗口实例无扩展
                cs.hInstance   =hInstance;                      //当前实例句柄
                cs.hIcon     =LoadIcon(NULL,IDI_APPLICATION);   //窗口最小化图标为默认图标
                cs.hCursor   =LoadCursor(NULL,IDC_ARROW);         //窗口采用箭头光标
                cs.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);  //窗口背景为白色
                cs.lpszMenuName   =NULL;                                 //窗口无菜单
                cs.lpszClassName   =szappname;                           //窗口类名为..
                cs.hIconSm   =LoadIcon(NULL,IDI_APPLICATION);        
                /* 以下是窗口类的注册 */
                if(! RegisterClassEx(&cs))            //如果注册失败则发出警告
                {
                MessageBeep(0);
                return FALSE;
                }
               
                hwnd=CreateWindow(szappname,            //窗口类名
                                  "9-1",                //窗口标题名
                                  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   iMsg,           //代表所处理消息的销售值
                             WPARAM   wParam,     //消息的附加参数
                             LPARAM   lParam)     //消息的附加参数
{
    HDC hdc;
        HPEN hPen;//定义画笔的句柄
    PAINTSTRUCT pstr;

       
       
       
       
       
        switch   (iMsg)
         {
            case   WM_DESTROY:
                 PostQuitMessage(0);                  //调用函数 PostQuitMessage退出程序
                 return   0;

                        case   WM_PAINT:
                                        hdc=BeginPaint(hwnd,&pstr);//开始画图
                                        //hPen=(HPEN)GetStockObject(PS_SOLID);
                                        hPen=CreatePen(PS_SOLID,2,RGB(255,0,0));//定义画
                                        SelectObject(hdc,hPen); //选择画笔
                                        MoveToEx(hdc,50,50,NULL);    ////画线起点
                                        LineTo(hdc,90,100);    ////画线
                                    Rectangle(hdc,20,500,300,20);//////矩形
                                 

                                        DeleteObject(hPen);//删除画笔
           return 0;

         }






    return DefWindowProc(hwnd,iMsg,wParam,lParam);
                              
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-12 14:26:34 | 显示全部楼层
http://blog.csdn.net/jesse1013000/article/details/1334630
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-14 15:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表