求助!c语言调用API编写画图程序的问题

dingruixin 2007-05-29 06:48:40
想用c语言编写一个函数来实现类似matlab里plot那样的画图功能,编译环境是vc++6.0
不知道怎么实现,我目前的想法是利用GDI,调用API函数来做,但具体不知道怎么做,请高手指正,或者还有什么其它的办法?
...全文
539 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingruixin 2007-05-31
  • 打赏
  • 举报
回复
用API函数画图,需要先创建窗口,取得DC,这样要调用WINMAIN,但是在函数里面是不能有WINMAIN的阿

我画的图形就是简单的二维图形,比如正弦函数。具体就是依次连接由横坐标和纵坐标数组确定的点
星羽 2007-05-31
  • 打赏
  • 举报
回复
谢谢!你还是没有完全明白我的意思,我是想写一个函数,比如:
void draw(double* xcoordinate,double* ycoordinate)
xcoordinate和ycoordinate分别是横坐标和纵坐标,然后直接调用draw函数就可以画出图形。


---------------


那图形的样式呢,什么形状,曲线方程之类的呢?draw怎么知道你要画什么东西啊??
stecdeng 2007-05-31
  • 打赏
  • 举报
回复
看<<Windows程序设计>>??

这些函数中最简单的就是画一个矩形:

Rectangle (hdc, xLeft, yTop, xRight, yBottom) ;

画椭圆,因为它们使用的参数都是相同的:

Ellipse (hdc, xLeft, yTop, xRight, yBottom) ;

......
believe_me 2007-05-31
  • 打赏
  • 举报
回复
vc是无法调用c的图形库的...
dingruixin 2007-05-30
  • 打赏
  • 举报
回复
谢谢!你还是没有完全明白我的意思,我是想写一个函数,比如:
void draw(double* xcoordinate,double* ycoordinate)
xcoordinate和ycoordinate分别是横坐标和纵坐标,然后直接调用draw函数就可以画出图形。
星羽 2007-05-30
  • 打赏
  • 举报
回复
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Gauss(hWnd, hdc, a, q);
EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN :
case WM_KEYUP :
if (wParam == VK_UP)
{
q += 0.05f;
::InvalidateRect(hWnd, NULL, FALSE);
}
if (wParam == VK_DOWN)
{
q -= 0.05f;
::InvalidateRect(hWnd, NULL, FALSE);
}
if (wParam == VK_LEFT)
{
a -= 0.05f;
::InvalidateRect(hWnd, NULL, FALSE);
}
if (wParam == VK_RIGHT)
{
a += 0.05f;
::InvalidateRect(hWnd, NULL, FALSE);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
星羽 2007-05-30
  • 打赏
  • 举报
回复
我有很久前写的一个高撕分布的画图函数,你可以参考参考

是用 sdk 写的

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

#include "stdafx.h"
#include "Gauss.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

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

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

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

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

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GAUSS));

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

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are 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;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GAUSS));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GAUSS);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, 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, UINT, WPARAM, LPARAM)
//
// 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
//
//


int Gauss(HWND hWnd, HDC hDC, FLOAT a, FLOAT q)
{
RECT rectCanvas;

GetClientRect(hWnd, &rectCanvas);

FillRect(hDC, &rectCanvas, CreateSolidBrush(RGB(255, 255, 255)));

POINT point;

int width = rectCanvas.right - rectCanvas.left;
int height = rectCanvas.bottom - rectCanvas.top;

HPEN pen = CreatePen(PS_DOT, 1, RGB(100, 100, 100));
SelectObject(hDC, pen);
MoveToEx(hDC, rectCanvas.left, rectCanvas.top + height / 2, &point);
LineTo(hDC, rectCanvas.right, rectCanvas.top + height / 2);

MoveToEx(hDC, rectCanvas.left + width / 2, rectCanvas.top, &point);
LineTo(hDC, rectCanvas.left + width / 2, rectCanvas.bottom);


int stick = (int)ceil((float(width)) / 200);
int centerX = width / 2 + rectCanvas.left;
int centerY = height / 2 + rectCanvas.top;

pen = CreatePen(PS_DOT, 1, RGB(255, 100, 100));
SelectObject(hDC, pen);
MoveToEx(hDC, centerX + a * 100, rectCanvas.top, &point);
LineTo(hDC, centerX + a * 100, rectCanvas.bottom);

MoveToEx(hDC, centerX + (a - q) * 100, centerY - 50, &point);
LineTo(hDC, centerX + (a - q) * 100, centerY + 50);

MoveToEx(hDC, centerX + (a + q) * 100, centerY - 50, &point);
LineTo(hDC, centerX + (a + q) * 100, centerY + 50);

SetBkMode(hDC, TRANSPARENT);

HFONT font = CreateFont(
15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial"); // lpszFacename

SelectObject(hDC, font);
pen = CreatePen(PS_SOLID, 1, RGB(10, 10, 255));
SelectObject(hDC, pen);
char info[128];
sprintf(info, "%s", _T("高斯分布"));
TextOut(hDC, rectCanvas.left + 10, rectCanvas.top + 10, info, (int)strlen(info));
sprintf(info, "平均值 : %.2f", a);
TextOut(hDC, rectCanvas.left + 10, rectCanvas.top + 30, info, (int)strlen(info));
sprintf(info, "标准差 : %.2f", q);
TextOut(hDC, rectCanvas.left + 10, rectCanvas.top + 50, info, (int)strlen(info));
int c = 0;
for (FLOAT f = -(FLOAT)stick; f < (FLOAT)stick; f += 0.01f)
{
FLOAT y = (1.0f / (q * sqrtf(2.0f * 3.141592654f))) * expf(-((f - a) * (f - a)) / (2 * q * q));

int nY = (int)(centerY - y * 1000);
int nX = (int)(centerX + f * 100);

if (f == -(FLOAT)stick)
MoveToEx(hDC, nX, nY, &point);
else
LineTo(hDC, nX, nY);
//SetPixel(hDC, nX, nY, RGB(0, 0, 255));

if (!(c++ % 100))
{

sprintf_s(info, "(%.2f,%.2f)", f, y);
if (fabs(f - a) < 0.01f)
TextOut(hDC, nX, nY - 15, info, (int)strlen(info));
else if (f > a)
TextOut(hDC, nX + 20, nY, info, (int)strlen(info));
else
TextOut(hDC, nX, nY, info, (int)strlen(info));
}
}

return TRUE;
}

FLOAT a = 0.0f;
FLOAT q = 2.0f;


dingruixin 2007-05-30
  • 打赏
  • 举报
回复
是不是没人做过这方面的
我想知道能不能写出这样一个函数
dingruixin 2007-05-29
  • 打赏
  • 举报
回复
我是要写个函数,然后直接调用就可以象matlab中那样用plot画图
celftj 2007-05-29
  • 打赏
  • 举报
回复
看<<Windows程序设计>>, 里面有很详尽的例子和讲解.

用VC可以考虑MFC

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