如何使用C#制作窗体?

shijies 2020-06-30 10:03:52
除了开发工具默认生成的窗体,制作新的窗体只能使用代码?
...全文
4933 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
DONGF331 2020-07-02
  • 打赏
  • 举报
回复
创建一个winform窗体
datafansbj 2020-07-01
  • 打赏
  • 举报
回复
引用 3 楼 shijies 的回复:
在VS 2017中没有看到供设计窗体使用的组件,生成一个最基本的窗体也需要写代码?


你是不是 VS2017 安装的不全?必须选择“Windows 桌面开发”这一项才有窗体设计器。
shijies 2020-07-01
  • 打赏
  • 举报
回复
在VS 2017中没有看到供设计窗体使用的组件,生成一个最基本的窗体也需要写代码?
软泡芙 2020-07-01
  • 打赏
  • 举报
回复
体验好的窗体都要代码去自定义的吧,要是不想写太多代码就调别人的窗体库。
desperaso 2020-06-30
  • 打赏
  • 举报
回复
winform的异形图像窗体、无框阴影窗体、动画窗体等等,搞窗体的路还很长
比如无框阴影的

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace contextUI
{
public partial class ConextForm : Form
{
public bool Sizeable = true;

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);

[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

private bool m_aeroEnabled = false;
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCPAINT = 0x0085;
private const int WM_ACTIVATEAPP = 0x001C;

public struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

protected override CreateParams CreateParams
{
get
{
m_aeroEnabled = CheckAeroEnabled();

CreateParams cp = base.CreateParams;
if (!m_aeroEnabled)
cp.ClassStyle |= CS_DROPSHADOW;

return cp;
}
}

private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}

const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 0x10;
const int HTBOTTOMRIGHT = 17;

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCPAINT:
if (m_aeroEnabled)
{
var v = 2;
DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 1,
rightWidth = 1,
topHeight = 1
};
DwmExtendFrameIntoClientArea(this.Handle, ref margins);

}
break;
case 0x0084:
if (Sizeable)
{
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPLEFT;
}
else
if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMLEFT;
}
else
{
m.Result = (IntPtr)HTLEFT;
}
}
else
if (vPoint.X >= ClientSize.Width - 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPRIGHT;
}
else
if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
}
else
{
m.Result = (IntPtr)HTRIGHT;
}
}
else
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOP;
}
else
{
if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOM;
}
}

}
break;

default:
break;
}
}

private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new Size(284, 261);
this.DoubleBuffered = true;
this.Name = "conextForm";
this.ResumeLayout(false);

}
}
}
datafansbj 2020-06-30
  • 打赏
  • 举报
回复
如果窗体在设计时已经做好,那么运行时只要 new,然后 show 即可。
如果窗体在运行时动态生成,那就需要编写代码来实现。
详见窗体的 .Designer.cs 文件,那个就是窗体的设计文件。

110,538

社区成员

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

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

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