leewon 发表于 2013-9-26 21:28:52

MFC VM_CREATE消息为啥触发不了。

#include "Header.h"
#include <conio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        wincl.hInstance = hInstance;
        wincl.lpszClassName = TEXT("szClassName");
        wincl.lpfnWndProc = WinProc;
        wincl.style = 0;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = CreateSolidBrush(RGB(255, 240, 240));
        wincl.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(thisicon));
        wincl.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(thisicon));
        RegisterClassEx(&wincl);
        hwnd = CreateWindowEx(WS_EX_ACCEPTFILES, TEXT("szClassName"), TEXT("Abandoned on a Moonlit Night"), WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 534, 311, HWND_DESKTOP, NULL, hInstance, NULL);
        ShowWindow(hwnd, nCmdShow);
        while (GetMessage(&messages, NULL, 0, 0))
        {
                  /*此处代码为本人加上去的,本意是想看下getmesage在消息队列获取消息后的消息参数情况
                   * 加了这段代码后,发现不能执行到WinProc的VM_CREATE里面的代码,只有主窗口
                   */
                FILE *stream;
                AllocConsole();
                freopen_s(&stream, "CONOUT$", "w", stdout);
                printf("hWnd:%d uMsg: %d WParam: %dLParam: %d\n",messages.hwnd, messages.message, messages.wParam, messages.lParam);
                //getchar();
                FreeConsole();
                TranslateMessage(&messages);
                DispatchMessage(&messages);
        }
        return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
                case WM_CREATE: //This runs when the window is created
                {
                        HWND textBox1 = CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER, 36, 10, 100, 20, hwnd, (HMENU)1, NULL, NULL); //This creates textbox
                        HWND textbox2 = CreateWindow(TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER, 36, 28, 100, 20, hwnd, (HMENU)2, NULL, NULL);
                        HWND label1 = CreateWindow(TEXT("static"), TEXT("X"), WS_VISIBLE | WS_CHILD | WS_TABSTOP, 12, 13, 14, 13, hwnd, (HMENU)14, NULL, NULL); //This creates a label
                        HWND label2 = CreateWindow(TEXT("static"), TEXT("Y"), WS_VISIBLE | WS_CHILD | WS_TABSTOP, 12, 28, 14, 13, hwnd, (HMENU)15, NULL, NULL);
                        HWND radioButton1 = CreateWindow(TEXT("BUTTON"), TEXT("Add"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP , 15, 72, 44, 17, hwnd, (HMENU)3, NULL, NULL); //This creates the first radiobutton in the group
                        HWND radioButton2 = CreateWindow(TEXT("BUTTON"), TEXT("Sub"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 15, 96, 44, 17, hwnd, (HMENU)4, NULL, NULL);
                        HWND radioButton3 = CreateWindow(TEXT("BUTTON"), TEXT("Div"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 15, 144, 44, 17, hwnd, (HMENU)5, NULL, NULL);
                        HWND radioButton4 = CreateWindow(TEXT("BUTTON"), TEXT("Mul"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 15, 120, 44, 17, hwnd, (HMENU)6, NULL, NULL);
                        HWND radioButton5 = CreateWindow(TEXT("BUTTON"), TEXT("Equal"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP , 271, 48, 160, 17, hwnd, (HMENU)7, NULL, NULL); //This creates the first radiobutton in the second group
                        HWND radioButton6 = CreateWindow(TEXT("BUTTON"), TEXT("Greater Than"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 271, 72, 160, 17, hwnd, (HMENU)8, NULL, NULL);
                        HWND radioButton7 = CreateWindow(TEXT("BUTTON"), TEXT("Less Than"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 271, 96, 160, 17, hwnd, (HMENU)9, NULL, NULL);
                        HWND radioButton8 = CreateWindow(TEXT("BUTTON"), TEXT("Greater Than or Equal"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 271, 120, 160, 17, hwnd, (HMENU)10, NULL, NULL);
                        HWND radioButton9 = CreateWindow(TEXT("BUTTON"), TEXT("Less Than or Equal"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 271, 144, 160, 17, hwnd, (HMENU)11, NULL, NULL);
                        HWND radioButton10 = CreateWindow(TEXT("BUTTON"), TEXT("Not Equal"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 271, 168, 160, 17, hwnd, (HMENU)12, NULL, NULL);
                        HWND label3 = CreateWindow(TEXT("static"), TEXT("Answer"), WS_VISIBLE | WS_CHILD | WS_GROUP | WS_TABSTOP, 33, 53, 160, 13, hwnd, (HMENU)13, NULL, NULL);
                        UpdateWindow(hwnd);
                        break;
                }
                case WM_COMMAND:
                {
                        switch (wParam)
                        {
                        case 3: //This is ran when the radiobutton with (HEMNU)3 is pressed
                        {
                                std::ostringstream ostr;
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                random = x_number + y_number;
                                ostr << random;
                                random_string = ostr.str();
                                SetDlgItemText(hwnd, 13, random_string.c_str());
                                break;
                        }
                        case 4:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                                                std::ostringstream ostr;
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                random = x_number - y_number;
                                ostr << random;
                                random_string = ostr.str();
                                SetDlgItemText(hwnd, 13, random_string.c_str());
                                break;
                        }
                        case 5:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                                                std::ostringstream ostr;
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                random = x_number / y_number;
                                ostr << random;
                                random_string = ostr.str();
                                SetDlgItemText(hwnd, 13, random_string.c_str());
                                break;
                        }
                        case 6:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                std::ostringstream ostr;
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                random = x_number * y_number;
                                ostr << random;
                                random_string = ostr.str();
                                SetDlgItemText(hwnd, 13, random_string.c_str());
                                break;
                        }
                        case 7:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                if (x_number == y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        case 8:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                if (x_number > y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        case 9:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atoi(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atoi(y);
                                if (x_number < y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        case 10:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                if (x_number >= y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        case 11:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                if (x_number <= y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        case 12:
                        {
                                GetDlgItemText(hwnd, 1, x, 32767);
                                GetDlgItemText(hwnd, 2, y, 32767);
                                if (x == "") //This sees if x has no value
                        {
                                x = '0'; //This sets x to 0
                                }
                    if (y == "") //This sees if x has no value
                        {
                                y = '0'; //This sets y to 0
                    }
                                x_number = atof(x); //This converts the string x to a integer then stores it in x_number
                                y_number = atof(y);
                                if (x_number != y_number)
                                {
                                        whattodisplay = 1;
                                }
                                else
                                {
                                        whattodisplay = 2;
                                }
                                RECT rect = {0, 0, 534, 311};
                                InvalidateRect(hwnd, &rect, TRUE);
                                break;
                        }
                        }
                }
                case WM_PAINT: //This runs when the window is requested to repaint itself
                {
                        BITMAP bm;
                        PAINTSTRUCT ps;
                        HDC screendc = BeginPaint(hwnd, &ps);
                        HDC bitmaphdc = CreateCompatibleDC(screendc);
                        HGDIOBJ orginal = SelectObject(bitmaphdc, LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(file))); //It is important to save the result of the first SelectObject, and put it back into the HDC before you delete the HDC
                        if (whattodisplay == 0)
                        {
                                BitBlt(screendc, 271, 10, 90, 35, bitmaphdc, 0, 0, SRCCOPY);
                        }
                        else
                        {
                                if (whattodisplay == 1)
                                {
                                        SelectObject(bitmaphdc, LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(yes)));
                                        BitBlt(screendc, 271, 10, 90, 35, bitmaphdc, 0, 0, SRCCOPY);
                                }
                                else
                                {
                                        SelectObject(bitmaphdc, LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(no)));
                                        BitBlt(screendc, 271, 10, 90, 35, bitmaphdc, 0, 0, SRCCOPY);
                                }
                        }
                        SelectObject(bitmaphdc, orginal);
                        DeleteDC(bitmaphdc);
                        EndPaint(hwnd, &ps);
                        break;
                }
                case WM_DESTROY: //This runs when the window is destroyed
                {
                        exit(0);
                        break;
                }
                default:
                        return DefWindowProc(hwnd, message, wParam, lParam);
        }
}

玉宁417 发表于 2013-9-27 07:40:44

新人,路过学习一下{:1_1:}

小号4 发表于 2013-9-28 21:30:40

不太懂,不过你可以调试一下哪里出问题了

elong130 发表于 2013-9-29 16:16:17

可以执行到,你在WM_CREATE的第一行加上MESSAGEBOX(NULL,NULL,NULL,NULL),运行后会弹出一个对话框,我自己按照你加的代码试过,可以执行,不过窗口会不停的闪烁+移动。 你可以看看WM_CREATE里的设置是否生效,但是该消息一定执行到了。

指尖 发表于 2013-9-30 15:09:28

本帖最后由 指尖 于 2013-9-30 15:10 编辑

执行到这个地方是一定的,但没按你的想法来做,,,哈哈,你最好看一下清华的李力祥的视频,自己得来的答案比较深刻,,你的问题在于没搞懂消息机制。。。。。如果您看视频解决了问题,别忘了给我发钱,哥需要鱼币下载OD的试验程序和ppt{:5_102:} ,,,最后别找我要视频地址,还是您自己找,,,,
页: [1]
查看完整版本: MFC VM_CREATE消息为啥触发不了。