最近做了个winfom的东西还挺复杂的,负责其中的一块,以前没有做过winform,只弄过webform,直接上手,虽然它们有很多东西很像,但也有很多东西不像。winfrom真是不适合做很炫的界面,很多简单的东西都要自己写控件,还有就是要布局经常动态算各种坐标。
备忘下。
1.属性FormBorderStyle 设置为 None 去掉form边框
2.图片布局好运行起来却是有些偏离 AutoScaleMode 设为 None;
3.BackColor TransParent 透明
4.panelFrameImg相对于panelBottom的坐标
var pTmp = panelBottom.PointToClient(panelFrameImg.Location);
5. btn.Left btn边缘左边距左的距离
6. btn.Right btn边缘右边距左的距离
7.用户控件中FindForm() 查找当前的From
8. 用户控件间传值可用委托 或 xx.parent.parent...Controls["controlName"]
9.没有onblur事件,可用鼠标点击任意位置触发
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEDOWN)
{
MouseDownOnForm();
}
base.WndProc(ref m);
}
10. 获取资源文件
Properties.Resources.xxx.png;
11. 按钮鼠标点击移动背景色
设置FlatApperance下的MouseDownBackColor,MouseOverBackColor
不规则图片为按钮背景,设为TransParent透明很有用。
12. Control.Invalidate();使控件的整个图面无效并导致重绘控件
13. Control.Dispose() ;移除控件
14. Dock属性设置 停靠,Anchor属性设置 锚定 窗口最大化时很有用
15.当前鼠标移动的坐标
btn_MouseMove(object sender, MouseEventArgs e)
e.X,e.Y
16.鼠标在屏幕点位置计算成工作区坐标
var pTemp = new Point(Cursor.Position.X, Cursor.Position.Y);
pTemp = mainPanel.PointToClient(pTemp);
17. 制作一个自定义ComboBox,边框背景颜色为纯色,下拉箭头自定义
1).组件类Component1.cs继承ComboBox把背景和边框绘制成纯色
2).将组件拖到用户控件中,再用下拉箭头图片盖住ComboBox自带的下拉箭头;
设置下拉箭头图片点击事件 comboBox1.DroppedDown = true; 会触发ComboBox下拉;
编译完,点 项目 > 刷新项目工具箱 可在左边组件栏看到控件
18.winform中的 DroppedDownList控件名叫ComboBox;combobox去除选中蓝色背景 最简的方式
comboBox1.Enabled = false;
comboBox1.Enabled = true;
19.在用户控件代码里public get set 使引用它的地方可以设置自定义ComboBox的属性等。
如:
public object SelectedItem
{
get
{
return comboBox1.SelectedItem;
}
}
20. 子窗体一打开居中
this.StartPosition = FormStartPosition.CenterParent;