c语言制作UI界面需要相关的UI库
如windows操作系统本身就提供了UI的接口
一个简单的示例代码如下
#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR line,int cmd)
{
static TCHAR AppName[]=TEXT("99");
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=AppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),AppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(AppName,TEXT("九九乘法口诀表"),\
WS_OVERLAPPEDWINDOW,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
NULL,\
NULL,\
hinstance,\
NULL);
ShowWindow(hwnd,cmd);
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;
static x,y;
int i,j;
int len;
TCHAR buf[50];
TEXTMETRIC tm;
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
x=tm.tmAveCharWidth;
y=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
//MessageBox(NULL,TEXT("Create Successed!"),TEXT("Successed"),MB_OK);
//PlaySound(TEXT("hello.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
//DrawText(hdc,TEXT("Hello World!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
//TextOut(hdc,rect.right/2-(strlen("Hello World!")/2)*x,rect.bottom/2-y/2,TEXT("Hello World!"),12);
for(i=1;i!=10;++i)
{
for(j=1;j!=i+1;++j)
{
len=wsprintf(buf,TEXT("%dx%d=%-4d"),j,i,i*j);
TextOut(hdc,j*len*x,i*y,buf,len);
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}
上面是一个打印windows下拥有窗口界面的九九乘法口诀表的c语言程序代码
同样的c语言也会有其它的UI库
比如Gtk,Gtk是可移植的UI库
可以使用它在Linux、windows包括mac等等操作系统上做ui程序设计
一个简单的示例代码如下
#include <gtk/gtk.h>
int main(int argc,char **argv)
{
GtkWidget *win;
GtkWidget *label;
int i,j;
GString *str;
gtk_init(&argc,&argv);
win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
g_signal_connect(G_OBJECT(win),"delete-event",G_CALLBACK(gtk_main_quit),NULL);
str=g_string_new(NULL);
for(i=1;i <= 9;++i)
{
for(j=1;j != i+1;++j)
g_string_append_printf(str,"%dx%d=%-4d",j,i,i*j);
g_string_append(str,"\n");
}
label=gtk_label_new(str->str);
gtk_container_add(GTK_CONTAINER(win),label);
gtk_widget_show_all(win);
gtk_main();
g_string_free(str,TRUE);
return 0;
}
- 相关评论
- 我要评论
-