2012-07-07 87 views
2

我想在我的主窗口中創建一個可以接受文本輸入幷包含按鈕的小框。當我說嵌入式時,我的意思是我希望它無縫地集成在我的主窗口中,而無需使用自己的關閉按鈕。我無休止地搜索了谷歌,並沒有找到不涉及mfc或.net的答案。我如何在原始的win32應用程序中執行此操作。請將您的代碼添加到下面顯示的框架中。謝謝!如何在win32窗口中創建一個嵌入式文本輸入框

#include <windows.h> 

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        PSTR szCmdLine, int iCmdShow) 
{ 
    static TCHAR szAppName[] = TEXT ("App") ; 
    HWND   hwnd ; 
    MSG   msg ; 
    WNDCLASS  wndclass ; 

    wndclass.style   = CS_HREDRAW | CS_VREDRAW ; 
    wndclass.lpfnWndProc = WndProc ; 
    wndclass.cbClsExtra = 0 ; 
    wndclass.cbWndExtra = 0 ; 
    wndclass.hInstance  = hInstance ; 
    wndclass.hIcon   = LoadIcon (NULL, IDI_APPLICATION) ; 
    wndclass.hCursor  = LoadCursor (NULL, IDC_ARROW) ; 
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; 
    wndclass.lpszMenuName = NULL ; 
    wndclass.lpszClassName = szAppName ; 

    if (!RegisterClass (&wndclass)) 
    { 
      MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
         szAppName, MB_ICONERROR) ; 
      return 0 ; 
    } 

    hwnd = CreateWindow (szAppName,     // window class name 
          TEXT ("App"), // window caption 
          WS_OVERLAPPEDWINDOW,  // window style 
          CW_USEDEFAULT,    // initial x position 
          CW_USEDEFAULT,    // initial y position 
          CW_USEDEFAULT,    // initial x size 
          CW_USEDEFAULT,    // initial y size 
          NULL,      // parent window handle 
          NULL,      // window menu handle 
          hInstance,     // program instance handle 
          NULL) ;      // creation parameters 

    ShowWindow (hwnd, iCmdShow) ; 
    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) 
{ 
    HDC   hdc ; 
    PAINTSTRUCT ps ; 
    RECT  rect ; 

    switch (message) 
    { 
    case WM_CREATE: 

      return 0 ; 

    case WM_PAINT: 
      hdc = BeginPaint (hwnd, &ps) ; 



      EndPaint (hwnd, &ps) ; 
      return 0 ; 

    case WM_DESTROY: 
      PostQuitMessage (0) ; 
      return 0 ; 
    } 
    return DefWindowProc (hwnd, message, wParam, lParam) ; 
} 
+0

這是在Visual Studio中啓動項目殼,哪裏是你嘗試的解決方案? – Ulterior 2012-07-08 00:26:46

回答

4

你想收到什麼信息?例如,按下按鈕時發送WM_COMMAND消息。處理這些消息如下:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
HDC   hdc ; 
PAINTSTRUCT ps ; 
RECT  rect ; 

switch (message) 
{ 
case WM_CREATE: 

     return 0 ; 

case WM_COMMAND: 
     { 
     switch(LOWORD(wParam)) 
      { 
      case IDC_BUTTON:    
       /* 
       IDC_BUTTON is id given to button 
       This id is specified while creating window. If you are using 
       CreateWindow then it will be as follows: 

      CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, (HMENU)IDC_BUTTON, hInst, 0);      

     and in resource.rc file or in the beginning of code. Do "#define IDC_BUTTON     3456" 
     3456 is just a reference you can give any other no as well. Prefer using bigger number so that they can not conflict with existing ones. 
       */ 


       //do whatever you want to do here when button is pressed 
      break 
      } 
     } 
     break; 
case WM_PAINT: 
     hdc = BeginPaint (hwnd, &ps) ; 



     EndPaint (hwnd, &ps) ; 
     return 0 ; 

case WM_DESTROY: 
     PostQuitMessage (0) ; 
     return 0 ; 
} 
return DefWindowProc (hwnd, message, wParam, lParam) ; 

}

+0

您可以使用GetWindowText在編輯框中獲取文本。 – Shubhansh 2012-07-09 12:53:22

3

你的問題不是很清楚,但你在Win32中創建文本框和按鈕動態的方式是使用CreateWindow功能。 Win32爲控件註冊特殊類,您可以使用它作爲第一個參數傳遞給CreateWindow

要創建一個文本框,呼叫 -

CreateWindow("EDIT", 0, WS_BORDER|WS_CHILD|WS_VISIBLE, 56, 10, 50, 18, g_hWnd, 0, hInst, 0); 

創建一個按鈕 -

CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, 0, hInst, 0); 

您必須指定WS_CHILDWS_VISIBLE風格,也主要提供手柄窗口作爲其父項。

+0

好的謝謝。你能告訴我如何從這個對話框中獲得消息嗎? – Iowa15 2012-07-08 04:02:51

+0

他們會被你的'WndProc'函數接收。 – Deanna 2012-07-09 13:06:18

相關問題