本文目錄一覽:
C語言怎樣構造窗口
其實可以用代碼實現,但是這個比較複雜不想VB那樣簡單。為此你必須寫一些接近硬件的函數,而且要寫寫函數對顯示模式進行設置。主要過程是 設置顯示模式 –保存顯示器內容—–載入菜單—接收用戶選擇—-恢復顯示內容,這些過程全部要自己寫代碼實現。
給你個大概的代碼
int int86(num,inregs,outregs)
int num//the interruptnumber
union REGS *inregs //the input register values
union REGS *outregs //the output register values
INT86()函數返回的是AX中的值
REGS的定義在 DOS.H中
struct WORDREGS//對寄存器用16位操作
{
unigned int ax,bx,cx,dx,si,di,cflag;
};
struct BYTEREGS//對寄存器用8位操作
{
unsiged char al,ah,bl,bh,cl,ch,dl,dh;//bh存顯示頁號dh存光標行號dl列號
};
union REGS
{
struct WORDREGS x;
struct BYTEREGS h;
};
void goto_xy(x,y)//send the cursor to x,y用了10H功能號2
int x,int y;
{ union REGS r;
r.h.ah=2;//curor addressing funciton
r.h.dl=y;
r.h.dh=x;
int86(0x10,r,r);
}
//中斷10H的子功能號8,字符ascii存在al寄存器屬性存在AH
//save a portion of the screen
void save_video(startx,endx,starty,endy,buf_ptr)
int startx,endx,starty,endy;
unsigned int; *buf_ptr;
{
union REGS r;
register int i,j;
for(i=starty;iendy;i++)
for(j=startx;jendx;j++)
{ goto_xy(j,i);
r.h.ah=8;//read character function
r.h.bh=0;//assume active display page is 0
*buf_ptr++=int86(0x10,r,r);
putchar(”)//clear the screen
}
}
//中斷10H的功能號為9 恢復屏幕
//restore a portion of the screen
void restore_video(startx,endx,starty,endy,buf_ptr)
int startx,endx,starty,endy;
unsigned char *buf_ptr;//you cuo ba
union REGS r;
register int i,j;
for(j=starty;jendx;j++)
{goto_xy(j,i);
r.h.ah=9;
r.h.bh=0;
r.x.cx=1;//number of times to write the character
r.h.al=*buf_ptr++;
r.h.bl=*buf_ptr++;
int86(0x10,r,r);
}
}
//選擇菜單 display a pop_up menu and return selection
int popup(menu,keys,count,x,y,border)
char*menu[];//menu text
char *keys//hot keys
int count//number of menu items
int x,y;//x,y coordinates of left hand corner
int border //no border if o
void display_menu(menu,x,y,count)
//display the menu in its proper location
char * menu[];
int x,y,count;
{registre int i;
for(i=0;icount;i++,x++)
{goto_xy(x,y);
printf(menu[i]);
}
}
//畫邊框函數
void draw_border(int startx,starty,endx,endy)
{ register int i;
for(i=startx+1;iendx;i++)
{goto_xy(i,starty);
putchar(179);
goto_xy(i,endy);
putchar(179);
}
for(i=starty+1;iendy;i++)
{goto_xy(startx,i);
putchar(196);
goto_xy(endx,i);
putchar(196);
}
goto_xy(startx,starty);putchar(218);
goto_xy(startx,endy);putchar(191);
goto_xy(endx,starty);putchar(192);
goto_xy(endx,endy);putchar(217);
}
//接收用戶的選擇 input user’s selection
get_resp(x,y,count,menu,keys)
int x,y,count;
char *menu[]
char *keys;
{
union inkey{char ch[2]; int i;}c;
int arrow_choice=0,key_choice;
y++;
//highlight the first selection
goto_xy(x,y);
write_video(x,y,menu[0],REV_VID);//reverse video
for(;;)
{while(!bioskey(1));
c.i=bioskey(0);//read the key
//reset the selection to normal video
goto_xy(x+arrow_choice,y);
write_video(x+arrow_choice,y,menu[arrow_choice],NORM_VID);
if(c,ch[0]){/is normal key //see if it is a hot key/
key_choice=is_in(keys,tolower(c.ch[]));
if(key_choice)return key_choice-1;
//check for enter or space bar
switch(c.ch[0])
{case’\r’:return arrow_choice;
case’ ‘ :arrow_choice++;
break;case ESC:return-1;//cancel
}
}
else{//is special key
swith(c.ch[1])
{case 72:arrow_choice-;break;
case 80;arrow_choice++;break;
}
}
if(arrow_choice==count)arrow_choice=0;
if(arrow_choice0)arrow_choice=count-1;
goto_xy(x+arrow_choice,y);
write_video(x+arrow_choice,y,menu[arrow_choice],REV_VID);
}
}//其中的REV_VID7黑底白字,NORM_VID70H白字黑底 ESC退出鍵
void write_video(intx,y,char*p,int attrib)//在指定位置顯示指定屬性的字符串
{union REGS r;
register int i,j;
for(i=y;*;i++)
{ goto_xy(x,i);
r.h.ah=9;
r.h.bh=0;
r.x.cx=1;
r.h.al=*p++;
r.h.bl=attrib;
int86(0x10,r,r);
}
}
//is_in 返回指定鍵在熱鍵中的位置 沒在就返回0
int is_in(char *s,char c)
{
register int i;
for(i=0;*s;i++)if(*s++==c)reurn i+1;
return 0;
}
C語言如何創建窗口
windows下通過調用API來創建窗口:
#includewindows.h
int main()
{
MessageBox(NULL,”Hello World!”,”C圖形程序”,MB_OK);
return 0;
}
linux下通過調用圖形庫來創建窗口。
樓主如果是學C的話,先不要急於搞這些東西,把基礎打紮實才是最重要的,GUI可以後學。基礎紮實了,這些只是很簡單的東西。
用c語言怎麼創建一個窗口?
通過調用windows API來創建窗口:
#includewindows.h
int main()
{
MessageBox(NULL,”Hello World!”,”C圖形程序”,MB_OK);
return 0;
}
這個是最簡單的了
至於MFC QT 什麼的 代碼太多了
c語言中 窗口 dc是什麼意思
device context的縮寫,翻譯成中文是設備上下文,或者叫設備內容
這個應該是在Windows Programming裡面用到的吧
詳細如下:
作為C語言,如果要在Windows環境繪圖,那麼就要自己動手實現顯卡驅動,顯示器驅動……微軟為了方便程序員編程,將各種與繪圖相關的數據封裝在DC這種數據結構中。程序員調用這種數據,由系統自動獲取硬件的信息,然後自動填充DC這種結構體。
這樣很方便。當然,如果你想自己把涉及到的硬件驅動自己親自寫一遍,也是可以的,不過……是個很艱巨的任務。。
怎麼用C語言編寫一個windows窗口?
調用window庫窗口函數即可創建windows窗口。
必須使用windows的編譯器,如VC,MS等等。
RegisterClassEx函數:
該函數註冊在隨後調用CreateWindow函數和CreateWindowEx函數中使用的窗口類。 RegisterClass函數己經由函數RegisterClassEx函數來代替,但是,如果不需要設置類的小目標則仍然可以使用RegisterClass函數。
CreateWindowEx函數:
該函數創建一個具有擴展風格的層疊式窗口、彈出式窗口或子窗口,其他與CreateWindow函數相同。關於創建窗口和其他參數的內容,請參看CreateWindow。具體仍可見微軟的msdn。
消息處理函數WindowProc:
該函數是一個應用程序定義的函數。它處理髮送給窗口的消息。WINDPROC類型定義了一個指向該回調函數的指針。WindowProc是用於應用程序定義函數的佔位符。
函數原型:
LRESULT CALLBACK WindowProc (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
參數:
hwnd:指向窗口的句柄。
uMsg:指定消息類型。
wParam:指定其餘的、消息特定的信息。該參數的內容與UMsg參數值有關。
IParam:指定其餘的、消息特定的信息。該參數的內容與uMsg參數值有關。
返回值:返回值就是消息處理結果,它與發送的消息有關。
一個簡單的Window的代碼如下:
#include Windows.h
#include tchar.h
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
int WinMain(
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nShowCmd
)
{
TCHAR *szName = _T(“myWindow”);
WNDCLASSEX wc = {0};
HWND hWnd = NULL;
MSG Msg = {0};
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//通過函數來設置一個白色的背景,這裡大家設置為NULL看看,會很有趣的
wc.hCursor = NULL;//不設置
wc.hIcon = NULL;//不設置
wc.hIconSm = NULL;//不設置
wc.hInstance = hInstance;//當前程序的句柄,hInstance是有系統給傳遞的
wc.lpfnWndProc = WinProc;//窗口處理過程的回調函數。
wc.lpszClassName = szName;//窗口類的名字。
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(wc);//在系統中註冊
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,szName,_T(“我的窗口我喜歡”),WS_OVERLAPPEDWINDOW,
200,100,600,400,NULL,NULL,hInstance,NULL);//創建窗口,窗口標題為”我的窗口我喜歡”
if(hWnd == NULL)
{
MessageBox(NULL,_T(“There’s an Error”),_T(“Error Title”),MB_ICONEXCLAMATION|MB_OK);
return 0;
}
ShowWindow(hWnd,nShowCmd);//顯示窗口
UpdateWindow(hWnd);
//下面是對消息的循環處理,大家先不必管這些,下節課我會細說的
while(GetMessage(Msg,NULL,0,0))
{
TranslateMessage(Msg);//翻譯消息
DispatchMessage(Msg);//分派消息
}
return Msg.message;
}
//消息處理函數
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)//處理消息過程
{
case WM_DESTROY://響應鼠標單擊關閉按鈕事件
PostQuitMessage(0);//退出消息隊列
return 0;//退出函數
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
原創文章,作者:ELGJ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/145127.html