VisualStudio怎么用C语言进行图形编程啊
发布网友
发布时间:2022-04-26 13:08
我来回答
共2个回答
热心网友
时间:2022-06-28 12:17
新建一个项目、选择win32项目,应用程序为windows程序,附加选项为空;在源代码目录中添加一个后缀为.c的源文件。接下来就可以写C代码了。
用c语言中通过调用windows的api函数,来显示窗口、菜单、滚动条、屏幕输出。
在《windows程序设计》一书中就有一个例子,显示一个标准的windows窗口,并在绘图区显示一个字符串。代码如下:
#include<windows.h>
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;
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("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("The Hello Program"),
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)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
//PlaySound(TEXT("C:\\Windows Notify.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("Hello, Windows 98!"),-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
复制到刚才的.c文件中即可以编译运行。
热心网友
时间:2022-06-28 12:18
#include <windows.h>
你要用的里面都有
顺便提供一个以前写的贪吃蛇
追问那, windows.h 的 的用法哪有啊?
追答http://wenku.baidu.com/link?url=WPy08F6gbDyo1ii-5u8kZ9qG0uEGeNTufF9smzVSs4xVY949aV4lnME0XKTE886kejEE3KDEaehJ8BmuyKg_Pn9DZlt9SDRX_pq6XF7r-eK