| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 34600 人关注过本帖, 106 人收藏
标题:发布个《懒人学C语言》--速成版 此贴有效
只看楼主 加入收藏
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
程序代码:
// 用一个BMP文件为窗口绘制背景
#include <windows.h>

HBITMAP hBmp = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_ERASEBKGND:
        {
            HDC hDC = GetDC(hWnd);

            RECT rect;
            GetClientRect(hWnd, &rect);

            HBRUSH brush = CreatePatternBrush(hBmp);
          
            HBRUSH pOldBrush = (HBRUSH)SelectObject(hDC, brush);

            RoundRect(hDC, rect.left, rect.top, rect.right, rect.bottom, 0, 0);
          
            SelectObject(hDC, pOldBrush);

            ReleaseDC(hWnd, hDC);    // 释放DC
          
            return 1;
        }
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

// 注册窗口信息
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
  
    wcex.cbSize = sizeof(WNDCLASSEX);                // WNDCLASSEX 的大小。
  
    wcex.style            = CS_HREDRAW | CS_VREDRAW;    // 从这个窗口类派生的窗口具有的风格。
    wcex.lpfnWndProc    = (WNDPROC)WndProc;            // 窗口处理函数的指针
    wcex.cbClsExtra        = 0;                        // 指定紧跟在窗口类结构后的附加字节数。
    wcex.cbWndExtra        = 0;                        // 指定紧跟在窗口事例后的附加字节数。
    wcex.hInstance        = hInstance;                // 本模块的事例句柄。
    wcex.hIcon            = NULL;                        // 图标的句柄。
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);    // 光标的句柄。
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW);        // 背景画刷的句柄。
    wcex.lpszMenuName    = NULL;                            // 指向菜单的指针。
    wcex.lpszClassName    = "STATIC";                        // 指向类名称的指针。
    wcex.hIconSm        = NULL;                            // 和窗口类关联的小图标。
  
    return RegisterClassEx(&wcex);        // 注册窗口
}

