在vb6里面,我們一般通過(guò)以下代碼來(lái)創(chuàng)建不規(guī)則窗體。 private declare function setwindowrgn lib "user32" (byval hwnd as long, _ byval hrgn as long, byval bredraw as boolean) as long private declare function createellipticrgn lib "gdi32" (byval x1 as long, _ byval y1 as long, byval x2 as long, byval y2 as long) as long private declare function deleteobject lib "gdi32" (byval hobject as long) as long
private sub form_activate() dim hndrgn as long hndrgn = createellipticrgn(0, 0, 175, 175) call setwindowrgn(me.hwnd, hndrgn, true) call deleteobject(hndrgn) end sub 首先用win32 api createellipticrgn創(chuàng)建一個(gè)圓形的區(qū)域,然后設(shè)置form的區(qū)域?yàn)橛脩糇远x的region,這樣我們就可以得到下面一個(gè)圓形的窗體
vb.net里面如何實(shí)現(xiàn)以上效果: vb.net是一中跨平臺(tái)的語(yǔ)言,更好的利用了面向?qū)ο髾C(jī)制。它的面向?qū)ο竽芰U(kuò)展了語(yǔ)言本身的通路:一切都是對(duì)象。這意味著比在以前的vb版本里,你獲得了更多的內(nèi)在功能,你將很少被迫使用 windows api。因此在vb.net里面我們也只好放棄vb6里面利用api的觀念,用vb.net強(qiáng)大的對(duì)象機(jī)制來(lái)闡述以上話題。 在vb.net里面,form有一個(gè)reigin屬性,我們通過(guò)創(chuàng)建自定義的reigin,然后指定form的reigin,就可以得到不規(guī)則的窗體。而且vb.net里面的reigin對(duì)象功能強(qiáng)大,遠(yuǎn)超過(guò)了之前vb的限制,因此我們可以作出很多漂亮的界面。
在form的load事件加入以下代碼: dim text_path as graphicspath dim text_region as region me.backcolor = color.red me.width = 600 ' create the text path. text_path = new graphicspath( drawing.drawing2d.fillmode.alternate) text_path.addstring("csdn", new fontfamily("times new roman"), fontstyle.bold, 200,new point(10, 10), stringformat.genericdefault) ' create a region from the path. text_region = new region(text_path) ' constrain the form to the region. me.region = text_region
運(yùn)行將將得到如下形狀的窗體,記住按shift+f5 中止程序。
2.橢圓形狀的窗體: 同樣加入以下代碼,得到如下窗體
me.width = 300 me.height = 220 me.backcolor = color.royalblue dim m_path as graphicspath m_path = new graphicspath(fillmode.winding) m_path.addellipse(1, 1, 200, 200) dim m_region as new region(m_path) me.region = m_region