首页 > 编程知识 正文

c语言中的窗口,c语言中的编程窗口

时间:2024-04-24 11:42:58 阅读:333456 作者:APBN

本文目录一览:

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);

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。