如何设置表单的位置

时间:2014-07-17 17:13:40

标签: c#

我想要做的是制作一个dll,当跑步时会弹出一个无边框。表格将始终位于最顶层,并且不透明。我也可以点击表格。截至目前所有这些都有效,我遇到的问题是设置表单位置。当我尝试设置表单位置时,它不会移动。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace pic
{
public class Class1
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_LAYERED = 0x80000;
    public const int WS_EX_TRANSPARENT = 0x20;
    public const int LWA_ALPHA = 0x2;
    public const int LWA_COLORKEY = 0x1;


    public void t()
    {

        Console.Beep(100,100);
        Form f = new Form();
        f.BackColor = Color.White;
        f.FormBorderStyle = FormBorderStyle.None;
        f.Bounds = Screen.PrimaryScreen.Bounds;
        f.TopMost = true;
        Bitmap bitmap = new Bitmap("c:\\users\\mike\\documents\\visual studio 2013\\Projects\\pic\\pic\\Grapes.png");
        f.Size = new Size(bitmap.Size.Width, bitmap.Size.Height);
        f.Location = new Point(500, 500);
        Application.EnableVisualStyles();
        Console.Beep(500,200);
        SetWindowLong(f.Handle, GWL_EXSTYLE,
        (IntPtr)(GetWindowLong(f.Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
        // set transparency to 50% (128)
        SetLayeredWindowAttributes(f.Handle, 0, 128, LWA_ALPHA);
        f.BackgroundImage = Bitmap.FromFile("c:\\users\\mike\\documents\\visual studio 2013\\Projects\\pic\\pic\\Grapes.png");
        Application.Run(f);

    }

}
}

1 个答案:

答案 0 :(得分:1)

您还必须设置表单StartPosition,因为默认值为(可能)WindowsDefaultLocation。使用默认位置,Windows将自动放置表单

f.StartPosition = Manual;

这将使用Location坐标来放置表单。