當前位置:首頁 » 編程語言 » c語言窗口程序

c語言窗口程序

發布時間: 2022-01-18 06:56:29

① 如何用c語言編寫一個窗體應用程序

最好不要,稍微麻煩一點的窗體程序你就搞不定,而且資料非常少
比較合算的應該是C#的WPF, VC的MFC,都不是C語言的

② c語言怎麼編輯可視窗口程序

C語言是編輯程序

可視窗口程序是就是軟體了,C語言介於匯編語言和高級語言之間是不能直接編輯成軟體的

學通了C語言,其他語言還在話下嗎?

③ 怎麼用c語言寫窗體程序

步驟:
1、注冊窗口類;
2、創建窗體;
3、消息循環;
4、編寫窗口消息處理函數。

代碼:


#include<windows.h>
#include<tchar.h>
LRESULTCALLBACKWindowProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam);
intWINAPI_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPWSTRszCmdLine,intnCmdShow)
{
WNDCLASSwc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;//(HBRUSH)GetStockObject();
wc.lpszMenuName=NULL;
wc.lpszClassName=_T("MyWindowClass");
if(!RegisterClass(&wc))
{
MessageBox(NULL,_T("無法注冊窗口類"),_T("錯誤"),MB_OK);
return0;
}
HWNDnewWindow=CreateWindow(
_T("MyWindowClass"),
_T("我的第一個winapi程序"),
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if(NULL==newWindow)
{
MessageBox(NULL,_T("無法創建窗體"),_T("錯誤"),MB_OK);
return0;
}
ShowWindow(newWindow,nCmdShow);
UpdateWindow(newWindow);
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULTCALLBACKWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
switch(uMsg)
{
caseWM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
returnDefWindowProc(hwnd,uMsg,wParam,lParam);
}
return0;
}

就是一個只有標題欄、關閉按鈕、最小化按鈕、最大化/還原按鈕、顯示區域的窗體。

④ 如何用C語言開發窗體應用程序。。

C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
#include <windows.h>

//Function prototype.
int WINAPI WinMain(HINSTANCE,HINSTANCE ,LPSTR ,int);
LRESULT CALLBACK MainWndProc(HWND ,UINT ,WPARAM,LPARAM);

InitApplication(HINSTANCE);
InitInstance(HINSTANCE,int);

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch(nMsg)
{
case WM_COMMAND:
{
hdc = GetDC((HWND)lParam);
GetClientRect ((HWND)lParam, &rect) ;
DrawText(hdc,"HOHO",-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC((HWND)lParam,hdc);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
GetClientRect (hwnd, &rect) ;
DrawText(hdc,"Hello Word",-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd,&ps);
}
break;
//按鍵
case WM_KEYDOWN:

if(wParam == VK_ESCAPE)
SendMessage(hwnd,WM_CLOSE,0,0);

break;
case WM_CHAR:
switch(wParam)
{
case 'w':
MessageBox(hwnd,"UP","KeyBorad Input",MB_OK);
break;
case 's':
MessageBox(hwnd,"DOWN","KeyBorad Input",MB_OK);
break;
case 'a':
MessageBox(hwnd,"LEFT","KeyBorad Input",MB_OK);
break;
case 'd':
MessageBox(hwnd,"RIGHT","KeyBorad Input",MB_OK);
break;
}
break;

//關閉
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd,nMsg,wParam,lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInStance,
LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;

if(!InitApplication(hInstance))
{
MessageBox(NULL,"InitApplication Failed","Error!",MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
if(!InitInstance(hInstance,nCmdShow)){
MessageBox(NULL,"InitInstance Failed","Error!",MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}

while(GetMessage(&msg,(HWND) NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;

wc.style = CS_DBLCLKS;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MainWClass";
return RegisterClass(&wc);
}

InitInstance(HINSTANCE hInstance,int nCmdShow){
HWND hwnd;
HWND hwndCloseButton;
hwnd = CreateWindow(
"MainWClass",
"Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,
(HMENU)NULL,
hInstance,
(LPVOID)NULL);

if(!hwnd)
return FALSE;

hwndCloseButton = CreateWindow(
"BUTTON",
"CLOSE",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100,
100,
100,
80,
hwnd,
(HMENU)NULL,
hInstance,
(LPVOID)NULL);

if(!hwndCloseButton)
return FALSE;

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}

⑤ 如何用C語言編程出可視化界面

//調用api函數創建窗口

//示例:

#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("MyWindows");//定義窗口類名

HWND hwnd;//窗口句柄

MSG msg; //

WNDCLASS wndclass; //窗口類


wndclass.style = CS_HREDRAW | CS_VREDRAW;//指定窗口類型,各種「類風格」(詳見下方↓)可以使用按位或操作符組合起來

wndclass.lpfnWndProc = WndProc;//指定窗口過程(必須是回調函數)

wndclass.cbClsExtra = 0;//預留的額外空間,一般為 0

wndclass.cbWndExtra = 0;//預留的額外空間,一般為 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("這個程序需要在 Windows NT 才能執行!"), szAppName, MB_ICONERROR);

return 0;

}


hwnd = CreateWindow(szAppName, //創建窗口

TEXT("windows"),

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_PAINT:

hdc = BeginPaint(hwnd, &ps);

GetClientRect(hwnd, &rect);

DrawText(hdc, TEXT("第一個C語言窗口程序!"), -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語言程序的窗體

可以是可以,但是如果光用C語言的特性來編寫Windows窗口程序太費勁,C不適合寫窗口程序。給你如下代碼,可以在VC中編譯成功。

/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/

#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, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

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 ("hellowin.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) ;
}

##########################################

這只是在窗口中間顯示hello,windows98而已,但是代碼卻這么長,估計你也看不懂。不管是窗體,還是所有功能事件完全得自己寫,太麻煩。研究windows程序運行原理,倒可以學學也無妨。所有windows程序最後編譯後都成為這種形式,但是因為寫代碼的時候完全被面向對象包裝了起來,所以都不知道windows程序原來是這么啟動的。

⑦ 學c語言如何做出有界面的程序

1、新建一個C項目,點擊文件,新建,項目,選擇C++Win32控制台應用。

熱點內容
伺服器的空島如何刷錢 發布:2024-11-15 09:40:52 瀏覽:262
安卓系統錄像設置在哪裡 發布:2024-11-15 09:36:33 瀏覽:917
電信級伺服器電腦 發布:2024-11-15 09:26:27 瀏覽:246
壓縮某個文件夾 發布:2024-11-15 09:03:11 瀏覽:891
網址能解壓嗎 發布:2024-11-15 08:54:09 瀏覽:933
python更改目錄 發布:2024-11-15 08:41:08 瀏覽:265
伺服器快閃記憶體可以裝在一般電腦上嗎 發布:2024-11-15 08:36:46 瀏覽:8
安卓手機怎麼查詢自己的路線軌跡 發布:2024-11-15 08:32:19 瀏覽:969
phpdatet 發布:2024-11-15 08:32:17 瀏覽:507
HDB3編解碼實驗 發布:2024-11-15 08:17:31 瀏覽:212