爱国小故事200字左右:C语言能直接对API函数进行操作吗

来源:百度文库 编辑:高校问答 时间:2024/05/03 12:28:36
C语言能直接对API函数进行操作吗?
要例子!

你这是什么例子!
看我的:

调用OPENGL API的例子:

#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glaux.h>

void myinit(void);

void CALLBACK elbowAdd (void);

void CALLBACK elbowSubtract (void);

void CALLBACK shoulderAdd (void);

void CALLBACK shoulderSubtract (void);

void CALLBACK viewenter (void);

void CALLBACK viewout (void);

void CALLBACK display(void);

void CALLBACK myReshape(GLsizei w, GLsizei h);

static int shoulder = 0, elbow = 0;

float i=0.0;

//shoulder:肩部角度,elbow: 肘部角度

//下面四个函数是用户的键盘事件处理程序

void CALLBACK elbowAdd (void)

{

elbow = (elbow + 5) % 360;

}

void CALLBACK elbowSubtract (void)

{

elbow = (elbow - 5) % 360;

}

void CALLBACK shoulderAdd (void)

{

shoulder = (shoulder + 5) % 360;

}

void CALLBACK shoulderSubtract (void)

{

shoulder = (shoulder - 5) % 360;

}

void CALLBACK viewenter (void)

{

i+=0.1;

}

void CALLBACK viewout (void)

{

i-=0.1;

}

//用户的绘图过程

void CALLBACK display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 1.0);

glPushMatrix();

//把当前的变换矩阵压入OpenGL内部栈中,用以保存当前矩阵

//画机器人的上臂

glTranslatef (-1.0, 0.0, 0.0);

//用平移矩阵乘当前矩阵,格式为:glTranslatef(x,y,z)

glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);

//用旋转矩阵乘当前矩阵,格式

//为glRotatef (角度,x轴,y

//轴,z轴)

//这里是绕Z轴旋转

glTranslatef (1.0, 0.0, 0.0); //再用平移矩阵乘当前矩阵,注意顺序

auxWireBox(2.0, 0.4, 1.0); //辅助库函数,画一个三维的

//Box,格式为: //auxWireBox(width, height, depth)

//画机器人的前臂,请注意平移矩阵和旋转矩阵的变化

glTranslatef (1.0, 0.0, 0.0);

glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);

glTranslatef (1.0, 0.0, 0.0);

auxWireBox(2.0, 0.4, 1.0);

glPopMatrix(); //恢复刚才的矩阵

//glMatrixMode(GL_MODELVIEW); //定义造型变换模式

//glLoadIdentity(); //用单位矩阵替换当前变换矩阵

glTranslatef (0.0, 0.0, i); //用平移矩阵乘当前矩阵

glFlush(); //结束绘图

}

void myinit (void)

{

glShadeModel (GL_FLAT);

}

//在窗口改变大小时调用

void CALLBACK myReshape(GLsizei width, GLsizei height)

{

glViewport(0, 0, width, height); //定义视口

glMatrixMode(GL_PROJECTION); //定义投影变换模式

glLoadIdentity(); //用单位矩阵替换当前变换矩阵

gluPerspective(65.0, (GLfloat) width/(GLfloat) height, 1.0, 20.0);

//建立一个透视投影矩阵,并乘当前矩阵,格式为:

// gluPerspective(视域的角度,宽高比,

//视点到近裁剪面的距离(总为正),

//视点到远裁剪面的距离(总为正))

glMatrixMode(GL_MODELVIEW); //定义造型变换模式

glLoadIdentity(); //用单位矩阵替换当前变换矩阵

glTranslatef (0.0, 0.0, -5.0); //用平移矩阵乘当前矩阵

}

// 主过程:

// 初始化Windows的窗口界面

// 并初始化OpenGL场景,并处理输入事件,如:键盘,鼠标的操作

int main(int argc, char** argv)

{

auxInitDisplayMode (AUX_SINGLE | AUX_RGB);

auxInitPosition (0, 0, 400, 400);

auxInitWindow ("Composite Modeling Transformations");

myinit ();

//定义键盘处理程序,格式:auxKeyFunc(按键的虚拟代码,用户的处理函数名)

auxKeyFunc (AUX_LEFT, shoulderSubtract);//函数shoulderSubtract()处理左//方向键

auxKeyFunc (AUX_RIGHT, shoulderAdd); //函数shoulderAdd()处理右方向键

auxKeyFunc (AUX_UP, elbowAdd); //函数elbowAdd()处理上方向键

auxKeyFunc (AUX_DOWN, elbowSubtract); //函数elbowSubtract()处理下方向//键

auxKeyFunc (AUX_W, viewenter);

auxKeyFunc (AUX_S, viewout);

auxReshapeFunc (myReshape); //定义窗口大小改变时调用的函数

auxMainLoop(display);

//循环运行display过程,display由用户编写

return(0);

}

API都是C写的, 反过来也成,
你使用VC来做C程序, 可以直接用API

例子:

// DD.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DD, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DD);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DD);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_DD;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}