首页 热点资讯 义务教育 高等教育 出国留学 考研考公

如何用C语言开发窗体应用程序。。

发布网友

我来回答

7个回答

热心网友

生产窗体可以使用CreateWindowEx函数。
函数功能:该函数创建一个具有扩展风格的层叠式窗口、弹出式窗口或子窗口,其他与CreateWindow函数相同。
函数原型:
CreateWindowEx函数创建一个层叠的,自动弹出的(pop-up)或是一个子窗口通过扩展格式。另外这个函数的作用与CreateWindow函数的作用相同。要获得更多的关于创建窗口的信息和关于CreateWindowEx函数参数的详细描述。参见CreateWindow

HWND CreateWindowEx(
DWOR DdwExStyle,        //窗口的扩展风格
LPCTSTR lpClassName,    //指向注册类名的指针
LPCTSTR lpWindowName,   //指向窗口名称的指针
DWORD dwStyle,          //窗口风格
int x,                  //窗口的水平位置
int y,                  //窗口的垂直位置
int nWidth,             //窗口的宽度
int nHeight,            //窗口的高度
HWND hWndParent,        //父窗口的句柄
HMENU hMenu,            //菜单的句柄或是子窗口的标识符
HINSTANCE hInstance,    //应用程序实例的句柄
LPVOID lpParam          //指向窗口的创建数据
);

例程:

include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinDouProc(
    HWND hwnd,      // handle to window
    UINT uMsg,      // message identifier
    WPARAM wParam,  // first message parameter
    LPARAM lParam   // second message parameter
);
class CWnd
{
public:
     CWnd()
     {
          m_hWnd = NULL;
     }
     BOOL CreateEx(
          DWORD dwExStyle,      // extended window style
          LPCTSTR lpClassName,  // pointer to registered class name
          LPCTSTR lpWindowName, // pointer to window name
          DWORD dwStyle,        // window style
          int x,                // horizontal position of window
          int y,                // vertical position of window
          int nWidth,           // window width
          int nHeight,          // window height
          HWND hWndParent,      // handle to parent or owner window
         HMENU hMenu,          // handle to menu or child-window identifier
         HANDLE hInstance,     // handle to application instance
         LPVOID lpParam        // pointer to window-creation data
     );
     BOOL ShowWindow( int nCmdShow );
     BOOL UpdateWindow();
public:
     HWND m_hWnd;
};
BOOL CWnd::CreateEx(
      DWORD dwExStyle,      // extended window style
      LPCTSTR lpClassName,  // pointer to registered class name
      LPCTSTR lpWindowName, // pointer to window name
      DWORD dwStyle,        // window style
      int x,                // horizontal position of window
      int y,                // vertical position of window
      int nWidth,           // window width
      int nHeight,          // window height
      HWND hWndParent,      // handle to parent or owner window
      HMENU hMenu,          // handle to menu or child-window identifier
      HANDLE hInstance,     // handle to application instance
      LPVOID lpParam        // pointer to window-creation data
)
{
     m_hWnd = ::CreateWindowEx  (dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);
     if(m_hWnd != NULL)
          return TRUE;
     else
         return FALSE;
}
BOOL CWnd::ShowWindow(int nCmdShow)
{
     return ::ShowWindow(m_hWnd,nCmdShow);
}
BOOL CWnd::UpdateWindow()
{
     return ::UpdateWindow(m_hWnd);
}
int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
    WNDCLASS wndclass;     //先设计窗口类
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
    wndclass.hCursor = LoadCursor(NULL,IDC_HELP);
    wndclass.hIcon = LoadIcon(NULL,IDI_WARNING);
    wndclass.hInstance = hInstance;
    wndclass.lpfnWndProc = WinDouProc;
    wndclass.lpszClassName = "Magic_Maggie";
    wndclass.lpszMenuName = 0;
    wndclass.style = CS_VREDRAW | CS_HREDRAW;
     //某一个变量原油几个变量去掉一个特征,可以用取反(~)后再进行与(&)
     //例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;
    RegisterClass(&wndclass);     ///注意先建立再注册昂
    CWnd wnd;
     wnd.CreateEx(NULL,"Magic_Maggie","DouDou",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
     wnd.ShowWindow(SW_SHOWNORMAL);
     wnd.UpdateWindow();
     MSG msg;     //消息循环
     while(GetMessage(&msg,NULL,0,0))
    {
           TranslateMessage(&msg);
           DispatchMessage(&msg);   //触发WinDouProc
     }
    return 0;
}
  
  
LRESULT CALLBACK WinDouProc(
    HWND hwnd,      // handle to window
     UINT uMsg,      // message identifier
    WPARAM wParam,  // first message parameter
    LPARAM lParam   // second message parameter
)
{
 switch(uMsg)
 {
 case WM_LBUTTONDOWN:
      MessageBox(hwnd,"您按下了鼠标左键昂","豆豆的程序",MB_OK);
      HDC hdc;
      hdc = GetDC(hwnd);    
     //The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context.
      TextOut(hdc,0,0,"感谢您对豆豆程序的支持昂",strlen("感谢您对豆豆程序的支持昂"));
      ReleaseDC(hwnd,hdc);
      break;
  
case WM_CHAR:
      char szChar[20];
      sprintf(szChar,"Char is %d",wParam);
      MessageBox(hwnd,szChar,"豆豆的程序",MB_OK);
      break;
  
case WM_PAINT:
      PAINTSTRUCT ps;
      HDC hDc;
      hDc = BeginPaint(hwnd,&ps);
      TextOut(hDc,0,0,"这个是重绘滴哦",strlen("这个是重绘滴哦"));
      EndPaint(hwnd,&ps);
      break;
  
case WM_CLOSE:   //这个case与下边的destroy这个case不要弄错了,否则窗口不出现,但任务管理器中运行
      if(IDYES == MessageBox(hwnd,"您真的要退出么?","豆豆的程序",MB_YESNO))
      {
           DestroyWindow(hwnd);
      }
      break;
  
case WM_DESTROY:
      PostQuitMessage(0);
      //////////////////////////////////////////?????????????????????
      break;
  
default:
     return DefWindowProc(hwnd,uMsg,wParam,lParam);  // 别忘记了return
  
  }
 return 0;
}

热心网友

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

热心网友

学了Windows API就可以了
调用别人的函数接口,立马做窗口程序。
可以看看 Windows 程序设计这本书(C语言),楼下那个根本就不知道接口是什么,C语言有微软提供的C语言图形函数库,调用之后就可以开发窗口程序

热心网友

最简单的就是开发工具支持了,当然一般都是C++,纯C的没有了。比如你在VC下开发,选择MFC相对比较简单。C++ bulider是个更简单的工具。

热心网友

windows 下用 MFC 类库
linux下 用 QT
都是C++的类库

热心网友

要懂得如何调用WINDOWS API函数

热心网友

用C语言做窗体很麻烦,推荐用WINCE

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com