winapi中的按钮scalling

时间:2016-03-23 00:20:48

标签: user-interface winapi visual-c++

我正在创建一个简单的计算器,我在扩展所有元素时遇到问题。他们必须与主窗口缩放,但我不知道如何执行这样的事情 以下是代码的一部分:

#define ID_BUTTON0 0
#define ID_BUTTON1 1
#define ID_BUTTON2 2
#define ID_BUTTON3 3
#define ID_BUTTON4 4
...

case WM_CREATE:
{
    CreateWindow(L"STATIC", L"", WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_TEXTFIELD), 0, NULL);
    CreateWindow(L"BUTTON", L"CE",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONCE), 0, NULL);
    CreateWindow(L"BUTTON", L"C",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONC), 0, NULL);
    CreateWindow(L"BUTTON", L"/",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONDIV), 0, NULL);
    CreateWindow(L"BUTTON", L"X",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONMUL), 0, NULL);
    CreateWindow(L"BUTTON", L"7",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON7), 0, NULL);
    CreateWindow(L"BUTTON", L"8",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON8), 0, NULL);
    CreateWindow(L"BUTTON", L"9",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON9), 0, NULL);
    ...

    resize_window(hWnd);
}
break;
case WM_SIZE:
{
    resize_window(hWnd);
    UpdateWindow(hWnd);
    return 0;
}
void resize_window(HWND hWnd) {

int buttonxsize = WINDOWSIZEW / 4;
int buttonysize = WINDOWSIZEH / 6;
int posx = WINDOWSIZEW / 4;
int posy = WINDOWSIZEH / 6;
RECT rc;
GetClientRect(hWnd, &rc);
SetWindowPos(GetDlgItem(hWnd, ID_TEXTFIELD), 0, 0, 0, WINDOWSIZEW, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONC), 0, 0, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONCE), 0, posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONDIV), 0, 2 * posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONMUL), 0, 3 * posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
...
}

依旧......

编译后我得到了这样的东西:enter image description here

如何让它发挥作用? 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

/^[\d]+[a-zA-Z]*\s+[a-zA-Z]+$|^[a-zA-Z]+\s+\b[\d]+[a-zA-Z]*\b$/

HWND hwndTextField = CreateWindow(L"Static", L"", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP, 0, 0, WINDOWSIZEW, WINDOWSIZEH / 6, hWnd, NULL, GetModuleHandle(NULL), NULL); 在堆栈中声明,一旦超出范围就会被遗忘,您以后无法找到控件(不管怎么说都不容易)。 hwndTextField应声明为静态或全局。更好的是,使用hwndTextField为每个控件分配ID。此ID可用于查找控件。此外,当您单击按钮时,它会发送带有该ID的HMENU消息。

此控件的样式应为WM_COMMAND。但是,除非在对话框中创建,否则将忽略WS_VISIBLE | WS_CHILD | WS_TABSTOP

并在单独的函数中调整控件的大小,以便可以从WS_TABSTOPWM_CREATE

完成

例如

WM_SIZE