GVKun编程网logo

delphi 自定义弹出窗口,减少form(dfm文件)的数量(delphi 弹出对话框)

15

以上就是给各位分享delphi自定义弹出窗口,减少form,其中也会对dfm文件的数量进行解释,同时本文还将给你拓展android实现百度地图自定义弹出窗口功能、Android自定义弹出窗口Popup

以上就是给各位分享delphi 自定义弹出窗口,减少form,其中也会对dfm文件的数量进行解释,同时本文还将给你拓展android实现百度地图自定义弹出窗口功能、Android自定义弹出窗口PopupWindow使用技巧、C# winForm自定义弹出页面效果、CSS3/jQuery自定义弹出窗口等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

delphi 自定义弹出窗口,减少form(dfm文件)的数量(delphi 弹出对话框)

delphi 自定义弹出窗口,减少form(dfm文件)的数量(delphi 弹出对话框)

总结

以上是小编为你收集整理的delphi 自定义弹出窗口,减少form(dfm文件)的数量全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

android实现百度地图自定义弹出窗口功能

android实现百度地图自定义弹出窗口功能

我们使用百度地图的时候,点击地图上的Marker,会弹出一个该地点详细信息的窗口,如下左图所示,有时候,我们希望自己定义这个弹出窗口的内容,或者,干脆用自己的数据来构造这样的弹出窗口,但是,在百度地图最新的Android SDK中,没有方便操作这种弹出窗口的类,虽然有一个PopupOverlay,但是它只支持将弹出内容转化为不多于三个Bitmap,如果这个弹出窗口里想有按钮来响应点击事件,用这个就不能满足要求了,于是,看了一遍百度地图覆盖物的API,我决定用自定义view的方法来实现类似的效果,先贴一下大体效果图,如下右图:

基本原理就是用itemizedoverlay来添加附加物,在OnTap方法中向MapView上添加一个自定义的View(如果已存在就直接设为可见),下面具体来介绍我的实现方法:

一、自定义覆盖物类:MyPopupOverlay,这个类是最关键的一个类itemizedoverlay,用于设置Marker,并定义Marker的点击事件,弹出窗口,至于弹出窗口的内容,则通过定义Listener,放到Activity中去构造。如果没有特殊需求,这个类不需要做什么改动。代码如下,popupLinear这个对象,就是加到地图上的自定义view:

复制代码 代码如下:

public class MyPopupOverlay extends itemizedoverlay<OverlayItem> {

    private Context context = null;
    // 这是弹出窗口, 包括内容部分还有下面那个小三角
    private LinearLayout popupLinear = null;
    // 这是弹出窗口的内容部分
    private View popupView = null;
    private MapView mapView = null;
    private Projection projection = null;

    // 这是弹出窗口内容部分使用的layoutId,在Activity中设置
    private int layoutId = 0;
    // 是否使用百度带有A-J字样的Marker
    private boolean useDefaultMarker = false;
    private int[] defaultMarkerIds = { R.drawable.icon_marka,
            R.drawable.icon_markb,R.drawable.icon_markc,
            R.drawable.icon_markd,R.drawable.icon_marke,
            R.drawable.icon_markf,R.drawable.icon_markg,
            R.drawable.icon_markh,R.drawable.icon_marki,
            R.drawable.icon_markj,};

    // 这个Listener用于在Marker被点击时让Activity填充PopupView的内容
    private OnTapListener onTapListener = null;

    public MyPopupOverlay(Context context,Drawable marker,MapView mMapView) {
        super(marker,mMapView);
        this.context = context;
        this.popupLinear = new LinearLayout(context);
        this.mapView = mMapView;
        popupLinear.setorientation(LinearLayout.VERTICAL);
        popupLinear.setVisibility(View.GONE);
        projection = mapView.getProjection();
    }

    @Override
    public boolean onTap(GeoPoint pt,MapView mMapView) {
        // 点击窗口以外的区域时,当前窗口关闭
        if (popupLinear != null && popupLinear.getVisibility() == View.VISIBLE) {
            LayoutParams lp = (LayoutParams) popupLinear.getLayoutParams();
            Point tapP = new Point();
            projection.toPixels(pt,tapP);
            Point popP = new Point();
            projection.toPixels(lp.point,popP);
            int xMin = popP.x - lp.width / 2 + lp.x;
            int yMin = popP.y - lp.height + lp.y;
            int xMax = popP.x + lp.width / 2 + lp.x;
            int yMax = popP.y + lp.y;
            if (tapP.x < xMin || tapP.y < yMin || tapP.x > xMax
                    || tapP.y > yMax)
                popupLinear.setVisibility(View.GONE);
        }
        return false;
    }

