-2

我能够将图像添加到按钮作为背景,但后来我想将文本添加到按钮作为“Weclome”,我尝试了使用 Settext、SendmessageA 的所有可能方式。请帮忙

#include <Windows.h>

int main()
{
    MSG msg;
    HWND hWnd = CreateWindow(TEXT("button"), TEXT("START"), WS_VISIBLE | WS_POPUP | WS_CHILD | WS_TABSTOP | BS_BITMAP,
        250, 250, 500, 500, NULL, NULL, NULL, NULL);
    HANDLE hImg = LoadImageW(NULL, L"Untitled.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
    SendMessageW(hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImg);

    SendMessageA(hWnd, WM_SETTEXT, 0, (LPARAM)"Welcome");
    //SendMessageW(hWnd, WM_SETTEXT, (WPARAM) 256,NULL);


    while (GetMessage(&msg, NULL, 0, 0))
    {
        ShowWindow(hWnd, SW_SHOW);
        UpdateWindow(hWnd);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
4

1 回答 1

0

您确实意识到您必须创建一个窗口,然后将按钮放在窗口内?

程序可能正在错误的目录中寻找文件。使用完整路径名并进行错误检查以确保已加载位图。例子:

HANDLE hImg = LoadImageW(NULL, L"c:\\fullpath\\Untitled.bmp", IMAGE_BITMAP, 0, 0, 
LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);

if (!hImg)
    report error...

不要将ShowWindowandUpdateWindow放在消息循环中。只需显示窗口,然后调用消息循环。例子:

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
于 2016-07-27T17:19:48.490 回答