2011-05-06 22 views
1

這似乎是一個簡單的問題,但我不知道應該在程序運行時將Win32代碼放在哪裏。作爲一個簡單的例子,我提供了一個包含我認爲是標準Win32 Window初始化代碼的例子,後面跟着一個簡單的「Beep」命令。我試圖插在各個不同的地方蜂鳴命令,但結果是以下三種之一:C++ WIN32:把程序運行時執行的代碼放在哪裏

  1. 發生的蜂鳴聲,並不斷地循環
  2. 沒有嘟嘟聲
  3. 的嗶嗶聲只能發生時,聽到因爲我關閉程序

我使用的代碼如下所示。這只是我從一個在線資源中解脫出來的一個例子,最後添加了嗶聲命令。沒有編譯器錯誤。在這個例子中,當我關閉程序時發出嘟嘟聲。正如你所料,這是我第一個Win32應用程序。

#include <windows.h> 

    /* Declare Windows procedure */ 
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 

    /* Make the class name into a global variable */ 
    char szClassName[ ] = "WindowsApp"; 

    int WINAPI WinMain (HINSTANCE hThisInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR lpszArgument, 
       int nFunsterStil) 

{ 
        HWND hwnd;    /* This is the handle for our window */ 
        MSG messages;   /* Here messages to the application are saved */ 
        WNDCLASSEX wincl;  /* Data structure for the windowclass */ 

     /* The Window structure */ 
     wincl.hInstance = hThisInstance; 
     wincl.lpszClassName = szClassName; 
     wincl.lpfnWndProc = WindowProcedure;  /* This function is called by windows */ 
     wincl.style = CS_DBLCLKS;     /* Catch double-clicks */ 
     wincl.cbSize = sizeof (WNDCLASSEX); 

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

     /* Register the window class, and if it fails quit the program */ 
     if (!RegisterClassEx (&wincl)) 
       return 0; 

     /* The class is registered, let's create the program*/ 
     hwnd = CreateWindowEx (
      0,     /* Extended possibilites for variation */ 
      szClassName,   /* Classname */ 
      "Matt's Program That Beeps",  /* Title Text */ 
      WS_OVERLAPPEDWINDOW, /* default window */ 
      CW_USEDEFAULT,  /* Windows decides the position */ 
      CW_USEDEFAULT,  /* where the window ends up on the screen */ 
      544,     /* The programs width */ 
      375,     /* and height in pixels */ 
      HWND_DESKTOP,  /* The window is a child-window to desktop */ 
      NULL,    /* No menu */ 
      hThisInstance,  /* Program Instance handler */ 
      NULL     /* No Window Creation data */ 
      ); 

     /* Make the window visible on the screen */ 
     ShowWindow (hwnd, nFunsterStil); 

     /* Run the message loop. It will run until GetMessage() returns 0 */ 
     while (GetMessage (&messages, NULL, 0, 0)) 
     { 
     /* Translate virtual-key messages into character messages */ 
     TranslateMessage(&messages); 
     /* Send message to WindowProcedure */ 
     DispatchMessage(&messages); 
     } 

     /* The program return-value is 0 - The value that PostQuitMessage() gave */ 
     return messages.wParam; 
} 

/* This function is called by the Windows function DispatchMessage() */ 
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
switch (message)     /* handle the messages */ 
{ 
    case WM_DESTROY: 
     PostQuitMessage (0);  /* send a WM_QUIT to the message queue */ 
     break; 
    default:      /* for messages that we don't deal with */ 
     return DefWindowProc (hwnd, message, wParam, lParam); 
} 
    Beep(750, 300);    /* This is the beep command */ 
    return 0; 
} 

回答

0

簡單來說,如果你想要的代碼對每個窗口消息運行(你可能不),然後把嗶聲通話在你的窗口過程的頂部。如果你希望它只發生在某些消息上,那麼爲這些消息添加一個case,並將其放在這種情況下。

+0

謝謝 - 這個工程。 – CaptainProg 2011-05-06 13:34:26

1

要放置的蜂鳴呼叫在窗口消息回調,這是應該每當窗口接收的消息(例如調整大小,鼠標上的NC區域等)來觸發;所以它可能會發射很多。

您可以在separate thread中運行您的代碼(如果操作將會很長),以避免凍結GUI作爲解決方法,或者如果您想要單次啓動代碼,則可以使用WM_CREATE事件消息來運行代碼一旦創建窗口。

Check this out as well

2

在典型的Windows應用程序中,所有事情都是作爲對消息循環中收到的消息的響應而發生的。如果你想在第一次打開窗口時發出一聲蜂鳴聲,你可以爲WM_CREATE消息添加一個處理程序,並將代碼放在那裏。

當您回覆消息時,您需要儘快返回以避免使UI緩慢或無響應。如果你需要做很多工作,你應該創建一個單獨的線程來處理工作。