tenho seguinte código winui3 c++ tenho seguinte código winui3 c++ namespace…
- User
tenho seguinte código winui3 c++
tenho seguinte código winui3 c++
namespace winrt::DataIntegrityTools::implementation { struct MainWindow : MainWindowT { MainWindow() { int windowWidth = 1600; int windowHeight = 800;
ExtendsContentIntoTitleBar(true); SetTitleBar(AppTitleBar()); //window.Title(L"Data Integrity Tools"); auto appWindow = AppWindow(); //appWindow.SetIcon(L"Assets/app.ico"); appWindow.Resize({ windowWidth, windowHeight }); auto presenter = appWindow.Presenter().as<OverlappedPresenter>(); presenter.IsResizable(false); presenter.IsMaximizable(false); RECT workArea; SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0); int screenWidth = workArea.right - workArea.left; int screenHeight = workArea.bottom - workArea.top; int posX = workArea.left + (screenWidth - windowWidth) / 2; int posY = workArea.top + (screenHeight - windowHeight) / 2; appWindow.Move({ posX, posY });}
int32_t MyProperty(); void MyProperty(int32_t value); }; }
Como posso desativar o maximizar usando api do windows ?
- Assistant
Para desativar o botão de maximizar usando a API do Windows, você precisa acessar o identificador da janela (HWND) e modificar o estilo da janela para remover o
WS_MAXIMIZEBOX. Aqui está como você pode fazer isso em seu código WinUI 3 C++:Primeiro, inclua o cabeçalho necessário para acessar a interface
IWindowNative:#include <Microsoft.UI.Interop.h>Em seguida, dentro do seu construtor
MainWindow(), após a criação da janela, obtenha oHWNDda janela:auto windowNative = this->try_as<::IWindowNative>(); HWND hwnd{}; if (windowNative && SUCCEEDED(windowNative->get_WindowHandle(&hwnd))) { // Remove o botão de maximizar DWORD style = GetWindowLong(hwnd, GWL_STYLE); style &= ~WS_MAXIMIZEBOX; SetWindowLong(hwnd, GWL_STYLE, style); // Atualiza a janela para refletir as mudanças no estilo SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED); }Certifique-se de chamar
SetWindowPoscom a flagSWP_FRAMECHANGEDpara que o Windows atualize a área não cliente da janela, refletindo a remoção do botão de maximizar.Seu código completo ficaria assim:
#include "pch.h" #include "MainWindow.h" #include "MainWindow.g.cpp" #include <Microsoft.UI.Interop.h> // Necessário para IWindowNative using namespace winrt; using namespace Windows::UI::Xaml; namespace winrt::DataIntegrityTools::implementation { MainWindow::MainWindow() { int windowWidth = 1600; int windowHeight = 800; ExtendsContentIntoTitleBar(true); SetTitleBar(AppTitleBar()); auto appWindow = AppWindow(); appWindow.Resize({ windowWidth, windowHeight }); auto presenter = appWindow.Presenter().as<OverlappedPresenter>(); presenter.IsResizable(false); presenter.IsMaximizable(false); RECT workArea; SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0); int screenWidth = workArea.right - workArea.left; int screenHeight = workArea.bottom - workArea.top; int posX = workArea.left + (screenWidth - windowWidth) / 2; int posY = workArea.top + (screenHeight - windowHeight) / 2; appWindow.Move({ posX, posY }); // Obtém o HWND da janela auto windowNative = this->try_as<::IWindowNative>(); HWND hwnd{}; if (windowNative && SUCCEEDED(windowNative->get_WindowHandle(&hwnd))) { // Remove o botão de maximizar DWORD style = GetWindowLong(hwnd, GWL_STYLE); style &= ~WS_MAXIMIZEBOX; SetWindowLong(hwnd, GWL_STYLE, style); // Atualiza a janela para refletir as mudanças no estilo SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED); } } int32_t MainWindow::MyProperty() { throw hresult_not_implemented(); } void MainWindow::MyProperty(int32_t /* value */) { throw hresult_not_implemented(); } }Com essas alterações, o botão de maximizar será removido da barra de título da janela, desabilitando a funcionalidade de maximizar via API do Windows.
Explicação:
IWindowNativeé usado para acessar oHWNDda janela da sua aplicação WinUI 3.GetWindowLongeSetWindowLongsão usados para modificar o estilo da janela. RemovemosWS_MAXIMIZEBOXpara desativar o botão de maximizar.SetWindowPoscom a flagSWP_FRAMECHANGEDavisa o sistema para atualizar a moldura da janela para refletir as mudanças no estilo.
Notas Adicionais:
- Certifique-se de que as chamadas para modificar o estilo da janela estão sendo feitas após a criação da janela.
- Lembre-se de incluir
Microsoft.UI.Interop.hpara acessarIWindowNative.
Assim, você pode desativar o maximizar usando a API do Windows em seu aplicativo WinUI 3 C++.
Referências:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.