    @Override
    protected boolean onTap(int i) {
        // 点击Marker时,该Marker滑动到地图中央偏下的位置,并显示Popup窗口
        OverlayItem item = getItem(i);
        if (popupView == null) {
            // 如果popupView还没有创建,则构造popupLinear
            if (!createPopupView()){
                return true;
            }
        }
        if (onTapListener == null)
            return true;
        popupLinear.setVisibility(View.VISIBLE);
        onTapListener.onTap(i,popupView);

        popupLinear.measure(0,0);
        int viewWidth = popupLinear.getMeasuredWidth();
        int viewHeight = popupLinear.getMeasuredHeight();

        LayoutParams layoutParams = new LayoutParams(viewWidth,viewHeight,
                item.getPoint(),-60,LayoutParams.BottOM_CENTER);
        layoutParams.mode = LayoutParams.MODE_MAP;

        popupLinear.setLayoutParams(layoutParams);
        Point p = new Point();
        projection.toPixels(item.getPoint(),p);
        p.y = p.y - viewHeight / 2;
        GeoPoint point = projection.fromPixels(p.x,p.y);

        mapView.getController().animateto(point);
        return true;
    }

    private boolean createPopupView() {
        // Todo Auto-generated method stub
        if (layoutId == 0)
            return false;
        popupView = LayoutInflater.from(context).inflate(layoutId,null);
        popupView.setBackgroundResource(R.drawable.popupborder);
        ImageView dialogStyle = new ImageView(context);
        dialogStyle.setimageDrawable(context.getResources().getDrawable(
                R.drawable.iw_tail));
        popupLinear.addView(popupView);
        android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        lp.topMargin = -2;
        lp.leftMargin = 60;
        popupLinear.addView(dialogStyle,lp);
        mapView.addView(popupLinear);
        return true;
    }

    @Override
    public void addItem(List<OverlayItem> items) {
        // Todo Auto-generated method stub
        int startIndex = getAllItem().size();
        for (OverlayItem item : items){
            if (startIndex >= defaultMarkerIds.length)
                startIndex = defaultMarkerIds.length - 1;
            if (useDefaultMarker && item.getMarker() == null){
                item.setMarker(context.getResources().getDrawable(
                        defaultMarkerIds[startIndex++]));
            }
        }
        super.addItem(items);
    }

    @Override
    public void addItem(OverlayItem item) {
        // Todo Auto-generated method stub
        // 重载这两个addItem方法,主要用于设置自己默认的Marker
        int index = getAllItem().size();
        if (index >= defaultMarkerIds.length)
            index = defaultMarkerIds.length - 1;
        if (useDefaultMarker && item.getMarker() == null){
            item.setMarker(context.getResources().getDrawable(
                    defaultMarkerIds[getAllItem().size()]));
        }
        super.addItem(item);
    }

    public void setLayoutId(int layoutId) {
        this.layoutId = layoutId;
    }

    public void setUseDefaultMarker(boolean useDefaultMarker) {
        this.useDefaultMarker = useDefaultMarker;
    }

    public void setonTapListener(OnTapListener onTapListener) {
        this.onTapListener = onTapListener;
    }

    public interface OnTapListener {
        public void onTap(int index,View popupView);
    }
}

二、MainActivity,这是主界面,用来显示地图,创建MyPopupOverlay对象,在使用我写的MyPopupOverlay这个类时,需要遵循以下步骤:

创建MyPopupOverlay对象,构造函数为public MyPopupOverlay(Context context,MapView mMapView),四个参数分别为当前的上下文、通用的Marker(这是itemizedoverlay需要的,当不设置Marker时的默认Marker)以及百度地图对象。
设置自定义的弹出窗口内容的布局文件ID,使用的方法为public void setLayoutId(int layoutId)。
设置是使用自定义的Marker,还是预先写好的带有A-J字样的百度地图原装Marker,使用的方法为public void setUseDefaultMarker(boolean useDefaultMarker),只有当这个值为true且没有调用OverlayItem的setMarker方法为特定点设置Marker时,才使用原装Marker。
创建Marker所在的点,即分别创建一个个OverlayItem,然后调用public void addItem(OverlayItem item)或public void addItem(List<OverlayItem> items)方法来把这些OverlayItem添加到自定义的附加层上去。
为MyPopupOverlay对象添加onTap事件,当Marker被点击时,填充弹出窗口中的内容(也就是第2条中layoutId布局中的内容),设置方法为public void setonTapListener(OnTapListener onTapListener),OnTapListener是定义在MyPopupOverlay中的接口,实现这个接口需要覆写public void onTap(int index,View popupView)方法,其中,index表示被点击的Marker(确切地说是OverlayItem)的索引,popupView是使用layoutId这个布局的View,也就是弹出窗口除了下面的小三角之外的部分。
把这个MyPopupOverlay对象添加到地图上去:mMapView.getoverlays().add(myOverlay);mMapView.refresh();
下面是我的代码(MainActivity):

复制代码 代码如下:

public class MainActivity extends Activity {

    private BMapManager mBMapMan = null;
    private MapView mMapView = null;
    private String keyString = "这里填入申请的KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Todo Auto-generated method stub
        super.onCreate(savedInstanceState);

        mBMapMan = new BMapManager(getApplication());
        mBMapMan.init(keyString,new MKGeneralHandler(MainActivity.this));//MKGeralHandler是一个实现MKGeneralListener接口的类,详见百度的文档

        setContentView(R.layout.activity_main);// activity_main.xml中就是百度地图官方文档提供的LinearLayout下面放一个MapView的布局
        mMapView = (MapView) findViewById(R.id.bmapsView);// 获取地图MapView对象
        mMapView.setBuiltInZoomControls(true);
        final MapController mMapController = mMapView.getController();
        mMapController.setZoom(16);

        GeoPoint p1 = new GeoPoint(39113458,117183652);// 天大正门的坐标
        GeoPoint p2 = new GeoPoint(39117258,117178252);// 天大大活的坐标
        mMapController.animateto(p1);

        //声明MyPopupOverlay对象
        MyPopupOverlay myOverlay = new MyPopupOverlay(
                MainActivity.this,
                getResources().getDrawable(R.drawable.icon_gcoding),
                mMapView);// 这是第1步,创建MyPopupOverlay对象
        myOverlay.setLayoutId(R.layout.popup_content);// 这是第2步,设置弹出窗口的布局文件
        myOverlay.setUseDefaultMarker(true);// 这是第3步,设置是否使用A-J的Marker

        OverlayItem item1 = new OverlayItem(p1,"","");
        OverlayItem item2 = new OverlayItem(p2,"");

        List<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(item1);
        items.add(item2);

        myOverlay.addItem(items);// 这是第4步,向MyPopupOverlay中依次添加OverlayItem对象,或存到链表中一次性添加
//        myOverlay.addItem(item2);

        final List<MapPopupItem> mItems = new ArrayList<MapPopupItem>();// 这是暂时自己造的model对象,存储显示的数据
        MapPopupItem mItem = new MapPopupItem();
        mItem.setTitle("天津大学");
        // ...... 这里依次添加了地址、电话、标签、图片等信息
        mItems.add(mItem);
        mItem = new MapPopupItem();
        mItem.setTitle("天津大学大学生活动中心");
        // ...... 同样添加第二个点的地址、电话、标签、图片信息
        mItems.add(mItem);

        myOverlay.setonTapListener(new OnTapListener() {

            @Override
            public void onTap(int index,View popupView) {// 这是第5步,设置监听器,为popupView填充数据
                // Todo Auto-generated method stub
                MapPopupItem mItem = mItems.get(index);// 这是存储model数据的数组,根据被点击的点的index获取具体对象

                TextView shopName = (TextView) popupView.findViewById(R.id.name);
                // ...... 依次获得视图中的各个控件(地址、电话、标签、图片等)

                shopName.setText(mItem.getTitle());
                // ...... 依次为这些控件赋上值(地址、电话、标签、图片等信息)
            }
        });

        mMapView.getoverlays().add(myOverlay); // 最后一步,添加覆盖物层
        mMapView.refresh();
    }

    @Override
    protected void onDestroy() {
        mMapView.destroy();
        if (mBMapMan != null) {
            mBMapMan.destroy();
            mBMapMan = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        if (mBMapMan != null) {
            mBMapMan.stop();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        if (mBMapMan != null) {
            mBMapMan.start();
        }

        super.onResume();
    }
}

这就是主要的思路和代码了,因为代码文件、资源文件弄得比较多,不大容易贴出来全部能直接运行的代码,而且布局文件里控件太多也不容易理解,就这么写了,如果大家有什么更好的方法,或者有什么好的建议,欢迎讨论和指正。

注:为了说明问题,主类中我简化了很多东西,而且有些图片找起来也挺麻烦,把源代码附在这里供大家参考,运行前需要在MainActivity中修改百度地图的Key。

Android自定义弹出窗口PopupWindow使用技巧

Android自定义弹出窗口PopupWindow使用技巧

PopupWindow是Android上自定义弹出窗口,使用起来很方便。

PopupWindow的构造函数为

复制代码 代码如下:
public PopupWindow(View contentView,int width,int height,boolean focusable)

contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过public void setFocusable(boolean focusable)来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。

如果PopupWindow中有Editor的话,focusable要为true。

下面实现一个简单的PopupWindow

主界面的layout为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/layout_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

 <Button
  android:id="@+id/btn_test_popupwindow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:text="@string/app_name" />

</RelativeLayout>

PopupWindow的layout为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#000000" >

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="80dp"
  android:text="@string/app_name" 
  android:textColor="#ffffffff"
  android:layout_centerInParent="true"
  android:gravity="center"/>

</RelativeLayout>

Activity的代码为:

public class MainActivity extends Activity {

 private Button mButton;
 private PopupWindow mPopupWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  View popupView = getLayoutInflater().inflate(R.layout.layout_popupwindow,null);

  mPopupWindow = new PopupWindow(popupView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true);
  mPopupWindow.setTouchable(true);
  mPopupWindow.setoutsidetouchable(true);
  mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),(Bitmap) null));

  mButton = (Button) findViewById(R.id.btn_test_popupwindow);
  mButton.setonClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    mPopupWindow.showAsDropDown(v);
   }
  });
 }
}

这三行代码

mPopupWindow.setTouchable(true);
mPopupWindow.setoutsidetouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),(Bitmap) null));


的作用是点击空白处的时候PopupWindow会消失。

mPopupWindow.showAsDropDown(v);
这一行代码将PopupWindow以一种向下弹出的动画的形式显示出来

public void showAsDropDown(View anchor,int xoff,int yoff)
这个函数的第一个参数为一个View,我们这里是一个Button,那么PopupWindow会在这个Button下面显示,xoff,yoff为显示位置的偏移。

点击按钮,就会显示出PopupWindow

很多时候我们把PopupWindow用作自定义的菜单,需要一个从底部向上弹出的效果,这就需要为PopupWindow添加动画。

在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件

menu_bottombar_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="250"
  android:fromYDelta="100.0%"
  android:toYDelta="0.0" />

</set>

menu_bottombar_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="250"
  android:fromYDelta="0.0"
  android:toYDelta="100%" />

</set>

在res/value/styles.xml添加一个sytle

 <style name="anim_menu_bottombar">
  <item name="android:windowEnteranimation">@anim/menu_bottombar_in</item>
  <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item>
 </style>

Acivity修改为

public class MainActivity extends Activity {

 private PopupWindow mPopupWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  View popupView = getLayoutInflater().inflate(R.layout.layout_popupwindow,(Bitmap) null));

  mPopupWindow.getContentView().setFocusableInTouchMode(true);
  mPopupWindow.getContentView().setFocusable(true);
  mPopupWindow.getContentView().setonKeyListener(new OnKeyListener() {
   @Override
   public boolean onKey(View v,int keyCode,KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
      && event.getAction() == KeyEvent.ACTION_DOWN) {
     if (mPopupWindow != null && mPopupWindow.isShowing()) {
      mPopupWindow.dismiss();
     }
     return true;
    }
    return false;
   }
  });
 }

 @Override
 public boolean onKeyDown(int keyCode,KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
   if (mPopupWindow != null && !mPopupWindow.isShowing()) {
    mPopupWindow.showAtLocation(findViewById(R.id.layout_main),Gravity.BottOM,0);
   }
   return true;
  }
  return super.onKeyDown(keyCode,event);
 }
}

这样点击菜单键会弹出自定义的PopupWindow,点击空白处或者返回键、菜单键,PopupWindow会消失。

文章如果有不对的地方,希望大家理解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

C# winForm自定义弹出页面效果

C# winForm自定义弹出页面效果

本文实例为大家分享了C# winForm自定义弹出页面效果的具体代码,供大家参考,具体内容如下

在C#的windows窗体应用程序中,添加弹出框效果.最后就是这样的效果.

页面Form2上有2个文本框,textBox1和textBox2.点击任意一个文本框,根据准备好的数据,弹出Form1.其中Form1中的button个数是根据准备好的数据生成的.并且Form1的弹出位置,应该是文本框上面.最后,点击任意一个按钮,会将按钮上的值,显示到对应的文本框中,然后弹出页面关闭.

两个文本框显示的数据效果就是如下,这是textBox2.

这个是textBox1的效果.

主要做法就是,自定义了一个用户控件.由于此代码是在颜料板的基础上改过来的,所以起名不大对.这个用户控件的作用就是根据数据生成button数,并且给button绑定click事件,并且接收参数textBox.在click事件中,获取button的text,然后给之前的textBox的Text赋值button的text,然后textBox初始化.这样就可以在textBox上显示button按钮上的值.而生成的button是放在flowLayoutPanel1控件中,这样他就会自动的一个一个排列.

首先单独新建一个windows窗体应用程序,叫ColorHelper.然后在里面加自定义用户控件ColorSelector.整个项目完成之后的截图是这样的.

然后再ColorSelector中,拖一个flowLayoutpanel控件就是flowLayoutPanel1。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace ColorHelper
{
    /// <summary>
    /// 自定义颜色控件
    /// </summary>
    public partial class ColorSelector : UserControl
    {
        
        //接收传递的参数,文本框和键值对的数据。
        public Control textBox = new Control();
        public Dictionary<string, string> dict = new Dictionary<string, string>();
 
        public Dictionary<string, string> Dict
        {
            get { return dict; }
            set
            {
                dict = value;
            }
        }
 
        //构造函数
        public ColorSelector()
        {
            InitializeComponent();
        }
 
        //选中的Text值
        private string selectText;
 
        public string SelectedText
        {
            get { return selectText; }
            set
            {
                selectText = value;
            }
        }
 
        //选中的Key值
        private string selectKey;
 
        public string SelectedKey
        {
            get { return selectKey; }
            set
            {
                selectKey = value;
            }
        }
 
        /// <summary>
        /// 生成Button
        /// </summary>
        public void LoadButton()
        {
            //情况panel中原来的所有控件
            flowLayoutPanel1.Controls.Clear();
 
            //根据传递的dict键值对生成Button,并设置button的大小,Text,Tag值,以及绑定鼠标点击事件。
            foreach (KeyValuePair<string, string> temp in dict)
            {
                Button button1 = new Button();
                button1.Text = temp.Value;
                button1.Tag = temp.Key;
                button1.Font = new Font("宋体", 22);
                button1.AutoSize = true;
                button1.Width = 120;
                button1.MouseClick += new MouseEventHandler(button_MouseClick);
                flowLayoutPanel1.Controls.Add(button1);
            }
        }
 
  
        /// <summary>
        /// 绑定到button上的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_MouseClick(object sender, MouseEventArgs e)
        {
            //声明一个button,获取到button上的Text和Tag上的值,分别是value和key。
            Button cb = (Button)sender;        
            selectText = cb.Text;
            selectKey = cb.Tag.ToString();
            //将value和key分别给textBox的Text和Tag。
            textBox.Text = selectText;
            textBox.Tag = selectKey;
            //重绘textBox
            textBox.Invalidate();
            textBox.Enabled = true;
            //隐藏控件,并将控件所在的Form关闭
            this.Visible = false;
            this.FindForm().Close();
        }
  
    }
 
}

然后自定义用户控件建立好了。可以再建立一个项目ColorTest,然后建立2个Form。其中Form1放这个控件,Form2放文本框,弹出Form1.

然后再ColorTest中添加引用,把colorHelper引用过来。

而添加到工具箱中,需要在工具箱中右击,选择选择项,然后浏览找到dll或者exe,就可以了。效果就是这样。

然后就能把这个Colorselector的自定义控件拖到Form1上。然后Form1的边框风格FormBorderStyle改为None,并且Form的AutoSize一定要是false。还有Form1的startPosition属性要改为Manual,自定义。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace colorTest
{
    public partial class Form1 : Form
    {
        //接收textBox和dict数据
        public TextBox textBox;
        public Dictionary<string, string> dict;
 
        //有参构造函数,接收Form1传递的值
        public Form1(TextBox textBox, Dictionary<string, string> dict)
        {
            InitializeComponent();
            this.textBox = textBox;
            this.dict = dict;
        }
 
        //加载时将textBox和Dict给自定义控件,并生成button按钮,最后显示button框
        private void Form1_Load(object sender, EventArgs e)
        {
            //设置重画控件
            colorSelector1.textBox = textBox;
     
            //返回到自定义用户控件上
            colorSelector1.Dict = dict;
            colorSelector1.LoadButton();
            //设置弹出事件
            colorSelector1.Visible = true;
        }
 
       
    }
}

最后就是Form2的效果。这个就是这样。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace colorTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void textBox2_MouseClick(object sender, MouseEventArgs e)
        {
            //先去查询数据,获取到返回值为实体数组,转成Dictionary<string,string>
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("1", "汽油");
            dict.Add("2", "柴油");
            dict.Add("3", "煤油");
            dict.Add("4", "4油");
            Form1 form = new Form1(this.textBox2, dict);
            form.Size = new Size(10, 10);
            //MessageBox.Show(textBox2.Location.X + " " + textBox2.Height + " " + textBox2.Location.Y + " " + textBox2.Width + " ");
            //form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y - textBox2.Height);
            //MessageBox.Show(textBox2.Location.X + " " + textBox2.Height+" "+ textBox2.Location.Y+" " +textBox2.Width+ " " );
            //form.Location = new Point(textBox2.Location.X - textBox2.Height, textBox2.Location.Y - textBox2.Width);
            //MessageBox.Show(this.Location.X + " " + this.Location.Y );
            //每行显示5个button按钮
            if (dict.Count >= 5)
            {
                //并且设置5个时,form的size,直接为626,这个是多次测试过得到的结果。
                form.Size = new Size(626, 33 * (dict.Count / 5 + 1));
            }
            else
            {
                form.Size = new Size(125 * dict.Count, 33);
            }
 
            //form的弹出位置,必须要设置Form2的startposition为自定义,否则不管用。
            //在窗体的x的基础上,加上textBox的x坐标,就能控制弹出框的x坐标,而窗体的y坐标加上窗体的y坐标,还要考虑form的height
            form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y - 15 * (dict.Count / 5 + 1));
            
            //弹出form
            form.ShowDialog();
           
        }
 
        /// <summary>
        /// textBox1的鼠标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            //先去查询数据,获取到返回值为实体数组,转成Dictionary<string,string>
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("1", "汽油");
            dict.Add("2", "柴油");
            dict.Add("3", "煤油");
            dict.Add("4", "4油");
            dict.Add("5", "5油");
            dict.Add("7", "6油");
            dict.Add("8", "7油");
            dict.Add("9", "8油");
            dict.Add("10", "9油");
            dict.Add("11", "10油");
            dict.Add("12", "6油");
            dict.Add("13", "7油");
            dict.Add("14", "8油");
            dict.Add("15", "9油");
            dict.Add("16", "10油");
            Form1 form = new Form1(this.textBox1, dict);
            
            if (dict.Count >= 5)
            {
                form.Size = new Size(626, 33 * (dict.Count/5+1));
            }
            else
            {
                form.Size = new Size(125 * dict.Count, 33);
            }
            form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y -15*(dict.Count/5+1));
          
          
            form.ShowDialog();
        }
    }
}

