C语言如何创建窗口

Python020

C语言如何创建窗口,第1张

windows下通过调用API来创建窗口

#include<windows.h>

int main()

{

MessageBox(NULL,"Hello World!","C图形程序",MB_OK)

return 0

}

linux下通过调用图形库来创建窗口。

楼主如果是学C的话,先不要急于搞这些东西,把基础打扎实才是最重要的,GUI可以后学。基础扎实了,这些只是很简单的东西。

通过调用windows API来创建窗口:

#include<windows.h>

int main()

{

MessageBox(NULL,"Hello World!","C图形程序",MB_OK)

return 0

}

这个是最简单的了 

至于MFC  QT 什么的 代码太多了

C语言在windows当然可以写窗口的,早期的窗口很多是用C而非C++写的只是现在很少有人这样做了(因为有MFC,VCL,QT)以下是一个EX:#include<windows.h>/*Thisiswherealltheinputtothewindowgoesto*/LRESULTCALLBACKWndProc(HWNDhwnd,UINTMessage,WPARAMwParam,LPARAMlParam){switch(Message){/*Upondestruction,tellthemainthreadtostop*/caseWM_DESTROY:{PostQuitMessage(0)break}/*Allothermessages(alotofthem)areprocessedusingdefaultprocedures*/default:returnDefWindowProc(hwnd,Message,wParam,lParam)}return0}/*The'main'functionofWin32GUIprograms:thisiswhereexecutionstarts*/intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){WNDCLASSEXwc/*Apropertiesstructofourwindow*/HWNDhwnd/*A'HANDLE',hencetheH,orapointertoourwindow*/MSGMsg/*Atemporarylocationforallmessages*//*zerooutthestructandsetthestuffwewanttomodify*/memset(&wc,0,sizeof(wc))wc.cbSize=sizeof(WNDCLASSEX)wc.lpfnWndProc=WndProc/*Thisiswherewewillsendmessagesto*/wc.hInstance=hInstancewc.hCursor=LoadCursor(NULL,IDC_ARROW)/*White,COLOR_WINDOWisjusta#defineforasystemcolor,tryCtrl+Clickingit*/wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1)wc.lpszClassName="WindowClass"wc.hIcon=LoadIcon(NULL,IDI_APPLICATION)/*Loadastandardicon*/wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION)/*usethename"A"tousetheprojecticon*/if(!RegisterClassEx(&wc)){MessageBox(NULL,"WindowRegistrationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK)return0}hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,/*x*/CW_USEDEFAULT,/*y*/640,/*width*/480,/*height*/NULL,NULL,hInstance,NULL)if(hwnd==NULL){MessageBox(NULL,"WindowCreationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK)return0}/*ThisistheheartofourprogramwhereallinputisprocessedandsenttoWndProc.NotethatGetMessageblockscodeflowuntilitreceivessomething,sothisloopwillnotproduceunreasonablyhighCPUusage*/while(GetMessage(&Msg,NULL,0,0)>0){/*Ifnoerrorisreceived...*/TranslateMessage(&Msg)/*Translatekeycodestocharsifpresent*/DispatchMessage(&Msg)/*SendittoWndProc*/}returnMsg.wParam}