Нужен пример использования WinAPI - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Всем привет! приведите пожалуйста пример использования WinAPI на языке си?

Решение задачи: «Нужен пример использования WinAPI»

textual
Листинг программы
  1. #include <windows.h>
  2. /*  Declare Windows procedure  */
  3. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  4.  
  5. /*  Make the class name into a global variable  */
  6. char szClassName[ ] = "CodeBlocksWindowsApp";
  7. HWND btn;
  8. HFONT fnt;
  9. HHOOK mbhook;
  10. int mbox(HWND parent,const char *text,const char *title,int type);
  11. int WINAPI WinMain (HINSTANCE hThisInstance,
  12.                      HINSTANCE hPrevInstance,
  13.                      LPSTR lpszArgument,
  14.                      int nCmdShow)
  15. {
  16.     HWND hwnd;               /* This is the handle for our window */
  17.     MSG messages;            /* Here messages to the application are saved */
  18.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  19.  
  20.     /* The Window structure */
  21.     wincl.hInstance = hThisInstance;
  22.     wincl.lpszClassName = szClassName;
  23.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  24.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  25.     wincl.cbSize = sizeof (WNDCLASSEX);
  26.  
  27.     /* Use default icon and mouse-pointer */
  28.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  29.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  30.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  31.     wincl.lpszMenuName = NULL;                 /* No menu */
  32.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  33.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  34.     /* Use Windows's default colour as the background of the window */
  35.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  36.  
  37.     /* Register the window class, and if it fails quit the program */
  38.     if (!RegisterClassEx (&wincl))
  39.         return 0;
  40.  
  41.     /* The class is registered, let's create the program*/
  42.     hwnd = CreateWindowEx (
  43.            0,                   /* Extended possibilites for variation */
  44.            szClassName,         /* Classname */
  45.            "Code::Blocks Template Windows App",       /* Title Text */
  46.            WS_OVERLAPPEDWINDOW, /* default window */
  47.            CW_USEDEFAULT,       /* Windows decides the position */
  48.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  49.            544,                 /* The programs width */
  50.            375,                 /* and height in pixels */
  51.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  52.            NULL,                /* No menu */
  53.            hThisInstance,       /* Program Instance handler */
  54.            NULL                 /* No Window Creation data */
  55.            );
  56.     btn=CreateWindowExA(0,"BUTTON","µВіГ±ВЁ",BS_PUSHBUTTON|WS_VISIBLE|WS_CHILD,100,100,100,20,hwnd,(HMENU)1001,hThisInstance,NULL);
  57.     fnt=CreateFont(-11,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial Armenian");
  58.     SendMessage(btn,WM_SETFONT,(WPARAM)fnt,0);
  59.     //SetWindowTextA(btn,"µВіГ±ВЁ");
  60.     /* Make the window visible on the screen */
  61.     ShowWindow (hwnd, nCmdShow);
  62.     /* Run the message loop. It will run until GetMessage() returns 0 */
  63.     while (GetMessage (&messages, NULL, 0, 0))
  64.     {
  65.         /* Translate virtual-key messages into character messages */
  66.         TranslateMessage(&messages);
  67.         /* Send message to WindowProcedure */
  68.         DispatchMessage(&messages);
  69.     }
  70.  
  71.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  72.     return messages.wParam;
  73. }
  74.  
  75.  
  76. /*  This function is called by the Windows function DispatchMessage()  */
  77.  
  78. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  79. {
  80.     switch (message)                  /* handle the messages */
  81.     {
  82.         case WM_COMMAND:
  83.             if (wParam==1001)
  84.                 //MessageBox(hwnd,"µВіГ±ВЁ","aaa",MB_OK);
  85.                 mbox(hwnd,"µВіГ±ВЁ","µВіГ±ВЁ",MB_OKCANCEL);
  86.             break;
  87.         case WM_DESTROY:
  88.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  89.             break;
  90.         default:                      /* for messages that we don't deal with */
  91.             return DefWindowProc (hwnd, message, wParam, lParam);
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. LRESULT CALLBACK mbhookproc(int code,WPARAM wp,LPARAM lp)
  98. {
  99.     if (code<0)
  100.         CallNextHookEx(mbhook,code,wp,lp);
  101.     if (code==HCBT_ACTIVATE)
  102.     {
  103.         SendMessage((HWND)wp,WM_SETFONT,(WPARAM)fnt,0);
  104.         HWND child=NULL;
  105.         while ((child=FindWindowEx((HWND)wp,child,"BUTTON",NULL))!=NULL)
  106.             SendMessage(child,WM_SETFONT,(WPARAM)fnt,0);
  107.         child=NULL;
  108.         while ((child=FindWindowEx((HWND)wp,child,"STATIC",NULL))!=NULL)
  109.             SendMessage(child,WM_SETFONT,(WPARAM)fnt,0);
  110.         child=FindWindowEx((HWND)wp,NULL,"BUTTON","OK");
  111.         SetWindowText(child,"ГєГЏ");
  112.         child=FindWindowEx((HWND)wp,NULL,"BUTTON","CANCEL");
  113.         SetWindowText(child,"Гў»Г•ВіГ±ГЏ»Г‰");
  114.         return 0;
  115.     }
  116.     return CallNextHookEx(mbhook,code,wp,lp);
  117. }
  118.  
  119. int mbox(HWND parent,const char *text,const char *title,int type)
  120. {
  121.     mbhook=SetWindowsHookEx(WH_CBT,mbhookproc,NULL,GetCurrentThreadId());
  122.     int ret=MessageBox(parent,text,title,type);
  123.     UnhookWindowsHookEx(mbhook);
  124.     return ret;
  125. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут