在新窗口中生成图像。

huangapple go评论74阅读模式
英文:

Image generation in a New Window

问题

我正在制作一个程序,可以发送我想要的自定义通知。我有一个显示在屏幕上的窗口,但是图像没有加载。

我已经尝试使用Chat GPT来检查我的代码,但由于其知识限制是在2021年9月,它不知道API的新更新,无法给我正确的回答。

以下是您的代码部分:

#include <Windows.h>
#include <Shellapi.h>

// 常量
#define WM_CLOSENOTIFICATION (WM_USER + 1)
#define BUTTON_OPENURL_ID 100
#define BUTTON_CLOSE_ID 101
#define IDB_NOTIFICATION_BITMAP 108 // 用您的位图资源ID替换

// 全局变量
HWND hMainWindow;
HWND hNotificationWindow;
HBITMAP hBitmap;

// 创建和显示通知窗口的函数
void ShowNotification()
{
    // 获取屏幕尺寸
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);

    // 计算通知窗口的中心位置
    int notificationWidth = 400; // 设置您希望的宽度
    int notificationHeight = 200; // 设置您希望的高度
    int notificationX = (screenWidth - notificationWidth) / 2;
    int notificationY = (screenHeight - notificationHeight) / 2;

    // 创建自定义通知窗口
    hNotificationWindow = CreateWindow(
        L"NotificationWindowClass",         // 类名
        L"Custom Notification",             // 窗口标题
        WS_POPUP | WS_VISIBLE | WS_BORDER,  // 窗口样式
        notificationX, notificationY,        // 位置
        notificationWidth, notificationHeight,   // 尺寸
        nullptr, nullptr, nullptr, nullptr   // 父窗口,菜单句柄,实例,附加数据
    );

    // 加载位图资源
    hBitmap = LoadBitmap(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDB_NOTIFICATION_BITMAP));
    if (hBitmap == nullptr)
    {
        OutputDebugStringA("Error Bitmap Null Value");
        // 显示适当的错误消息或采取必要的操作
    }

    // 创建图像的静态控件
    HWND hImageStatic = CreateWindow(
        L"STATIC",                      // 窗口类名
        nullptr,                        // 文本内容(图像为空)
        WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_CENTERIMAGE,  // 窗口样式
        10, 10, notificationWidth - 20, notificationHeight - 20, // 位置和尺寸
        hNotificationWindow, nullptr, nullptr, nullptr  // 父窗口,菜单句柄,实例,附加数据
    );
    SendMessage(hImageStatic, STM_SETIMAGE, IMAGE_BITMAP, reinterpret_cast<LPARAM>(hBitmap));

    // 创建一个打开网站的按钮
    HWND hOpenButton = CreateWindow(
        L"BUTTON",                      // 窗口类名
        L"Open Website",                // 文本内容
        WS_CHILD | WS_VISIBLE | BS_CENTER | BS_DEFPUSHBUTTON,  // 窗口样式
        10, notificationHeight - 60, (notificationWidth - 40) / 2, 30, // 位置和尺寸
        hNotificationWindow, reinterpret_cast<HMENU>(BUTTON_OPENURL_ID), nullptr, nullptr  // 父窗口,菜单句柄,实例,附加数据
    );

    // 创建一个关闭通知的按钮
    HWND hCloseButton = CreateWindow(
        L"BUTTON",                      // 窗口类名
        L"Close",                       // 文本内容
        WS_CHILD | WS_VISIBLE | BS_CENTER | BS_DEFPUSHBUTTON,  // 窗口样式
        20 + (notificationWidth - 40) / 2, notificationHeight - 60, (notificationWidth - 40) / 2, 30, // 位置和尺寸
        hNotificationWindow, reinterpret_cast<HMENU>(BUTTON_CLOSE_ID), nullptr, nullptr  // 父窗口,菜单句柄,实例,附加数据
    );
}

// 通知窗口的窗口过程
LRESULT CALLBACK NotificationWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        // 处理命令消息
        if (LOWORD(wParam) == BUTTON_OPENURL_ID)
        {
            // 单击按钮时打开网站
            ShellExecute(nullptr, L"open", L"https://example.com", nullptr, nullptr, SW_SHOWNORMAL);
        }
        else if (LOWORD(wParam) == BUTTON_CLOSE_ID)
        {
            // 再次显示主窗口
            ShowWindow(hMainWindow, SW_SHOW);
            // 清理并退出通知窗口
            DestroyWindow(hNotificationWindow);
        }
        break;

    case WM_CLOSE:
        // 再次显示主窗口
        ShowWindow(hMainWindow, SW_SHOW);
        // 清理并退出通知窗口
        DestroyWindow(hNotificationWindow);
        break;
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
}

// 入口点
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    // 创建并注册通知窗口的窗口类
    WNDCLASS wc = {};
    wc.lpfnWndProc = NotificationWindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = L"NotificationWindowClass";
    RegisterClass(&wc);

    // 创建主窗口
    hMainWindow = CreateWindow(
        L"MainWindowClass",             // 窗口类名
        L"Main Window",                 // 窗口标题
        WS_OVERLAPPEDWINDOW,            // 窗口样式
        CW_USEDEFAULT, CW_USEDEFAULT,   // 位置
        CW_USEDEFAULT, CW_USEDEFAULT,   // 尺寸
        nullptr, nullptr, hInstance, nullptr   // 父窗口,菜单句柄,实例,附加数据
    );

    // 显示主窗口
    ShowWindow(hMainWindow, nCmdShow);

    // 显示初始通知
    ShowNotification();

    // 消息循环
    MSG msg = {};
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 清理资源
    if (hBitmap !=

<details>
<summary>英文:</summary>

I am making a program that will send a custom notification sent for whatever I want. I have a window that shows up on the screen, but the image doesn&#39;t load.

I have tried using Chat GPT to check my code, but with its knowledge limit being Sept 2021 it doesn&#39;t know the new updates to API&#39;s to give me a correct response.

Code can be found below:

    #include &lt;Windows.h&gt;
    #include &lt;Shellapi.h&gt;
     
    // Constants
    #define WM_CLOSENOTIFICATION (WM_USER + 1)
    #define BUTTON_OPENURL_ID 100
    #define BUTTON_CLOSE_ID 101
    #define IDB_NOTIFICATION_BITMAP 108 // Replace with your bitmap resource ID
     
    // Global variables
    HWND hMainWindow;
    HWND hNotificationWindow;
    HBITMAP hBitmap;
     
    // Function to create and show the notification window
    void ShowNotification()
    {
        // Get the screen dimensions
        int screenWidth = GetSystemMetrics(SM_CXSCREEN);
        int screenHeight = GetSystemMetrics(SM_CYSCREEN);
     
        // Calculate the center position for the notification window
        int notificationWidth = 400; // Set your desired width
        int notificationHeight = 200; // Set your desired height
        int notificationX = (screenWidth - notificationWidth) / 2;
        int notificationY = (screenHeight - notificationHeight) / 2;
     
        // Create the custom notification window
        hNotificationWindow = CreateWindow(
            L&quot;NotificationWindowClass&quot;,         // Class name
            L&quot;Custom Notification&quot;,             // Window title
            WS_POPUP | WS_VISIBLE | WS_BORDER,  // Window styles
            notificationX, notificationY,        // Position
            notificationWidth, notificationHeight,   // Size
            nullptr, nullptr, nullptr, nullptr   // Parent window, Menu handle, Instance, Additional data
        );
     
        // Load the bitmap resource
        hBitmap = LoadBitmap(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDB_NOTIFICATION_BITMAP));
        if (hBitmap == nullptr)
        {
            OutputDebugStringA(&quot;Error Bitmap Null Value&quot;);
            // Display appropriate error message or take necessary actions
        }
     
        // Create a static control for the image
        HWND hImageStatic = CreateWindow(
            L&quot;STATIC&quot;,                      // Window class name
            nullptr,                        // Text content (empty for image)
            WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_CENTERIMAGE,  // Window styles
            10, 10, notificationWidth - 20, notificationHeight - 20, // Position and size
            hNotificationWindow, nullptr, nullptr, nullptr  // Parent window, Menu handle, Instance, Additional data
        );
        SendMessage(hImageStatic, STM_SETIMAGE, IMAGE_BITMAP, reinterpret_cast&lt;LPARAM&gt;(hBitmap));
     
        // Create a button to open a website
        HWND hOpenButton = CreateWindow(
            L&quot;BUTTON&quot;,                      // Window class name
            L&quot;Open Website&quot;,                // Text content
            WS_CHILD | WS_VISIBLE | BS_CENTER | BS_DEFPUSHBUTTON,  // Window styles
            10, notificationHeight - 60, (notificationWidth - 40) / 2, 30, // Position and size
            hNotificationWindow, reinterpret_cast&lt;HMENU&gt;(BUTTON_OPENURL_ID), nullptr, nullptr  // Parent window, Menu handle, Instance, Additional data
        );
     
        // Create a button to close the notification
        HWND hCloseButton = CreateWindow(
            L&quot;BUTTON&quot;,                      // Window class name
            L&quot;Close&quot;,                       // Text content
            WS_CHILD | WS_VISIBLE | BS_CENTER | BS_DEFPUSHBUTTON,  // Window styles
            20 + (notificationWidth - 40) / 2, notificationHeight - 60, (notificationWidth - 40) / 2, 30, // Position and size
            hNotificationWindow, reinterpret_cast&lt;HMENU&gt;(BUTTON_CLOSE_ID), nullptr, nullptr  // Parent window, Menu handle, Instance, Additional data
        );
    }
     
    // Window procedure for the notification window
    LRESULT CALLBACK NotificationWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_COMMAND:
            // Handle command messages
            if (LOWORD(wParam) == BUTTON_OPENURL_ID)
            {
                // Open a website when the button is clicked
                ShellExecute(nullptr, L&quot;open&quot;, L&quot;https://example.com&quot;, nullptr, nullptr, SW_SHOWNORMAL);
            }
            else if (LOWORD(wParam) == BUTTON_CLOSE_ID)
            {
                // Show the main window again
                ShowWindow(hMainWindow, SW_SHOW);
                // Clean up and exit the notification window
                DestroyWindow(hNotificationWindow);
            }
            break;
     
        case WM_CLOSE:
            // Show the main window again
            ShowWindow(hMainWindow, SW_SHOW);
            // Clean up and exit the notification window
            DestroyWindow(hNotificationWindow);
            break;
        }
     
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
     
    // Entry point
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
    {
        // Create and register the window class for the notification window
        WNDCLASS wc = {};
        wc.lpfnWndProc = NotificationWindowProc;
        wc.hInstance = hInstance;
        wc.lpszClassName = L&quot;NotificationWindowClass&quot;;
        RegisterClass(&amp;wc);
     
        // Create the main window
        hMainWindow = CreateWindow(
            L&quot;MainWindowClass&quot;,             // Window class name
            L&quot;Main Window&quot;,                 // Window title
            WS_OVERLAPPEDWINDOW,            // Window styles
            CW_USEDEFAULT, CW_USEDEFAULT,   // Position
            CW_USEDEFAULT, CW_USEDEFAULT,   // Size
            nullptr, nullptr, hInstance, nullptr   // Parent window, Menu handle, Instance, Additional data
        );
     
        // Show the main window
        ShowWindow(hMainWindow, nCmdShow);
     
        // Show the initial notification
        ShowNotification();
     
        // Message loop
        MSG msg = {};
        while (GetMessage(&amp;msg, nullptr, 0, 0))
        {
            TranslateMessage(&amp;msg);
            DispatchMessage(&amp;msg);
        }
     
        // Clean up resources
        if (hBitmap != nullptr)
        {
            DeleteObject(hBitmap);
        }
     
        return 0;
    }

Update: Here is a screenshot of my .rc file in VS. I have to see if I can find the file in my source files.

[.rc file][1]


And here is what the program currently makes when you run it.

[Program Execution Result][2]


  [1]: https://i.stack.imgur.com/P9vc2.jpg
  [2]: https://i.stack.imgur.com/MkLN2.png

</details>


# 答案1
**得分**: 1

切换到 GDI+ 而不是位图,它立即起作用。

感谢 @jwezorek 的帮助。

<details>
<summary>英文:</summary>

Switched over to GDI + instead of Bitmaps and it worked instantly. 

Thank you for trying to help @jwezorek.

</details>



huangapple
  • 本文由 发表于 2023年7月13日 23:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定