void main()
{
    MyRegisterClass(GetModuleHandle(NULL));

    // 创建窗口
    HWND hWnd = CreateWindow("STATIC", "XX", WS_OVERLAPPEDWINDOW, 100, 100, 400, 300, NULL, NULL, NULL, NULL);
    if (hWnd == NULL)
        return;
    ShowWindow(hWnd, SW_SHOW);

    hBmp = (HBITMAP)LoadImage(NULL, "xxx.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    // 显示窗口要立即让窗口进行一次重绘
    InvalidateRect(hWnd, NULL, TRUE);

    // 建立windows消息处理循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册

XXX.BMP:
xxx.rar (3.42 KB)




[ 本帖最后由 闲出屁 于 2012-2-5 01:30 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
// window消息宏定义都是以WM_开头,每个消息的wparam和lparam用途不同,具体参与MSDN

程序代码:
// 处理windows消息函数中
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONDOWN:    // 鼠标左键按下
    case WM_KEYDOWN:        // 键盘按下
    case WM_SIZE:            // 窗口改变大小
    case WM_TIMER:            // 计时器
    case WM_COMMAND:        // 按钮
        break;
        // 等等,有很多,具体查阅MSDN
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

windows消息列表.rar (18.71 KB)


[ 本帖最后由 闲出屁 于 2012-2-6 21:52 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
程序代码:
// 用钩子做一个简单的监听整个系统键盘按下的外挂
// 如果想监听某一个程序,需要把监听函数放到一个dll里
#include <windows.h>
#include <stdio.h>

HHOOK hook = NULL;

#ifndef WH_KEYBOARD_LL
    #define WH_KEYBOARD_LL 13
    /*
     * Structure used by WH_KEYBOARD_LL
     */
    typedef struct tagKBDLLHOOKSTRUCT {
        DWORD   vkCode;
        DWORD   scanCode;
        DWORD   flags;
        DWORD   time;
        DWORD   dwExtraInfo;
    } KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
#endif

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LPKBDLLHOOKSTRUCT pKb = (LPKBDLLHOOKSTRUCT)lParam;
    printf("%c-%d\n", pKb->vkCode, pKb->vkCode);
    return CallNextHookEx(hook, nCode, wParam, lParam);
}
void main()
{
    hook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL),  NULL);
    if (hook == NULL)
    {
        printf("监听失败!\n");
        return;
    }
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册

Windows应用程序捆绑核心编程.rar (199.74 KB)


[ 本帖最后由 闲出屁 于 2012-2-6 21:53 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
程序代码:
// 让我们来听首歌!
#include <conio.h>
#include <windows.h>
#include <vfw.h>
#include <stdio.h>

#pragma comment(lib,"vfw32.lib")
#pragma comment(lib,"winmm.lib")

void main()
{
    // 选择要播放的音乐文件
    char szOpenFileNames[80*MAX_PATH] = {0};
    char szPath[MAX_PATH] = "";  
    char szFileName[80*MAX_PATH] = "";   

    OPENFILENAME   ofn;
    ZeroMemory( &ofn, sizeof(ofn) );
    ofn.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT;
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = szOpenFileNames;           // lpstrFile 赋值给szOpenFileNames 包含打开的文件路
    ofn.nMaxFile = sizeof(szOpenFileNames);    // 传了指针给结构体之后,必须再传一个字符串最大位数给结构体,否则打开对话框不会弹出 
  
    GetOpenFileName(&ofn);

    // 打开选择的音乐
    MCI_OPEN_PARMS mciOpen;
    mciOpen.lpstrDeviceType = "mpegvideo";
    mciOpen.lpstrElementName = ofn.lpstrFile;
    MCIERROR mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen);
    if (mciError)
    {
        printf("mci播放失败!\n");
        return;
    }

    // 播放打开的音乐
    UINT DeviceID = mciOpen.wDeviceID ;
    MCI_PLAY_PARMS mciPlay; 
    mciError = mciSendCommand(DeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay);
    if (mciError)
    {
        printf("mci播放失败!\n");
        return;
    }

    getch();
}



[ 本帖最后由 闲出屁 于 2012-2-5 02:42 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
OpenglBase.rar (506.02 KB)

如何安装OPENGL:
下载下面的压缩包,然后把所有的dll放到C:\WINDOWS\system32下 把lib和h全放到你当前用的sdk目录下
这个目录可以通过vc菜单--> Tools --> Options --> Directories-->
    Include files里放的是.h
    Library files里放的是.lib
也就是把压缩包里的所有.h文件全部分放到incule文件夹下的gl文件夹里(一般这么做)
把所有.lib文件放到lib文件夹下

当然,在安装前先确认下你是否已经安装,一般编译器都会自动安装这个东西!

在创建工程时,要在右键项目的setting里 对其Link下的Object/library Modules:下的文本输入框中,引入增加:opengl32.lib glu32.lib glaux.lib 这三项


[ 本帖最后由 闲出屁 于 2012-2-5 03:22 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
程序代码:
// 在控制台中绘制GL三维图形--控制台也疯狂
// 更多代码可以去网上下载,例如搜nehe源码
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

HDC hDC = NULL;
HGLRC hRC=NULL;
GLfloat    rtri;                // Angle For The Triangle ( NEW )
GLfloat    rquad;                // Angle For The Quad ( NEW )

HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
    HWND hwndFound;         // This is what is returned to the caller.
    char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
    char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
 
    GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

 
    wsprintf(pszNewWindowTitle,"%d/%d",
        GetTickCount(),
        GetCurrentProcessId());

 
    SetConsoleTitle(pszNewWindowTitle);

 
    Sleep(40);

 
    hwndFound=FindWindow(NULL, pszNewWindowTitle);

 
    SetConsoleTitle(pszOldWindowTitle);

 
    return(hwndFound);
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)        // Resize And Initialize The GL Window
{
    if (height==0)                                        // Prevent A Divide By Zero By
    {
        height=1;                                        // Making Height Equal One
    }
  
    glViewport(0,0,width,height);                        // Reset The Current Viewport
  
    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                    // Reset The Projection Matrix
  
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  
    glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
    glLoadIdentity();                                    // Reset The Modelview Matrix
}

int InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
{
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                // Black Background
    glClearDepth(1.0f);                                    // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
    return TRUE;                                        // Initialization Went OK
}

int DrawGLScene(GLvoid)                                    // Here's Where We Do All The Drawing
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
    glLoadIdentity();                                    // Reset The Current Modelview Matrix
    glTranslatef(-1.5f,0.0f,-6.0f);                        // Move Left 1.5 Units And Into The Screen 6.0
    glRotatef(rtri,0.0f,1.0f,0.0f);                        // Rotate The Triangle On The Y axis ( NEW )
    glBegin(GL_TRIANGLES);                                // Start Drawing A Triangle
    glColor3f(1.0f,0.0f,0.0f);                        // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                    // Top Of Triangle (Front)
    glColor3f(0.0f,1.0f,0.0f);                        // Green
    glVertex3f(-1.0f,-1.0f, 1.0f);                    // Left Of Triangle (Front)
    glColor3f(0.0f,0.0f,1.0f);                        // Blue
    glVertex3f( 1.0f,-1.0f, 1.0f);                    // Right Of Triangle (Front)
    glColor3f(1.0f,0.0f,0.0f);                        // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                    // Top Of Triangle (Right)
    glColor3f(0.0f,0.0f,1.0f);                        // Blue
    glVertex3f( 1.0f,-1.0f, 1.0f);                    // Left Of Triangle (Right)
    glColor3f(0.0f,1.0f,0.0f);                        // Green
    glVertex3f( 1.0f,-1.0f, -1.0f);                    // Right Of Triangle (Right)
    glColor3f(1.0f,0.0f,0.0f);                        // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                    // Top Of Triangle (Back)
    glColor3f(0.0f,1.0f,0.0f);                        // Green
    glVertex3f( 1.0f,-1.0f, -1.0f);                    // Left Of Triangle (Back)
    glColor3f(0.0f,0.0f,1.0f);                        // Blue
    glVertex3f(-1.0f,-1.0f, -1.0f);                    // Right Of Triangle (Back)
    glColor3f(1.0f,0.0f,0.0f);                        // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                    // Top Of Triangle (Left)
    glColor3f(0.0f,0.0f,1.0f);                        // Blue
    glVertex3f(-1.0f,-1.0f,-1.0f);                    // Left Of Triangle (Left)
    glColor3f(0.0f,1.0f,0.0f);                        // Green
    glVertex3f(-1.0f,-1.0f, 1.0f);                    // Right Of Triangle (Left)
    glEnd();                                            // Done Drawing The Pyramid
  
    glLoadIdentity();                                    // Reset The Current Modelview Matrix
    glTranslatef(1.5f,0.0f,-7.0f);                        // Move Right 1.5 Units And Into The Screen 7.0
    glRotatef(rquad,1.0f,1.0f,1.0f);                    // Rotate The Quad On The X axis ( NEW )
    glBegin(GL_QUADS);                                    // Draw A Quad
    glColor3f(0.0f,1.0f,0.0f);                        // Set The Color To Green
    glVertex3f( 1.0f, 1.0f,-1.0f);                    // Top Right Of The Quad (Top)
    glVertex3f(-1.0f, 1.0f,-1.0f);                    // Top Left Of The Quad (Top)
    glVertex3f(-1.0f, 1.0f, 1.0f);                    // Bottom Left Of The Quad (Top)
    glVertex3f( 1.0f, 1.0f, 1.0f);                    // Bottom Right Of The Quad (Top)
    glColor3f(1.0f,0.5f,0.0f);                        // Set The Color To Orange
    glVertex3f( 1.0f,-1.0f, 1.0f);                    // Top Right Of The Quad (Bottom)
    glVertex3f(-1.0f,-1.0f, 1.0f);                    // Top Left Of The Quad (Bottom)
    glVertex3f(-1.0f,-1.0f,-1.0f);                    // Bottom Left Of The Quad (Bottom)
    glVertex3f( 1.0f,-1.0f,-1.0f);                    // Bottom Right Of The Quad (Bottom)
    glColor3f(1.0f,0.0f,0.0f);                        // Set The Color To Red
    glVertex3f( 1.0f, 1.0f, 1.0f);                    // Top Right Of The Quad (Front)
    glVertex3f(-1.0f, 1.0f, 1.0f);                    // Top Left Of The Quad (Front)
    glVertex3f(-1.0f,-1.0f, 1.0f);                    // Bottom Left Of The Quad (Front)
    glVertex3f( 1.0f,-1.0f, 1.0f);                    // Bottom Right Of The Quad (Front)
    glColor3f(1.0f,1.0f,0.0f);                        // Set The Color To Yellow
    glVertex3f( 1.0f,-1.0f,-1.0f);                    // Top Right Of The Quad (Back)
    glVertex3f(-1.0f,-1.0f,-1.0f);                    // Top Left Of The Quad (Back)
    glVertex3f(-1.0f, 1.0f,-1.0f);                    // Bottom Left Of The Quad (Back)
    glVertex3f( 1.0f, 1.0f,-1.0f);                    // Bottom Right Of The Quad (Back)
    glColor3f(0.0f,0.0f,1.0f);                        // Set The Color To Blue
    glVertex3f(-1.0f, 1.0f, 1.0f);                    // Top Right Of The Quad (Left)
    glVertex3f(-1.0f, 1.0f,-1.0f);                    // Top Left Of The Quad (Left)
    glVertex3f(-1.0f,-1.0f,-1.0f);                    // Bottom Left Of The Quad (Left)
    glVertex3f(-1.0f,-1.0f, 1.0f);                    // Bottom Right Of The Quad (Left)
    glColor3f(1.0f,0.0f,1.0f);                        // Set The Color To Violet
    glVertex3f( 1.0f, 1.0f,-1.0f);                    // Top Right Of The Quad (Right)
    glVertex3f( 1.0f, 1.0f, 1.0f);                    // Top Left Of The Quad (Right)
    glVertex3f( 1.0f,-1.0f, 1.0f);                    // Bottom Left Of The Quad (Right)
    glVertex3f( 1.0f,-1.0f,-1.0f);                    // Bottom Right Of The Quad (Right)
    glEnd();                                            // Done Drawing The Quad
  
    rtri+=0.2f;                                            // Increase The Rotation Variable For The Triangle ( NEW )
    rquad-=0.15f;                                        // Decrease The Rotation Variable For The Quad ( NEW )
    return TRUE;                                        // Keep Going
}

void InitSet(HWND hConsole)
{
    GLuint    PixelFormat;
    static    PIXELFORMATDESCRIPTOR pfd=                // pfd Tells Windows How We Want Things To Be
    {
        sizeof(PIXELFORMATDESCRIPTOR),                // Size Of This Pixel Format Descriptor
            1,                                            // Version Number
            PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
            PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
            PFD_DOUBLEBUFFER,                            // Must Support Double Buffering
            PFD_TYPE_RGBA,                                // Request An RGBA Format
            16,                                            // Select Our Color Depth
            0, 0, 0, 0, 0, 0,                            // Color Bits Ignored
            0,                                            // No Alpha Buffer
            0,                                            // Shift Bit Ignored
            0,                                            // No Accumulation Buffer
            0, 0, 0, 0,                                    // Accumulation Bits Ignored
            16,                                            // 16Bit Z-Buffer (Depth Buffer)
            0,                                            // No Stencil Buffer
            0,                                            // No Auxiliary Buffer
            PFD_MAIN_PLANE,                                // Main Drawing Layer
            0,                                            // Reserved
            0, 0, 0                                        // Layer Masks Ignored
    };
  
    if (!(hDC=GetDC(hConsole)))                            // Did We Get A Device Context?
    {
        MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))    // Did Windows Find A Matching Pixel Format?
    {                  
        MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if(!SetPixelFormat(hDC,PixelFormat,&pfd))        // Are We Able To Set The Pixel Format?
    {
        MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if (!(hRC=wglCreateContext(hDC)))                // Are We Able To Get A Rendering Context?
    {
        MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
    {
        MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
}
void main ()
{
    HWND hConsole = GetConsoleHwnd();    // 获得控制台窗口句柄

    InitSet(hConsole);
  
    RECT rect;
    GetWindowRect(hConsole, &rect);
    ReSizeGLScene(rect.right-rect.left, rect.bottom-rect.top);

    InitGL();

    // 设置消息循环
    MSG msg;
    while(1)                                    // Loop That Runs While done=FALSE
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))    // Is There A Message Waiting?
        {
            if (msg.message==WM_QUIT)                // Have We Received A Quit Message?
            {
                break;                            // If So done=TRUE
            }
            else                                    // If Not, Deal With Window Messages
            {
                TranslateMessage(&msg);                // Translate The Message
                DispatchMessage(&msg);                // Dispatch The Message
            }
        }
        else                                        // If There Are No Messages
        {
            // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
            if (!DrawGLScene())    // Active?  Was There A Quit Received?
            {
                break;                        // ESC or DrawGLScene Signalled A Quit
            }
            else                                    // Not Time To Quit, Update Screen
            {
                SwapBuffers(hDC);                    // Swap Buffers (Double Buffering)
            }
        }
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册




[ 本帖最后由 闲出屁 于 2012-2-5 03:23 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
程序代码:
// 蝴蝶满天飞
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

HDC hDC = NULL;
HGLRC hRC=NULL;
GLfloat    rtri;                // Angle For The Triangle ( NEW )
GLfloat    rquad;                // Angle For The Quad ( NEW )

HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
    HWND hwndFound;         // This is what is returned to the caller.
    char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
    char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
 
    GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

 
    wsprintf(pszNewWindowTitle,"%d/%d",
        GetTickCount(),
        GetCurrentProcessId());

 
    SetConsoleTitle(pszNewWindowTitle);

 
    Sleep(40);

 
    hwndFound=FindWindow(NULL, pszNewWindowTitle);

 
    SetConsoleTitle(pszOldWindowTitle);

 
    return(hwndFound);
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)        // Resize And Initialize The GL Window
{
    if (height==0)                                        // Prevent A Divide By Zero By
    {
        height=1;                                        // Making Height Equal One
    }
  
    glViewport(0,0,width,height);                        // Reset The Current Viewport
  
    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                    // Reset The Projection Matrix
  
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  
    glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
    glLoadIdentity();                                    // Reset The Modelview Matrix
}

// User Defined Variables
GLuint texture[4];                                                // Storage For 3 Textures

struct object                                                    // Create A Structure Called Object
{
    int   tex;                                                    // Integer Used To Select Our Texture
    float x;                                                    // X Position
    float y;                                                    // Y Position
    float z;                                                    // Z Position
    float yi;                                                    // Y Increase Speed (Fall Speed)
    float spinz;                                                // Z Axis Spin
    float spinzi;                                                // Z Axis Spin Speed
    float flap;                                                    // Flapping Triangles :)
    float fi;                                                    // Flap Direction (Increase Value)
};

object obj[50];                                                    // Create 50 Objects Using The Object Structure

void SetObject(int loop)                                        // Sets The Initial Value Of Each Object (Random)
{
    obj[loop].tex=rand()%3;                                        // Texture Can Be One Of 3 Textures
    obj[loop].x=rand()%34-17.0f;                                // Random x Value From -17.0f To 17.0f
    obj[loop].y=-18.0f;                                            // Set y Position To 18 (Off Top Of Screen)
    obj[loop].z=-((rand()%30000/1000.0f)+10.0f);                // z Is A Random Value From -10.0f To -40.0f
    obj[loop].spinzi=(rand()%10000)/5000.0f-1.0f;                // spinzi Is A Random Value From -1.0f To 1.0f
    obj[loop].flap=0.0f;                                        // flap Starts Off At 0.0f;
    //obj[loop].fi=0.05f+(rand()%100)/1000.0f;                    // fi Is A Random Value From 0.05f To 0.15f
    obj[loop].yi=0.001f+(rand()%1000)/10000.0f;                    // yi Is A Random Value From 0.001f To 0.101f
    obj[loop].fi=obj[loop].yi*(obj[loop].yi*60);
}

void LoadGLTextures()                                            // Creates Textures From Bitmaps In The Resource File
{
    char Texture[4][200] = {"1.bmp", "2.bmp", "3.bmp", "Tim.bmp"};

    glGenTextures(4, &texture[0]);                // Generate 3 Textures (sizeof(Texture)=3 ID's)
    for (int loop=0; loop<4; loop++)                // Loop Through All The ID's (Bitmap Images)
    {
        AUX_RGBImageRec* TextureImage[1];
        TextureImage[0] = auxDIBImageLoad(Texture[loop]);
        glPixelStorei(GL_UNPACK_ALIGNMENT,4);                // Pixel Storage Mode (Word Alignment / 4 Bytes)
        glBindTexture(GL_TEXTURE_2D, texture[loop]);        // Bind Our Texture
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);    // Linear Filtering
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); // Mipmap Linear Filtering
        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    }
}

int Draw (void)                                                // Draw The Scene
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear Screen And Depth Buffer
  
    for (int loop=0; loop<50; loop++)                            // Loop Of 50 (Draw 50 Objects)
    {
        glLoadIdentity ();                                        // Reset The Modelview Matrix
        glBindTexture(GL_TEXTURE_2D, texture[obj[loop].tex]);    // Bind Our Texture
        glTranslatef(obj[loop].x,obj[loop].y,obj[loop].z);        // Position The Object
        glRotatef(45.0f,1.0f,0.0f,0.0f);                        // Rotate On The X-Axis
        glRotatef((obj[loop].spinz),0.0f,0.0f,1.0f);            // Spin On The Z-Axis
      
        glBegin(GL_TRIANGLES);                                    // Begin Drawing Triangles
        // First Triangle                                                            _____
        glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);                //    (2)|    / (1)
        glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f, obj[loop].flap);    //       |  /
        glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);                //    (3)|/
      
        // Second Triangle
        glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);                //           /|(1)
        glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);                //         /  |
        glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f, obj[loop].flap);    //    (2)/____|(3)
      
        glEnd();                                                // Done Drawing Triangles
      
        obj[loop].y+=obj[loop].yi;                                // Move Object Down The Screen
        obj[loop].spinz+=obj[loop].spinzi;                        // Increase Z Rotation By spinzi
        obj[loop].flap+=obj[loop].fi;                            // Increase flap Value By fi
      
        if (obj[loop].y>18.0f)                                    // Is Object Off The Screen?
        {
            SetObject(loop);                                    // If So, Reassign New Values
        }
      
        if ((obj[loop].flap>1.0f) || (obj[loop].flap<-1.0f))    // Time To Change Flap Direction?
        {
            obj[loop].fi=-obj[loop].fi;                            // Change Direction By Making fi = -fi
        }
    }

    glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D, texture[3]);    // This Will Select The BG Maps...
        glPushMatrix();
        glTranslatef(0.0f, 0.0f, -30.0f);
        glBegin(GL_QUADS);
        glNormal3f( 0.0f, 0.0f, 1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-13.3f, -10.0f,  10.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 13.3f, -10.0f,  10.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 13.3f,  10.0f,  10.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-13.3f,  10.0f,  10.0f);
        glEnd();
    glPopMatrix();

    Sleep(20);                                                    // Create A Short Delay (15 Milliseconds)
  
    glFlush ();                                                    // Flush The GL Rendering Pipeline
  
    return TRUE;
}

void InitSet(HWND hConsole)
{
    GLuint    PixelFormat;
    static    PIXELFORMATDESCRIPTOR pfd=                // pfd Tells Windows How We Want Things To Be
    {
        sizeof(PIXELFORMATDESCRIPTOR),                // Size Of This Pixel Format Descriptor
            1,                                            // Version Number
            PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
            PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
            PFD_DOUBLEBUFFER,                            // Must Support Double Buffering
            PFD_TYPE_RGBA,                                // Request An RGBA Format
            16,                                            // Select Our Color Depth
            0, 0, 0, 0, 0, 0,                            // Color Bits Ignored
            0,                                            // No Alpha Buffer
            0,                                            // Shift Bit Ignored
            0,                                            // No Accumulation Buffer
            0, 0, 0, 0,                                    // Accumulation Bits Ignored
            16,                                            // 16Bit Z-Buffer (Depth Buffer)
            0,                                            // No Stencil Buffer
            0,                                            // No Auxiliary Buffer
            PFD_MAIN_PLANE,                                // Main Drawing Layer
            0,                                            // Reserved
            0, 0, 0                                        // Layer Masks Ignored
    };
  
    if (!(hDC=GetDC(hConsole)))                            // Did We Get A Device Context?
    {
        MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))    // Did Windows Find A Matching Pixel Format?
    {                  
        MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if(!SetPixelFormat(hDC,PixelFormat,&pfd))        // Are We Able To Set The Pixel Format?
    {
        MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if (!(hRC=wglCreateContext(hDC)))                // Are We Able To Get A Rendering Context?
    {
        MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
  
    if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
    {
        MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return ;                                // Return FALSE
    }
}

int InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
{
    // Start Of User Initialization
    LoadGLTextures();                                            // Load The Textures From Our Resource File
  
    glClearColor (0.0f, 0.0f, 0.0f, 0.5f);                        // Black Background
    glClearDepth (1.0f);                                        // Depth Buffer Setup
    glDepthFunc (GL_LEQUAL);                                    // The Type Of Depth Testing (Less Or Equal)
    glDisable(GL_DEPTH_TEST);                                    // Disable Depth Testing
    glShadeModel (GL_SMOOTH);                                    // Select Smooth Shading
    glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);            // Set Perspective Calculations To Most Accurate
    glEnable(GL_TEXTURE_2D);                                    // Enable Texture Mapping
    glBlendFunc(GL_ONE,GL_SRC_ALPHA);                            // Set Blending Mode (Cheap / Quick)
    glEnable(GL_BLEND);                                            // Enable Blending
  
    for (int loop=0; loop<50; loop++)                            // Loop To Initialize 50 Objects
    {
        SetObject(loop);                                        // Call SetObject To Assign New Random Values
    }
  
    return TRUE;                                        // Initialization Went OK
}
void main ()
{
    HWND hConsole = GetConsoleHwnd();    // 获得控制台窗口句柄
    InitSet(hConsole);
  
    RECT rect;
    GetWindowRect(hConsole, &rect);
    ReSizeGLScene(rect.right-rect.left, rect.bottom-rect.top);

    InitGL();

    // 设置消息循环
    MSG msg;
    while(1)                                    // Loop That Runs While done=FALSE
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))    // Is There A Message Waiting?
        {
            if (msg.message==WM_QUIT)                // Have We Received A Quit Message?
            {
                break;                            // If So done=TRUE
            }
            else                                    // If Not, Deal With Window Messages
            {
                TranslateMessage(&msg);                // Translate The Message
                DispatchMessage(&msg);                // Dispatch The Message
            }
        }
        else                                        // If There Are No Messages
        {
            // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
            if (!Draw())    // Active?  Was There A Quit Received?
            {
                break;                        // ESC or DrawGLScene Signalled A Quit
            }
            else                                    // Not Time To Quit, Update Screen
            {
                SwapBuffers(hDC);                    // Swap Buffers (Double Buffering)
            }
        }
    }
}

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

ProName.rar (86.97 KB)




[ 本帖最后由 闲出屁 于 2012-2-5 04:54 编辑 ]

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:44
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
以下是引用闲出屁在2012-2-4 21:44:53的发言:

占位
占位

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:45
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
以下是引用闲出屁在2012-2-4 21:45:00的发言:

占位
占位

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:45
闲出屁
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:420
专家分:612
注 册:2012-1-24
收藏
得分:0 
以下是引用闲出屁在2012-2-4 21:45:06的发言:

占位
占位

乔布斯是神 宫崎骏也是神~~ 才知道 原来士兵突击的主题曲是久石让的 久石让的音乐 真传神
2012-02-04 21:45
快速回复:发布个《懒人学C语言》--速成版 此贴有效
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069786 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved