简体   繁体   English

单击文本框时发送WM_Quit消息

[英]WM_Quit message being sent when i click on textbox

so title says it all. 标题说明了一切。 i was thinking maybe because there are 81 textboxes it has somthing to do with layers but quite frankly i have no idea.. just started learning windows api like 2 days ago and ive been learning streight off the msdn library for functions.. i googled this problem multiple times and no luck so here i am. 我在想,也许是因为有81个文本框与图层有关,但坦率地说,我不知道..就像2天前才开始学习Windows api一样,我一直在学习msdn库中的函数功能。问题多次,没有运气,所以我在这里。 the help is much appreciated ^.^ 非常感谢帮助^。^

// Win32Project9.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project9.h"
#include "Resource.h"
#include <Windows.h>
#include <vector>
#include <cstring>

using namespace std;

HWND Hwnd;
HMENU hMenu;
HWND boxes[81];
int x, y;
vector<LPWSTR> BoxNum;


LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDM_EXIT:
        PostQuitMessage(0);
        return 0;
    break;

    case ID_SOLVE:
        for (int i = 0; i < 81; i++)
        {
            GetWindowText(boxes[i], BoxNum[i], NULL);
        }
    break;
    }
break;
}

if (msg == WM_COMMAND)
{
    if (LOWORD(wParam) > 199 && LOWORD(wParam) < 281)
    {
        if (HIWORD(wParam) == EN_SETFOCUS | HIWORD(wParam) == EN_UPDATE)
        {
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
}

else if (msg == WM_CLOSE)
{
    PostQuitMessage(0);
    return 0;
}

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

void DrawBoard()
{
x = 10;
y = 10;
int count = 0;

for (int i = 0; i < 81; i++)
{
    int BOX_ID = 200 + i;
    boxes[i] = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_BORDER | WS_VISIBLE, x, y, 20, 20, Hwnd, (HMENU)BOX_ID, NULL, NULL);

    x += 30;
    count++;

    if (count == 9)
    {
        y += 30;
        x = 10;
        count = 0;
    }
}
}

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{   

//structure to hold window specs
WNDCLASSEX Wc;

//allocate memory for window class
ZeroMemory(&Wc, sizeof(WNDCLASSEX));

//fill in neccessary info
Wc.cbSize = sizeof(WNDCLASSEX);

Wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
Wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
Wc.hInstance = hInstance;
Wc.lpfnWndProc = WindowProcedure;
Wc.lpszClassName = L"MyClass";
Wc.style = CS_HREDRAW | CS_VREDRAW;

//register class
RegisterClassEx(&Wc);

//load menu into handle
hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(ID_MENU));

//Create Window with class and create handle
Hwnd = CreateWindow(L"MyClass", L"Sudoku", WS_OVERLAPPEDWINDOW, 0, 0, 300, 340, NULL, hMenu, hInstance, NULL);

//DisplayWindow
ShowWindow(Hwnd, nCmdShow); 

DrawBoard();

//structure to hold input stream
MSG msg;

//listen for input
while(GetMessage(&msg, Hwnd, NULL, NULL))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return 0;
}

also i read that wm_close is processed when i press the x button. 我也看到当我按下x按钮时wm_close被处理了。 well this message is being recieved even when i click the text boxes. 好,即使我单击文本框也收到此消息。 and if you look at my case WM_Close.. i coded it to make a message box and give the user a chance to accept or not.... so when this happens after clicking on a text box i click no and another message box apperas asking again, i click no and it goes away but when i click the x button and i click no the window still dissapears ..... 如果您看我的情况WM_Close ..我将其编码为一个消息框,并为用户提供接受或不接受的机会....因此,在单击文本框后发生这种情况时,我单击否,然后单击另一个消息框再问一次,我单击“否”,它消失了,但是当我单击“ x”按钮,但我单击“否”时,窗口仍然消失.....

The problem is probably that WM_COMMAND is not handled properly. 问题可能是WM_COMMAND处理不正确。

The arguments received are as follows: 收到的参数如下:

WORD code = HIWORD(wParam);
WORD id = LOWORD(lParam);

The problem is that code depends on the type of control you are using. 问题在于code取决于您使用的控件类型。 For example, if it is a button it will be some of the BTN_* values, if it is an edit it will be EN_* and so on. 例如,如果它是一个按钮,它将是一些BTN_*值,如果它是一个编辑,它将是EN_* ,依此类推。 But these values overlap badly, so you cannot use them in a single switch. 但是这些值重叠严重,因此您不能在单个开关中使用它们。

For example CBN_KILLFOCUS==4 , but also LBN_SETFOCUS==4 ... Also menu items will get here a 0 and accelerators a 1. By the way, BN_CLICKED==0 and it looks like no other notification message uses 0, so you can use the same IDs in menus and buttons and it will just work. 例如CBN_KILLFOCUS==4 ,还有LBN_SETFOCUS==4 ...菜单项在这里也将为0,加速器为1。顺便说一句, BN_CLICKED==0 ,看起来没有其他通知消息使用0,所以您可以在菜单和按钮中使用相同的ID,它将正常工作。 And accelerators too, with a bit of care... BN_PAINT==1 , I think this one does not exist anymore, but you get the point... 还有加速器, BN_PAINT==1小心... BN_PAINT==1 ,我认为这已经不存在了,但是您明白了...

Anyway, to your problem. 无论如何,要解决您的问题。 My guess is that you have an EDIT that happens to have an ID equal to IDM_EXIT . 我的猜测是您有一个碰巧具有等于IDM_EXIT的ID的EDIT。 Since you are not checking the HIWORD(wParam) you are quitting when you receive the EN_SETFOCUS on this control. 由于您没有检查HIWORD(wParam)EN_SETFOCUS在此控件上收到EN_SETFOCUS时就退出了。

The solution is: First, always check both WORDs from wParam . 解决方案是:首先,始终检查wParam两个WORD。 Second, avoid collisions between menu options and ID controls, except maybe with buttons. 其次,避免菜单选项和ID控件之间发生冲突,除非可能与按钮发生冲突。

case WM_COMMAND:
    switch (LOWORD(wParam))

That isn't quite good enough. 那还不够好。 Edit controls also send WM_COMMAND messages to notify their parent window about stuff going on. 编辑控件还会发送WM_COMMAND消息,以通知其父窗口发生的事情。 Like EN_UPDATE whenever you type a character. 每次输入字符时都类似于EN_UPDATE。 Or EN_SETFOCUS when they get the focus, sounds like your case when you see this go wrong when you click on them. EN_SETFOCUS当他们获得焦点时,听起来就像是您的情况,当您单击它们时看到了错误。 These notifications are wrapped in a WM_COMMAND message. 这些通知包装在WM_COMMAND消息中。

You must therefore pay attention to where the WM_COMMAND message came from. 因此,您必须注意WM_COMMAND消息的来源。 The LPARAM argument tells you. LPARAM参数告诉您。 If IDM_EXIT comes from a menu item then you must verify that LPARAM is 0. Check the MSDN library for details. 如果IDM_EXIT来自菜单项,则必须验证LPARAM为0。有关详细信息,请检查MSDN库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM