C语言怎么只能编写控制台应用程序,怎么编写WINDOWS 应用程序?

如题所述

  需要些SDK的知识,windows的实现中基本上都是用的C语言,其各种接口基本上都是原生C语言函数,具体比如SDK用的windows API。
使用纯C语言编写windows程序,工作量将会相当大,下面是一个小例子:
  /*
  * This is a simple windows program, it does nothing but draw an ellipse.
  * Windows SDK, Win32 API ,Pure C, (Not C++ or MFC !!)
  * Suxpert at gmail dot com, 2008/8/24
  * */
  #include <windows.h>
  LONG WINAPI WndProc( HWND, UINT, WPARAM, LPARAM );
  int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpszCmdLine, int nCmdShow ){
  /* The Entry for windows program, just like main() in dos */
  WNDCLASS wc;
  HWND hwnd;
  MSG msg;
  wc.style = 0; // Class style
  wc.lpfnWndProc = (WNDPROC)WndProc; // Window procedure address
  wc.cbClsExtra = 0; // Class extra bytes
  wc.cbWndExtra = 0; // Window extra bytes
  wc.hInstance = hInstance; // Instance handle
  wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); // Icon handle
  wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // Cursor handle
  wc.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 ); // Background color
  wc.lpszMenuName = NULL; // Menu name
  wc.lpszClassName = "WinSDKtest"; // WNDCLASS name
  RegisterClass( &wc );
  hwnd = CreateWindow (
  "WinSDKtest", // WNDCLASS name
  "SDK Application", // Window title
  WS_OVERLAPPEDWINDOW, // Window style
  CW_USEDEFAULT, // Horizontal position
  CW_USEDEFAULT, // Vertical position
  CW_USEDEFAULT, // Initial width
  CW_USEDEFAULT, // Initial height
  HWND_DESKTOP, // Handle of parent window
  NULL, // Menu handle
  hInstance, // Application's instance handle
  NULL // Window-creation data
  );
  ShowWindow( hwnd, nCmdShow );
  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 )
  {
  /* Windows will call this function anytime... */
  PAINTSTRUCT ps;
  HDC hdc;
  switch(message){
  case WM_PAINT:
  hdc = BeginPaint( hwnd, &ps );
  Ellipse( hdc, 0, 0, 800, 600 );
  // Here we Draw an ellipse in the window of our program
  EndPaint( hwnd, &ps );
  break; // Someone like to write return here.
  case WM_DESTROY:
  PostQuitMessage(0);
  break;
  default:
  return DefWindowProc( hwnd, message, wParam, lParam );
  }
  return 0;
  }
  基本过程就是直接调用windows提供的API函数,完成从窗口创建,显示等界面功能到深层的文件操作,注册表等甚至windows内核调试等高级功能。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-01
C语言只是一种编程语言,他可以做任何事情,谁说他只能编写控制台应用的?那只是因为你的编译环境不支持windows应用的开发,不是C语言开发不了!
你用VC或者C++Builder编译环境开发试试!
第2个回答  2011-11-01
路过,觉得你的问题挺有趣的,我给你个比喻:就像为什么母鸡只生鸡蛋,不生鸭蛋,可是鸡蛋鸭蛋煎好了之后是一个样。

不是不行,是途径不同,有自身的局限性,各种编程语言都有优越的地方也有不足的地方。
第3个回答  2011-11-01
百度一下, 80x86汇编小站, 看看 站长写的下面这几篇文章, 或者 你直接联系 这个站长 跟他交流编程方面的事情。

编程是什么,什么是编程?
如何正确入门Windows系统下C/C++学习领域?
掌握C/C++后,如何在Windows系统下开发有界面的程序?
如何正确入门Windows系统下驱动开发领域?
如何正确进入基于x86-64Bit Windows系统商业软件破解领域?
如何正确的待汇编学习和反汇编学习?
成为VC++程序员,我需要学会哪些东西?本回答被提问者和网友采纳
相似回答