1,怎樣得到下拉數(shù)據(jù)窗顯示列的值
下面的代碼使用了Describe()的Evaluate()函數(shù),其功能是得到state_code列的顯示值,該列使用了下拉數(shù)據(jù)窗口(DropDownDataWindow)編輯風(fēng)格。這段代碼應(yīng)該在發(fā)生過ItemChanged事件后執(zhí)行,這樣用戶所選值已經(jīng)被放入了相應(yīng)的緩沖區(qū)。為了安全,下面的代碼放置在名稱為getdisplayvalue的數(shù)據(jù)窗口用戶自定義事件中:
string rownumber, displayvalue
rownumber = String(dw_1.GetRow())
displayvalue = dw_1.Describe( "Evaluate('LookUpDisplay(state_code) ', " &
+ rownumber + ")")
dw_1.Describe("Evaluate('LookUpDisplay(columnname)',"+String(this.GetRow())+")"))
2,在PB下實現(xiàn)多邊形的窗口
看了如煙網(wǎng)友《如何在PB下實現(xiàn)圓形窗口》的文章,用了一下不錯,希望也能寫一些東西對大家有所幫助!
如下:
首先在窗口定義下列局部外部函數(shù)(Local External Functions...):
Function ulong CreatePolygonRgn (ref tagPOINT lppt[], int cPoints, int fnPolyFillMode ) Library "gdi32.dll"
FUNCTION ulong SetWindowRgn(ulong hWnd,ulong hRgn,boolean bRedraw) LIBRARY "user32.dll"
在窗口上定義
Structure
tagpoint
{ long x
long y}
在窗口的open事件中加上:
long hrgn
long lres
tagPOINT l_pointapi[]
l_pointapi[1].x = 173
l_pointapi[1].y = 112
l_pointapi[2].x = 412
l_pointapi[2].y = 66
l_pointapi[3].x = 478
l_pointapi[3].y = 240
l_pointapi[4].x = 338
l_pointapi[4].y = 340 //多邊形各個頂點坐標值
hrgn = CreatePolygonRgn(l_pointapi[],4,1)
//其中第 1 個參數(shù)為多邊形各個頂點坐標值的數(shù)組
//其中第 2 個參數(shù)為多邊形邊數(shù),可修改,配合數(shù)組值
//其中第 3 個參數(shù)為填充模式 ALTERNATE /WINDING
lres=setwindowRgn(handle(this),hrgn,true)
設(shè)置了填充模式,但看不出來,不知到底有什么用,望大蝦們告知
3,PB的狀態(tài)條很難看,不能增加更多的分割條,字體也很難看,有什么辦法可以改善?
PB狀態(tài)欄并不是一個控件,它是一個屬于MDI主窗口的子窗口(child window)
在6.0中其類名為"FNHELP60",由此可查到狀態(tài)欄的句柄,下面的程序段將句柄取出并保存在llhStatusBar變量中
// Win32 API
function long FindWindowA( String lpClassName, String lpWindowName ) Library "user32"
function long FindWindowExA( long hParent, long hChildAfter, String lpszClass, String lpszWindow ) Library "user32"
//Open event script in the frameWindow
long llNULL, llhStatusBar
string lsClassName, lsWindowName
Environment lEnv
// Check the FrameWindow is a valid window, and get current environment settings
IF IsValid( this ) AND (GetEnvironment(lEnv) = 1) THEN
// Check that this an MDI frame window with a standard statusbar
IF (this.WindowType = MDIHelp!) THEN
// Set classname of statusbar based on PB version
CHOOSE CASE lEnv.PBMajorRevision
CASE 5
lsClassName = "FNHELP050"
CASE 6
lsClassName = "FNHELP60"
// Add code here for PB4 and PB7
END CHOOSE
// Get handle to statusbar
SetNull(lsWindowName)
SetNull(llNULL)
llhStatusBar = FindWindowEXA( Handle(this), llNULL, lsClassName, lsWindowName )
END IF
END IF
但是我想引用這個子窗口的名字,
以便把它分成多列!有辦法嗎?
得到微幫助的句柄以后,還有什么是不能做的呢?你可以用TextOut在微幫助條上輸出字符,當(dāng)然在此之前,你還可以用CreateFont設(shè)定字體, 用SetBkColor和 SetTextColor設(shè)定顏色.如果你想加入分割線可以用MoveToEx加LineTo,不過這樣一來就比較麻煩了.^_^
//Win 32 API
Function ulong CreateFont (int nHeight, int nWidth, int nEscapement, int nOrientation, int nWeight, ulong fbItalic, ulong fbUnderline, ulong fbStrikeOut, ulong fbCharSet, ulong fbOutputPrecision, ulong fbClipPrecision, ulong fbQuality, ulong fbPitchAndFamily, ref string lpszFace) ALIAS FOR "CreateFontA" Library "GDI32.DLL"
Function ulong GetDC (ulong hWnd) Library "USER32.DLL"
Function ulong SetBkColor (ulong hDC, ulong crColor) Library "GDI32.DLL"
Function ulong SetTextColor (ulong hDC, ulong crColor) Library "GDI32.DLL"
Function boolean TextOut (ulong hdcr, integer stx, integer sty, ref string lpString, long nCount) Library "GDI32.DLL" Alias for "TextOutA"
Function boolean LineTo (ulong hDC, integer wx, integer wy) Library "GDI32.DLL"
Function boolean MoveToEx (ulong hDC, int x, int y, ref tagPoint lppt ) Library "GDI32.DLL"
Function ulong SelectObject (ulong hDC, ulong hWnd) Library "GDI32.dll"
//Open event script
string ls_facename = "宋體"
ilh_statusbarhdc = GetDC(llhStatusBar)
ll_Font = CreateFont(16, 0, 0, 0, 400, 0, 0, 0, 255, 0, 0, 0, 18, ls_facename)
SelectObject (ilh_statusbarhdc, ll_font)
SetBkColor (ilh_statusbarhdc, rgb( 0,0,165 ))
SetTextColor (ilh_statusbarhdc, rgb( 0,165,0 ))
自定義一個事件ue_paint,選擇pbm_paint,在其中添加字符串輸出:
string ls_facename = "呵呵!一切就緒"
TextOut ( ilh_statusbarhdc, 100, 3, ls_facename, len( ls_facename ))
為PB的微幫助條加分割線:
首先tagPoint 是一個結(jié)構(gòu),你需要先在PB中聲明之:
type str_pos from structure
long x
long y
end type
//win32 API
Function ulong GetDC(ulong hwnd) library "user32.dll"
FUNCTION boolean MoveToEx(ulong hwnd,long wx, long wy,ref str_pos prepos2) LIBRARY "Gdi32.dll"
FUNCTION boolean LineTo(ulong hwnd,long wx, long wy) LIBRARY "Gdi32.dll"
Function ulong CreatePen (uint nPenStyle, uint nWidth, ulong crColor) Library "GDI32.DLL"
Function ulong SelectObject (ulong hDC, ulong hWnd) Library "GDI32.dll"
//Open event script
// llhStatusBar是微幫助條窗口句柄
ilh_statusbarhdc = GetDC(llhStatusBar) //如何取微幫助條窗口句柄,見595貼
//用CreatePen建立畫線的線形,這里為紅線,1個像素寬
SelectObject (ilh_statusbarhdc, CreatePen ( 0, 1, rgb( 255,0, 0 ) ) )
//在ue_paint事件中畫分割線,線的起點在98,3,終點在98,18(單位: 像素)
str_pos strvar
MoveToEx(ilh_statusbarhdc, 98, 3, strvar)
Lineto( ilh_statusbarhdc, 98, 18 )
4,以動畫效果打開你的Window
下面的函數(shù)只能用于Windows 98/2000平臺
可以為您的程序增加很Cool的Splash畫面(或者您需要的Window)
在您的程序中定義外部函數(shù)
Function boolean AnimateWindow( & long lhWnd, long lTm, long lFlags ) library 'user32'
注:以上第一個參數(shù)是您的Windows的句柄,使用handle(w_1)即可獲得,第二個參數(shù)是需要的動畫效果。第三個參數(shù)是動畫類型
再將下面代碼放入到您的窗口open事件中
// Animate the window from left to right
Constant Long AW_HOR_POSITIVE = 1
// Animate the window from right to left
Constant Long AW_HOR_NEGATIVE = 2
// Animate the window from top to bottom
Constant Long AW_VER_POSITIVE = 4
// Animate the window from bottom to
Constant Long AW_VER_NEGATIVE = 8
// Makes the window appear to collapse inward
Constant Long AW_CENTER = 16
// Hides the window
Constant Long AW_HIDE = 65536
// Activates the window
Constant Long AW_ACTIVATE = 131072
// Uses slide animation
Constant Long AW_SLIDE = 262144
// Uses a fade effect
Constant Long AW_BLEND = 524288
AnimateWindow( Handle( this ), 500, AW_VER_NEGATIVE )可以下載更完整的PB例程和演示效果,由ilike提供
hWnd:指定產(chǎn)生動畫的窗口的句柄。
dwTime:指明動畫持續(xù)的時間(以微秒計),完成一個動畫的標準時間為200微秒。
dwFags:指定動畫類型。這個參數(shù)可以是一個或多個下列標志的組合。標志描述:
AW_SLIDE:使用滑動類型。缺省則為滾動動畫類型。當(dāng)使用AW_CENTER標志時,這個標志就被忽略。
AW_ACTIVATE:激活窗口。在使用了AW_HIDE標志后不能使用這個標志。
AW_BLEND:實現(xiàn)淡出效果。只有當(dāng)hWnd為頂層窗口的時候才可以使用此標志。
AW_HIDE:隱藏窗口,缺省則顯示窗口。 65536
AW_CENTER:若使用了AW_HIDE標志,則使窗口向內(nèi)重疊,即收縮窗口;若未使用AW_HIDE標志,則使窗口向外擴展,即展開窗口。
AW_HOR_POSITIVE:自左向右顯示窗口。該標志可以在滾動動畫和滑動動畫中使用。當(dāng)使用AW_CENTER標志時,該標志將被忽略。
AW_VER_POSITIVE:自頂向下顯示窗口。該標志可以在滾動動畫和滑動動畫中使用。當(dāng)使用AW_CENTER標志時,該標志將被忽略。
AW_VER_NEGATIVE:自下向上顯示窗口。該標志可以在滾動動畫和滑動動畫中使用。當(dāng)使用AW_CENTER標志時,該標志將被忽略。
返回值:如果函數(shù)成功,返回值為非零;如果函數(shù)失敗,返回值為零。在下列情況下函數(shù)將失敗:
窗口使用了窗口邊界;窗口已經(jīng)可見仍要顯示窗口;窗口已經(jīng)隱藏仍要隱藏窗口。
5,判斷一個應(yīng)用是否正在運行
定義一個全局外部函數(shù):
FUNCTION ulong FindWindowA(ulong classname,string windowname) LIBRARY "user32.dll"
Application的Open事件中的代碼:
boolean rtn
ulong l_handle, lu_class
string ls_name
setnull(lu_class)
ls_name = "API Name" // 將API Name換了你的主窗口的標題title
l_handle = FindWindowA(lu_class, ls_name)
if l_handle > 0 then
MessageBox("應(yīng)用程序多次運行", This.AppName + "已經(jīng)運行,你不能多次啟動!")
Halt Close
else
open(w_main)
end if
6,打開任意注冊后類型的文件(用API)
ShellExecute
VB聲明
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
說明
查找與指定文件關(guān)聯(lián)在一起的程序的文件名。關(guān)聯(lián)的方式要么是運行程序,要么是打印文件。可用Windows注冊表編輯器將特定的文件類型同應(yīng)用程序關(guān)聯(lián)起來。例如,擴展名為.TXT的文本文件通常與Windows記事本(NOTEPAD.EXE)關(guān)聯(lián)到一起。如在文件管理器中雙擊含.TXT擴展名的一個文件,就會自行啟動記事本程序,并在其中載入文本文件;或者將指定的文件打印出來
返回值
Long,大于32表示成功。請參考對FindExecutable函數(shù)的說明,了解具體的錯誤代碼列表
參數(shù)表
參數(shù) 類型及說明
hwnd Long,指定一個窗口的句柄,有些時候,Windows程序有必要在創(chuàng)建自己的主窗口前顯示一個消息框。如果發(fā)生這種情況,由這個參數(shù)指定的窗口就會作為消息框的父窗口使用。在VB環(huán)境中,通常將活動窗體的窗口句柄作為這個參數(shù)使用
lpOperation String,指定字串“Open”來打開lpFlie文檔;或指定“Print”來打印它。也可設(shè)為vbNullString,表示默認為“Open”
lpFile String,想用關(guān)聯(lián)的程序打印或打開的一個程序名或文件名
lpParameters String,如lpFile是一個可執(zhí)行文件,則這個字串包含了傳遞給執(zhí)行程序的參數(shù)。如lpFile引用的是一個文檔文件,或者不需要使用參數(shù),則設(shè)為vbNullString
lpDirectory String,想使用的默認路徑完整路徑
nShowCmd Long,定義了如何顯示啟動程序的常數(shù)值。參考ShowWindow函數(shù)的nCmdShow參數(shù)
用Run函數(shù)
Run("???.??? !!!.!!!")?為類型文件打開的程序,!為要打開的文件名,例如:Run(Winrar.Exe Abc.Zip)用Winrar打開Abc.Zip文件
用ShellExecute
聲明:
FUNCTION ulong ShellExecute(ulong hwnd,ref string lpOperation,ref string lpFile,ref string lpParameters,ref string lpDirectory,ulong nShowCmd) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteA"
調(diào)用:
ShellExecute(Handle(Parent),str_null,pathname,str_null,str_null,1)
注:
str_null:字符型變量,不需賦值,聲明即可
pathname:要打開的文件名(包括完整路徑)
7,實現(xiàn)鼠標左鍵按住窗體就可以移動窗體
窗體上面有Picture的處理方法
把 Picture 設(shè)為 Enabled = False
仍在窗口的 MouseDown 中寫代碼:
Send ( Handle ( This ), 274, 61458, 0 )
8,PB制作進度條
PowerBuilder 是大家公認的、 最佳的數(shù)據(jù)庫前端開發(fā)工具之一,但一直以來,PB在用戶界面的處理上十分單調(diào),最明顯的例子是,所有的窗口控件都不支持透明色即transparent 。PowerBuilder 7以前的版本沒有提供進度條,造成大多數(shù)程序員總是自己在程序中利用兩個矩形控件(Rectangle)進行編程產(chǎn)生進度效果。但是這樣一來,進度條中的進度百分比就沒有了,如果再加上一個靜態(tài)文本作為百分比顯示,由于statictext控件沒有透明色,進度條的百分比顯示無法與進度條進行重疊,只得在進度條外的其它位置放置一statictext控件用以顯示進度情況,這樣即浪費了空間,又不美觀。
在網(wǎng)上也曾看到過解決的方法,主要是利用了數(shù)據(jù)窗口中控件可以透明處理的特征,但總感覺美中不足,數(shù)據(jù)窗口是一種重型控件,要占用大量的系統(tǒng)資源,能不能用其他更簡單的方法解決上述問題呢?
上述問題的關(guān)鍵在于文本的輸出背景要透明,我們知道:Windows系統(tǒng)本身提供了大量的底層API功能函數(shù)供上層的應(yīng)用程序調(diào)用,文本的輸出該是最基本的吧,所以Windows一定能夠解決文本背景透明輸出的問題。仔細查找API幫助,果然找到相關(guān)的函數(shù),于是問題迎刃而解。
下面列出主要的聲明和對象實現(xiàn)的主要程序段:
新建一可視定制用戶對象uo_progressbar,在其中放置兩個矩形控件(Rectangle),分別命名為:r_back 和 r_front。定義實例變量如下:
ulong iul_ihDC //保存可視定制用戶對象的DC句柄
real ir_step //根據(jù)對象寬度計算出來的步進值
integer ii_x, ii_y
//根據(jù)對象寬度和高度計算出來的文本輸出位置
聲明Local external functions如下:
Function ulong GetDC (ulong hWnd) Library "USER32.DLL"
Function boolean TextOut (ulong hdcr, integer stx,
integer sty, ref string lpString, long nCount)
Library "GDI32.DLL" Alias for "TextOutA"
Function int SetBkMode (ulong hdcr, integer mode)
Library "GDI32.DLL"
在對象的constructor事件中輸入以下代碼:
integer li_width, li_height
//根據(jù)對象變框計算寬高
CHOOSE CASE this.BorderStyle
CASE StyleLowered!,StyleRaised!
li_width = this.width - PixelsToUnits(4, XPixelsToUnits!)
li_height = this.height - PixelsToUnits(4, YPixelsToUnits!)
CASE StyleBox!,StyleShadowBox!
li_width = this.width - PixelsToUnits(2, XPixelsToUnits!)
li_height = this.height - PixelsToUnits(2, XPixelsToUnits!)
CASE Else
li_width = this.width
li_height = this.height
END CHOOSE
r_back.width = li_width
r_front.height = li_height
r_front.width = 0
r_back.height = li_height
//計算百分比文本輸出的位置使其在水平和垂直方向上居中
ii_x = UnitsToPixels(li_width, XUnitsToPixels!)/2 - 16
ii_y = ( UnitsToPixels(li_height, YUnitsToPixels!) - 16 )/2
ir_step = li_width/100 //計算步進值
iul_ihDC = GetDC( handle(this) )
//獲得對象DC句柄,用于API調(diào)用
SetBkMode (iul_ihdc, 1 ) //設(shè)置文本輸出背景色透明
return 0
為對象寫一個函數(shù):of_setposition,
//功能:根據(jù)參數(shù)改變進度顯示
//參數(shù):integer ai_percent
//返回:無
string ls_msg
integer li_x
If ai_percent <= 100 and ai_percent < 0 then SetRedraw(FALSE) li_x="ai_percent*ir_step" r_back.x="li_x" r_front.width="li_x" ls_msg="string(" ai_percent )+"%" SetRedraw(True) TextOut (iul_ihdc, ii_x, ii_y, ls_msg, len( ls_msg ) ) End If
當(dāng)然你還可以編寫函數(shù)改變進度條中的前景色背景色和文本顏色,這里不再贅述。
9,Datawindow中每頁打印固定行
(深圳:獨孤求敗 2003-05-19)
第一步:
增加一個計算列,此計算列必須放在Detail段,Expression中輸入:ceiling(getrow()/20),這里20還可以用全局函數(shù)取代,這樣可以允許用戶任意設(shè)置每頁打印多少行。
第二步:
定義分組,選擇菜單Rows->Create Group...按計算列字段分組,并一定將check box-->New Page On Group Break選中。
第三步:
將此計算列設(shè)為不可視。另外,如果需要最后一頁不足補空行。也很簡單,如下:
long ll_pagerow = 6 //每頁打印行數(shù)
long ll_count, ll_row
ll_count = dw_report.retrieve(...) //取得現(xiàn)有報表的總行數(shù)
ll_count = ll_pagerow - mod(ll_count, ll_pagerow)
If ll_count < ll_pagerow Then
for ll_row = 1 to ll_count
dw_print.insertrow(0) //補足空行
next
end If
10,sql語句將行轉(zhuǎn)換成列
declare @sql varchar(8000)
set @sql = 'select 物料代碼'
select @sql = @sql + ',sum(case 地區(qū) when '''+地區(qū)+''' then 數(shù)量 end) ['+地區(qū)+']'
+ ',sum(case 地區(qū) when '''+地區(qū)+''' then 比率 end) [比率]'
from (select distinct 地區(qū) from 表) as a
select @sql = @sql+' from 表 group by 物料代碼'
exec(@sql)
簡化如下:
select (select 物料代碼 ,
sum(case 地區(qū) when '地區(qū)' then 數(shù)量 end)[地區(qū)],
sum(case 地區(qū) when '地區(qū)' then 比率 end)[比率]
from (select distinct 地區(qū) from 表) as a
)
from 表 group by 物料代碼