关于WPF窗体焦点的问题~!~!

andy1020tc 2011-10-11 09:13:09
是这样的
我有个主窗体
窗体运行起来没什么毛病 但是有个很烦躁的地方
我点窗体第一下没反应
比如我点窗体最大化 点一下没反应
第二下窗体才最大化了

此窗体加载的一个usercontrol里的按钮也是这样

用窗体的this.Focus()方法 没什么作用
不知道有什么办法能解决的。。。
...全文
370 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bullatus 2011-10-13
  • 打赏
  • 举报
回复
你用窗体的PreviewMouseDown事件看看,你的第一次操作是否传入了
andy1020tc 2011-10-13
  • 打赏
  • 举报
回复
谢谢大家了 是我XAML里的问题 找出来了 工具条的IsOverflowOpen属性在作怪
改为FALSE就好了
0xAAFF 2011-10-13
  • 打赏
  • 举报
回复
好吧 ..我是小白
我觉得窗体玻璃化那里没有问题
那你在这里,我其实看不懂,因为我没用过鼠标拖动窗体,但是我想或许和鼠标有关系

[DllImport("user32.dll", EntryPoint = "SendMessage")]// 移动
public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
public static extern int ReleaseCapture();

也就是你的鼠标进来的时候先要和这个有关系,然后再执行其他的,或许这里就是原因。
你在调用这两个函数的时候先注释一下看看
如果是这个问题,修改下代码 我能说的 就这点
andy1020tc 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 bunliney 的回复:]
你用窗体的PreviewMouseDown事件看看,你的第一次操作是否传入了
[/Quote]
进去了 鼠标点第一下就进去了
晕倒啊 这是为什么

点其他的 要第二下才有反应
andy1020tc 2011-10-12
  • 打赏
  • 举报
回复
做了个win7的玻璃效果 要出问题的话 估计是那里
其他地方应该不会有什么问题
andy1020tc 2011-10-12
  • 打赏
  • 举报
回复

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
#region 引用 Dll
[DllImport("DwmApi.dll")] //玻璃
public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
[DllImport("user32.dll", EntryPoint = "SendMessage")]// 移动
public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
public static extern int ReleaseCapture();
public const int WM_SysCommand = 0x0112;
public const int SC_MOVE = 0xF012;
#endregion

private void ExtendAeroGlass(Window window)
{
try
{
// 为WPF程序获取窗口句柄
IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
// 设置Margins
MARGINS margins = new MARGINS();
// 扩展Aero Glass
margins.cxLeftWidth = -1;
margins.cxRightWidth = -1;
margins.cyTopHeight = -1;
margins.cyBottomHeight = -1;
int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
if (hr < 0)
{
MessageBox.Show("玻璃效果加载失败!");
}
}
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = Brushes.White;
}
}


private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Background = Brushes.Transparent;
ExtendAeroGlass(this);
}


private void window_SizeChanged(object sender, SizeChangedEventArgs e)
{
Size newSize = e.NewSize;
newSize = new Size(e.NewSize.Width - 6, 30);
Thickness thick = new Thickness(newSize.Width / 50, 0, 0, 0);
//MessageBox.Show(newSize.Height.ToString());
if (newSize.Width < 754)
{
this.lblLogo.Visibility = Visibility.Collapsed;
}
else if (newSize.Width < 915)
{
this.lblLogo.Visibility = Visibility.Visible;
btnMineSongs.Visibility = Visibility.Collapsed;
btnNewSongs.Visibility = Visibility.Collapsed;
btnNewUpLoadSong.Visibility = Visibility.Collapsed;
this.cboSearch.Width = newSize.Width / 7.5;
this.btnMineSong.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewSong.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewUpLoad.Margin = new Thickness(newSize.Width / 15, 0, 0, 0);
}
else
{
this.lblLogo.Visibility = Visibility.Visible;
btnMineSongs.Visibility = Visibility.Visible;
btnNewSongs.Visibility = Visibility.Visible;
btnNewUpLoadSong.Visibility = Visibility.Visible;
this.cboSearch.Width = newSize.Width / 6;
this.btnMineSongs.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewSongs.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewUpLoadSong.Margin = new Thickness(newSize.Width / 15, 0, 0, 0);
this.btnMineSong.Margin = new Thickness(0);
this.btnNewSong.Margin = new Thickness(0);
this.btnNewUpLoad.Margin = new Thickness(0);
}

this.lblLogo.Margin = new Thickness(newSize.Width / 180, 0, 0, 0);
this.btnLogin.Margin = new Thickness(newSize.Width / 90, 0, 0, 0);
this.btnMineSongs.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewSongs.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnNewUpLoadSong.Margin = new Thickness(newSize.Width / 15, 0, 0, 0);
this.cboSearch.Margin = new Thickness(newSize.Width / 60, 0, 0, 0);
this.btnTime.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnUpLoadSong.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);
this.btnDownLoadSong.Margin = new Thickness(newSize.Width / 45, 0, 0, 0);

//窗体大小变化 播放器始终位于窗体中间
if (iMusicControl1.Margin.Top == 0)
iMusicControl1.Margin = new Thickness((newSize.Width - iMusicControl1.Width) / 2, 0, 0, 0);
if (iMusicControl1.Margin.Left == 0)
iMusicControl1.Margin = new Thickness(0, (this.Height - iMusicControl1.Height - 20) / 2, 0, 0);
if (this.WindowState == WindowState.Maximized || this.Height > 438)
{
iMusicControl1.Margin = new Thickness((newSize.Width - iMusicControl1.Width) / 2, (this.Height - iMusicControl1.Height - 20) / 2 , 0, 0);
}
//this.Title = this.Height.ToString();
}

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}

private void btnRegiest_Click(object sender, RoutedEventArgs e)
{
RegistWindow regist = new RegistWindow();
regist.ShowDialog();
}
}
andy1020tc 2011-10-12
  • 打赏
  • 举报
回复
别沉了啊。。。。
only9002 2011-10-11
  • 打赏
  • 举报
回复
你的代码是什么,帖出来看看
andy1020tc 2011-10-11
  • 打赏
  • 举报
回复
不是就这么要沉了吧 别啊 我急着想解决呢

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