[导入]C语言最简单的Windows程序

[separator]

[quote]
[font=Consolas][color=#804000]#include [/color][color=#000080]<[/color][color=#800040]windows[/color][color=#000080].[/color][color=#800040]h[/color][color=#000080]>

[/color][color=#008000]/*  Declare Windows procedure  */
[/color][color=#800040]LRESULT CALLBACK WindowProcedure [/color][color=#000080]([/color][color=#800040]HWND[/color][color=#000080], [/color][color=#800040]UINT[/color][color=#000080], [/color][color=#800040]WPARAM[/color][color=#000080], [/color][color=#800040]LPARAM[/color][color=#000080]);

[/color][color=#008000]/*  Make the class name into a global variable  */
[/color][color=#0000FF]char [/color][color=#800040]szClassName[/color][color=#000080][ ] = [/color][color=#808080]"WindowsApp"[/color][color=#000080];

[/color][color=#0000FF]int [/color][color=#800040]WINAPI WinMain [/color][color=#000080]([/color][color=#800040]HINSTANCE hThisInstance[/color][color=#000080],
                   [/color][color=#800040]HINSTANCE hPrevInstance[/color][color=#000080],
                   [/color][color=#800040]LPSTR lpszArgument[/color][color=#000080],
                   [/color][color=#0000FF]int [/color][color=#800040]nFunsterStil[/color][color=#000080])

{
   [/color][color=#800040]HWND hwnd[/color][color=#000080];               [/color][color=#008000]/* This is the handle for our window */
   [/color][color=#800040]MSG messages[/color][color=#000080];            [/color][color=#008000]/* Here messages to the application are saved */
   [/color][color=#800040]WNDCLASSEX wincl[/color][color=#000080];        [/color][color=#008000]/* Data structure for the windowclass */

   /* The Window structure */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]hInstance [/color][color=#000080]= [/color][color=#800040]hThisInstance[/color][color=#000080];
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]lpszClassName [/color][color=#000080]= [/color][color=#800040]szClassName[/color][color=#000080];
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]lpfnWndProc [/color][color=#000080]= [/color][color=#800040]WindowProcedure[/color][color=#000080];      [/color][color=#008000]/* This function is called by windows */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]style [/color][color=#000080]= [/color][color=#800040]CS_DBLCLKS[/color][color=#000080];                 [/color][color=#008000]/* Catch double-clicks */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]cbSize [/color][color=#000080]= [/color][color=#0000FF]sizeof [/color][color=#000080]([/color][color=#800040]WNDCLASSEX[/color][color=#000080]);

   [/color][color=#008000]/* Use default icon and mouse-pointer */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]hIcon [/color][color=#000080]= [/color][color=#800040]LoadIcon [/color][color=#000080]([/color][color=#800040]NULL[/color][color=#000080], [/color][color=#800040]IDI_APPLICATION[/color][color=#000080]);
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]hIconSm [/color][color=#000080]= [/color][color=#800040]LoadIcon [/color][color=#000080]([/color][color=#800040]NULL[/color][color=#000080], [/color][color=#800040]IDI_APPLICATION[/color][color=#000080]);
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]hCursor [/color][color=#000080]= [/color][color=#800040]LoadCursor [/color][color=#000080]([/color][color=#800040]NULL[/color][color=#000080], [/color][color=#800040]IDC_ARROW[/color][color=#000080]);
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]lpszMenuName [/color][color=#000080]= [/color][color=#800040]NULL[/color][color=#000080];                 [/color][color=#008000]/* No menu */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]cbClsExtra [/color][color=#000080]= [/color][color=#800080]0[/color][color=#000080];                      [/color][color=#008000]/* No extra bytes after the window class */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]cbWndExtra [/color][color=#000080]= [/color][color=#800080]0[/color][color=#000080];                      [/color][color=#008000]/* structure or the window instance */
   /* Use Windows's default color as the background of the window */
   [/color][color=#800040]wincl[/color][color=#000080].[/color][color=#800040]hbrBackground [/color][color=#000080]= ([/color][color=#800040]HBRUSH[/color][color=#000080]) [/color][color=#800040]COLOR_BACKGROUND[/color][color=#000080];

   [/color][color=#008000]/* Register the window class, and if it fails quit the program */
   [/color][color=#0000FF]if [/color][color=#000080](![/color][color=#800040]RegisterClassEx [/color][color=#000080](&[/color][color=#800040]wincl[/color][color=#000080]))
       [/color][color=#0000FF]return [/color][color=#800080]0[/color][color=#000080];

   [/color][color=#008000]/* The class is registered, let's create the program*/
   [/color][color=#800040]hwnd [/color][color=#000080]= [/color][color=#800040]CreateWindowEx [/color][color=#000080](
          [/color][color=#800080]0[/color][color=#000080],                   [/color][color=#008000]/* Extended possibilites for variation */
          [/color][color=#800040]szClassName[/color][color=#000080],         [/color][color=#008000]/* Classname */
          [/color][color=#808080]"Windows 应用程序"[/color][color=#000080],       [/color][color=#008000]/* Title Text */
          [/color][color=#800040]WS_OVERLAPPEDWINDOW[/color][color=#000080], [/color][color=#008000]/* default window */
          [/color][color=#800040]CW_USEDEFAULT[/color][color=#000080],       [/color][color=#008000]/* Windows decides the position */
          [/color][color=#800040]CW_USEDEFAULT[/color][color=#000080],       [/color][color=#008000]/* where the window ends up on the screen */
          [/color][color=#800080]400[/color][color=#000080],                 [/color][color=#008000]/* The programs width */
          [/color][color=#800080]300[/color][color=#000080],                 [/color][color=#008000]/* and height in pixels */
          [/color][color=#800040]HWND_DESKTOP[/color][color=#000080],        [/color][color=#008000]/* The window is a child-window to desktop */
          [/color][color=#800040]NULL[/color][color=#000080],                [/color][color=#008000]/* No menu */
          [/color][color=#800040]hThisInstance[/color][color=#000080],       [/color][color=#008000]/* Program Instance handler */
          [/color][color=#800040]NULL                 [/color][color=#008000]/* No Window Creation data */
          [/color][color=#000080]);

   [/color][color=#008000]/* Make the window visible on the screen */
   [/color][color=#800040]ShowWindow [/color][color=#000080]([/color][color=#800040]hwnd[/color][color=#000080], [/color][color=#800040]nFunsterStil[/color][color=#000080]);

   [/color][color=#008000]/* Run the message loop. It will run until GetMessage() returns 0 */
   [/color][color=#0000FF]while [/color][color=#000080]([/color][color=#800040]GetMessage [/color][color=#000080](&[/color][color=#800040]messages[/color][color=#000080], [/color][color=#800040]NULL[/color][color=#000080], [/color][color=#800080]0[/color][color=#000080], [/color][color=#800080]0[/color][color=#000080]))
   {
       [/color][color=#008000]/* Translate virtual-key messages into character messages */
       [/color][color=#800040]TranslateMessage[/color][color=#000080](&[/color][color=#800040]messages[/color][color=#000080]);
       [/color][color=#008000]/* Send message to WindowProcedure */
       [/color][color=#800040]DispatchMessage[/color][color=#000080](&[/color][color=#800040]messages[/color][color=#000080]);
   }

   [/color][color=#008000]/* The program return-value is 0 - The value that PostQuitMessage() gave */
   [/color][color=#0000FF]return [/color][color=#800040]messages[/color][color=#000080].[/color][color=#800040]wParam[/color][color=#000080];
}


[/color][color=#008000]/*  This function is called by the Windows function DispatchMessage()  */

[/color][color=#800040]LRESULT CALLBACK WindowProcedure [/color][color=#000080]([/color][color=#800040]HWND hwnd[/color][color=#000080], [/color][color=#800040]UINT message[/color][color=#000080], [/color][color=#800040]WPARAM wParam[/color][color=#000080], [/color][color=#800040]LPARAM lParam[/color][color=#000080])
{
   [/color][color=#0000FF]switch [/color][color=#000080]([/color][color=#800040]message[/color][color=#000080])                  [/color][color=#008000]/* handle the messages */
   [/color][color=#000080]{
       [/color][color=#0000FF]case [/color][color=#800040]WM_DESTROY[/color][color=#000080]:
           [/color][color=#800040]PostQuitMessage [/color][color=#000080]([/color][color=#800080]0[/color][color=#000080]);       [/color][color=#008000]/* send a WM_QUIT to the message queue */
           [/color][color=#0000FF]break[/color][color=#000080];
       [/color][color=#0000FF]default[/color][color=#000080]:                      [/color][color=#008000]/* for messages that we don't deal with */
           [/color][color=#0000FF]return [/color][color=#800040]DefWindowProc [/color][color=#000080]([/color][color=#800040]hwnd[/color][color=#000080], [/color][color=#800040]message[/color][color=#000080], [/color][color=#800040]wParam[/color][color=#000080], [/color][color=#800040]lParam[/color][color=#000080]);
   }

   [/color][color=#0000FF]return [/color][color=#800080]0[/color][color=#000080];
}
[/color][/font][/quote]
文章来源:http://localhost/blog/read.php?13
posted @ 2007-07-14 02:12  zeus2  Views(399)  Comments(0Edit  收藏  举报