无法创建全屏WPF弹出窗口

时间:2010-04-13 20:55:51

标签: .net wpf

在VS2010 RTM中使用WPF .NET 4.0:我无法创建全屏WPF弹出窗口。

如果我创建一个大小为50%宽度和100%高度的弹出窗口,一切正常,但如果我尝试创建一个大小为100%宽度和高度的“全屏”弹出窗口,它最终会以100%的宽度显示高度为75%......底部被截断。

注意:宽度和高度实际上是以代码中的像素表示的,我使用百分比来使情况更容易理解......

它“感觉”有某种限制,防止弹出区域超过屏幕总面积的约75%。

更新:这是一个显示问题的Hello World示例。

<Window x:Class="TechnologyVisualizer.PopupTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PopupTest" 
        WindowStyle="None" WindowState="Maximized" Background="DarkGray">
    <Canvas x:Name="MainCanvas" Width="1920" Height="1080">
        <Popup Placement="Center" VerticalAlignment="Center" HorizontalAlignment="Center" Width="1900" Height="1060" Name="popContent">
            <TextBlock Background="Red">Hello World</TextBlock>
        </Popup>
        <Button Canvas.Left="50" Canvas.Top="50" Content="Menu" Height="60" Name="button1" Width="80"
                 FontSize="22" Foreground="White" Background="Black" Click="button1_Click" />
    </Canvas>
</Window>




 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TechnologyVisualizer
{
    /// <summary>
    /// Interaction logic for PopupTest.xaml
    /// </summary>
    public partial class PopupTest : Window
    {
        public PopupTest()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            popContent.IsOpen = true;
        }

    }
}

如果您运行此选项,如果将弹出窗口的宽度更改为500,则弹出窗口的底部25%将丢失,然后它将变为全高

3 个答案:

答案 0 :(得分:11)

您对尺寸限制的猜测(屏幕的75%)是正确的。它记录在这里:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.placementmode%28VS.85%29.aspx

.NET 4文档中缺少相关说明。我想你需要使用另一种获取全屏的方法。我通常使用最大化,无边框,不可调整大小的窗口。

答案 1 :(得分:0)

我现在没有安装VS2010来试用它,但如果这应该是WPF中的错误(如评论中所述),我肯定使用P / Invoke和< Raymond Chen发布的强> this sample 应该让它发挥作用:

WINDOWPLACEMENT g_wpPrev = { sizeof(g_wpPrev) };

void OnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
{
  DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
  if (dwStyle & WS_OVERLAPPEDWINDOW) {
    MONITORINFO mi = { sizeof(mi) };
    if (GetWindowPlacement(hwnd, &g_wpPrev) &&
        GetMonitorInfo(MonitorFromWindow(hwnd,
                       MONITOR_DEFAULTTOPRIMARY), &mi)) {
      SetWindowLong(hwnd, GWL_STYLE,
                    dwStyle & ~WS_OVERLAPPEDWINDOW);
      SetWindowPos(hwnd, HWND_TOP,
                   mi.rcMonitor.left, mi.rcMonitor.top,
                   mi.rcMonitor.right - mi.rcMonitor.left,
                   mi.rcMonitor.bottom - mi.rcMonitor.top,
                   SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
    }
  } else {
    SetWindowLong(hwnd, GWL_STYLE,
                  dwStyle | WS_OVERLAPPEDWINDOW);
    SetWindowPlacement(hwnd, &g_wpPrev);
    SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
                 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
                 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  }
}

很抱歉没有更多的帮助,但我认为C ++示例可以使用相应的DllImport轻松转换为C#。查看pinvoke.net以获取有关将其转换为C#的更多帮助。

答案 2 :(得分:0)

set the with of dialog from code, in constructor

public PopupTest()
{
    InitializeComponent();
    this.Width = Window.Current.Bounds.Width;
    this.Height = Window.Current.Bounds.Height;
}
相关问题