2

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

问题是..我完全不知道从哪里开始。我已经用谷歌搜索了它并在没有帮助的情况下浏览了第一页半,所以我在这里发布:)。任务是做一些简单的事情,比如显示我的名字和画一些省略号,现在我想添加一个组合框..

这是我的代码。

/*------------------------------------------------------------------------
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);
     }

我将把命令放在哪里来制作组合框?

4

2 回答 2

3

为 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);
于 2013-04-13T07:40:54.260 回答
0

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

关于WM_CREATE

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

switch(message){

...

case WM_CREATE: {
//create combobox

return 0;
}
}

请参阅MSDN以获取参考。

编辑

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

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

CreateWindow-功能。

于 2013-04-13T07:29:34.623 回答