将组合框添加到win32 API

时间:2013-04-13 07:23:33

标签: c++ api winapi combobox

首先,这是家庭作业,但我们的老师鼓励我们超越他实际给我们作为作业的内容。所以我想学习如何添加常见的对话框。我选择使用组合框。

问题是......我完全不知道从哪里开始。我已经谷歌了,看了第一页半,没有任何帮助,所以我在这里张贴:)。任务是做一些简单的事情,比如显示我的名字并绘制一些elipses,现在我想添加一个组合框..

这是我的代码。

/*------------------------------------------------------------------------
Name: Jesse Moreland
Class: CST 238 GUI
Lab 2
---------------------------------------------------------------------------*/

#include <windows.h>
#include <WinUser.h>
#include <string>

using std::string;

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR lpszCmdParam, int nCmdShow)
     {
     static char szAppName[] = "ErrorSample";
     HWND           hwnd;
     HWND           comboBox;
     MSG            msg;    // Look how this struct is defined-see help/class notes NOW
     WNDCLASSEX     wndclass; 

          wndclass.cbSize        = sizeof(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 = static_cast<HBRUSH>(GetStockObject (WHITE_BRUSH));
          wndclass.lpszMenuName  = NULL;
          wndclass.lpszClassName = szAppName;
          wndclass.hIconSm       = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ERROR));

          RegisterClassEx (&wndclass);

//        PlaySound("C:\\Windows\\winsxs\\x86_microsoft-windows-speech-userexperience_31bf3856ad364e35_6.1.7601.17514_none_1e1159f6aa0eb8c7\\Speech Off.wav",NULL, SND_FILENAME);

     hwnd = CreateWindow (szAppName,         // window class name
            "Jesse Moreland LAB 2 - Press Left or Right Mouse Button",           // 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, nCmdShow);

     UpdateWindow (hwnd);

     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
          }
     return (int) msg.wParam;
     }

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

     char * summer = "SUMMER IS COMING SOON!";
     char * leftOrRight = "Press Left or Right Mouse Button to make selection";

     switch (message)
         {

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

                GetClientRect (hwnd, &rect);

                GetWindowRect(hwnd, &size);

                HPEN hPenOld;

                //Draw ellipse
                HPEN hEllipsePen;
                COLORREF qEllipseColor;

                qEllipseColor = RGB(0, 0, 255);
                hEllipsePen = CreatePen(PS_SOLID, 3, qEllipseColor);
                hPenOld =(HPEN)SelectObject(hdc, hEllipsePen);

                Arc(hdc, 40, 40, 160, 80, 0, 0, 0, 0);
                Arc(hdc, 50, 50, 180, 60, 0, 0, 0, 0);

                SelectObject(hdc, hPenOld);
                DeleteObject(hEllipsePen);

                TextOut(hdc, 0, 0, summer, strlen(summer));
                TextOut(hdc, 0, 20, leftOrRight, strlen(leftOrRight));



                EndPaint (hwnd, &ps);
                return 0;

          case WM_LBUTTONDOWN:
              return 0;

          case WM_RBUTTONDOWN:
              return 0;

          case WM_DESTROY:
              PostQuitMessage(0);
              return 0;

         }

     return DefWindowProc (hwnd, message, wParam, lParam);
     }

我将把命令放在哪里组成一个组合框?

2 个答案:

答案 0 :(得分:2)

为WM_CREATE创建一个处理程序,如

CASE WM_CREATE:
    CreateWindowEx(WS_EX_STATICEDGE, "COMBOBOX", "MyCombo1", 
            CBS_DROPDOWN | WS_CHILD | WS_VISIBLE, 
            0, 0, 50, 20, hwnd, 100, hInstance, NULL); // 100 = ID of this control
    CreateWindowEx(WS_EX_STATICEDGE, "COMBOBOX", "MyCombo2", 
            CBS_DROPDOWN | WS_CHILD | WS_VISIBLE, 
            100, 100, 50, 20, hwnd, 101, hInstance, NULL); // 101 = ID of this control
    return 0;

您可以使用

检索dlgproc中其他位置的窗口句柄
hEditCombo1 = GetDlgItem(hwnd, 100);

答案 1 :(得分:0)

有一条名为WM_CREATE的特殊消息,在第一次创建窗口时调用。您可以将代码放在那里,以便在启动时创建组合框。

关于WM_CREATE

当应用程序通过调用CreateWindowEx或CreateWindow函数请求创建窗口时发送。 (在函数返回之前发送消息。)新窗口的窗口过程在创建窗口后但在窗口可见之前收到此消息。

switch(message){

...

case WM_CREATE: {
//create combobox

return 0;
}
}

请参阅MSDN以获取参考。

修改

首先,您需要了解Windows中的 EVERY 控件是一个“窗口”,因此您将使用CreateWindow()创建一个组合框

HWND hCombobox = CreateWindow(L"COMBOBOX", ...);

CreateWindow - 功能。