2 iamtoo cai IamToo_cai 于 2015.06.07 23:32 提问

想用按钮叫出对话框,可是不知在哪里出了问题,我是刚入门,各位高人帮我看看,大谢了。

#include

#define ID_START 2

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

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

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


if (!RegisterClass (&wndclass))
{
    MessageBox (NULL, TEXT ("Need NT"), szAppName, MB_ICONERROR);

    return 0;
}

hwnd = CreateWindow (szAppName, TEXT ("WBD"), WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    NULL, NULL, hInstance, NULL);

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)
{
static HINSTANCE hInstance;
static HWND hwndButton;
static RECT rect;
static int cxChar, cyChar;
HDC hdc;
PAINTSTRUCT ps;
static TCHAR szStart[] = TEXT ("Start");
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
cxChar = LOWORD (GetDialogBaseUnits ());
cyChar = HIWORD (GetDialogBaseUnits ());

    hwndButton = CreateWindow (TEXT ("button"), szStart,
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        30 * cxChar, 30 * cyChar,
        20 * cxChar, 7 * cyChar / 4,
        hwnd, (HMENU)ID_START,
        ((LPCREATESTRUCT)lParam)->hInstance, NULL);
    return 0;

case WM_SIZE:
    rect.left   = 24 * cxChar;
    rect.top    = 2 * cyChar;
    rect.right = LOWORD (lParam);
    rect.bottom = HIWORD (lParam);
    return 0;


case WM_PAINT:
    InvalidateRect (hwnd,&rect,TRUE);

    hdc = BeginPaint (hwnd, &ps);
    SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
    SetBkMode (hdc, TRANSPARENT);

    EndPaint (hwnd, &ps);
    return 0;


case WM_DRAWITEM:

case WM_COMMAND:
    switch (LOWORD (wParam))
    {
    case ID_START:
        DialogBox (hInstance, TEXT ("StartMenu"), hwnd, StartMenuProc);
        break;
    }
        return 0;


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

}
BOOL CALLBACK StartMenuProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG :
return TRUE;

case WM_COMMAND:
    switch (LOWORD (wParam))
    {
    case IDOK:
    //case IDCANCEL:
        EndDialog (hDlg, 0);
        return TRUE;
    }
    break;
}
return FALSE;

}

4个回答

91program
91program   Ds   Rxr 2015.06.08 08:14

这一段代码有问题吧
另外,调试一下看看能否执行到 ID_START 分支?

 case WM_DRAWITEM:

case WM_COMMAND:
    switch (LOWORD (wParam))
    {
    case ID_START:
        DialogBox (hInstance, TEXT ("StartMenu"), hwnd, StartMenuProc);
        break;
    }
        return 0;
IamToo_cai
IamToo_cai 回复IamToo_cai: 重新写了控件,终于出来了,谢谢了
接近 3 年之前 回复
IamToo_cai
IamToo_cai 回复91program:
接近 3 年之前 回复
91program
91program 回复IamToo_cai: 你点击按键后,有没有进入 WndProc 函数的处理呢?如果没有,说明系统压根就没有识别到你的按键。还是重新做控件吧,然后复制对应的代码
接近 3 年之前 回复
IamToo_cai
IamToo_cai 我也感觉是那里出了问题,因为主窗口可以出来,但按按钮没反应,不知道是什么原因,
接近 3 年之前 回复
IamToo_cai
IamToo_cai 我也感觉是那里出了问题,因为主窗口可以出来,但按按钮没反应,不知道是什么原因,
接近 3 年之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.07 23:48

事先你准备好了DialogBox需要的对话框资源了么?
建议你用VC++的win32程序向导创建一个程序,在此基础上修改。

IamToo_cai
IamToo_cai 资源文件还有头文件都准备好了
接近 3 年之前 回复
IamToo_cai
IamToo_cai 资源文件还有头文件都准备好了
接近 3 年之前 回复
MSB4011
MSB4011   2015.06.07 23:39

这段代码莫非是利物浦的教材?

frank_20080215
frank_20080215   2015.06.08 05:49

RS文件有吗?查一下。就是写一个资源文件,然后编译

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!