以上就是弹出框的全部代码了。花了我不少时间,并且,学会了算x,y的值。发现所有的显示的location的值,都是相对值,相对于包含他们的容器。textBox是在form2中,他的location是相对于form2,而form2的location的相对于屏幕。

知道了改不来FlowLayoutpanel,每5个换一行,可以控制他的容器Form1,控制他的大小。所以里面的FlowLayoutpanel也不能设置autosize为true,不能自适应,否则他就一行显示了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

CSS3/jQuery自定义弹出窗口

CSS3/jQuery自定义弹出窗口

简单演示一下,精简了演示效果和css样式文件,更利于在项目中的实际应用

引入style.css   index.js

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
	
    <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <Meta http-equiv="description" content="this is my page">
    <Meta http-equiv="content-type" content="text/html; charset=gbk">
    <link href="style.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
  <divid="modal-1">
		<div>
			<h3>
				CSS3/jQuery自定义弹出窗口 多种弹出动画
			</h3>
			<div>
				<ul>
					<li>
						这是一款利用jQuery和CSS3实现的自定义弹出窗口,这可比浏览器默认的弹出窗口漂亮多了.
					</li>
					<li>
						弹出窗口中可以自定义html,十分灵活.
					</li>
					<li>
						另外最重要的一个特点是,它利用了jQuery和CSS3可以实现很多种弹出窗口动画效果,挺酷的.
					</li>
				</ul>
				<button>
					关闭!
				</button>
			</div>
		</div>
  </div>
  <divid="modal-2">
		<div>
			<h3>
				CSS3/jQuery自定义弹出窗口 多种弹出动画
			</h3>
			<div>
				<ul>
					<li>
						这是一款利用jQuery和CSS3实现的自定义弹出窗口,这可比浏览器默认的弹出窗口漂亮多了.
					</li>
					<li>
						弹出窗口中可以自定义html,十分灵活.
					</li>
					<li>
						另外最重要的一个特点是,它利用了jQuery和CSS3可以实现很多种弹出窗口动画效果,挺酷的.
					</li>
				</ul>
				<button>
					关闭!
				</button>
			</div>
		</div>
  </div>
  <divid="modal-3">
		<div>
			<h3>
				CSS3/jQuery自定义弹出窗口 多种弹出动画
			</h3>
			<div>
				<ul>
					<li>
						这是一款利用jQuery和CSS3实现的自定义弹出窗口,这可比浏览器默认的弹出窗口漂亮多了.
					</li>
					<li>
						弹出窗口中可以自定义html,十分灵活.
					</li>
					<li>
						另外最重要的一个特点是,它利用了jQuery和CSS3可以实现很多种弹出窗口动画效果,挺酷的.
					</li>
				</ul>
				<button>
					关闭!
				</button>
			</div>
		</div>
  </div>
  <div>
  <buttondata-modal="modal-1">淡入</button>
  <buttondata-modal="modal-2">从右滑动</button>
  <buttondata-modal="modal-3">报纸</button>
  </div>
  </br>
  </br>
  </br>
  <div>全部效果演示请参考:<a target="_blank" href="http://www.html5tricks.com/demo/jquery-css3-modal-window/index.html">
      http://www.html5tricks.com/demo/jquery-css3-modal-window/index.html</a>  </div>
  </body>
  <script src="index.js" type="text/javascript"></script>
</html>

然后将style.css、index.js和index.html放在同一路径下 用chrome或Firefox打开index.html即可

 

不支持IE浏览器

style.css index.js下载地址:http://download.csdn.net/detail/itmyhome/7433847

参考:http://www.html5tricks.com/category/jquery-plugin

全部效果演示:http://www.html5tricks.com/demo/jquery-css3-modal-window/index.html

 

 

 

 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

今天关于delphi 自定义弹出窗口,减少formdfm文件的数量的讲解已经结束,谢谢您的阅读,如果想了解更多关于android实现百度地图自定义弹出窗口功能、Android自定义弹出窗口PopupWindow使用技巧、C# winForm自定义弹出页面效果、CSS3/jQuery自定义弹出窗口的相关知识,请在本站搜索。

本文标签:

上一篇Delphi Excel导入 的通用程序(delphi导入excel文件)

下一篇Delphi中获取Unix时间戳及注意事项(delphi 获取时间)