如何改变系统自带的颜色选择对话框位置

 1 /*----------------------------------------------
2 COLORS3.C -- Version using Common Dialog Box
3 (c) Charles Petzold, 1998
4 ----------------------------------------------*/
5
6 #include <windows.h>
7 #include <commdlg.h>
8
9 UINT_PTR CALLBACK CCHookProc( HWND hdlg,
10 UINT uiMsg,
11 WPARAM wParam,
12 LPARAM lParam
13 );
14
15 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
16 PSTR szCmdLine, int iCmdShow)
17 {
18 static CHOOSECOLOR cc ;
19 static COLORREF crCustColors[16] ;
20
21 cc.lStructSize = sizeof (CHOOSECOLOR) ;
22 cc.hwndOwner = NULL ;
23 cc.hInstance = NULL ;
24 cc.rgbResult = RGB (0x80, 0x80, 0x80) ;
25 cc.lpCustColors = crCustColors ;
26 cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
27 cc.lCustData = 0 ;
28 cc.lpfnHook = CCHookProc ;
29 cc.lpTemplateName = NULL ;
30
31 return ChooseColor (&cc) ;
32 }
33
34 UINT_PTR CALLBACK CCHookProc( HWND hdlg,
35 UINT uiMsg,
36 WPARAM wParam,
37 LPARAM lParam
38 )
39 {
40 RECT ScreenRect,ClientRect;
41 INT cx,cy;
42 switch(uiMsg)
43 {
44 case WM_INITDIALOG:
45 cx=GetSystemMetrics(SM_CXSCREEN);
46 cy=GetSystemMetrics(SM_CYSCREEN);
47 GetWindowRect(hdlg,&ScreenRect);
48 GetClientRect(hdlg,&ClientRect);
49 MoveWindow(hdlg,(cx-ScreenRect.right)/2,(cy-ScreenRect.bottom)/2,ScreenRect.right,ScreenRect.bottom,TRUE);
50 return TRUE;
51 break;
52 }
53 return FALSE;
54 }

用hook procedure 实现,需要加上CC_ENABLEHOOK flag并且制定回调函数地址CCHookProc。

MSDN

The CCHookProc hook procedure is an application-defined or library-defined callback function that is used with the ChooseColor function. The hook procedure receives messages or notifications intended for the default dialog box procedure of the Color dialog box.

Remarks

When you use the ChooseColor function to create a Color dialog box, you can provide a CCHookProc hook procedure to process messages or notifications intended for the dialog box procedure. To enable the hook procedure, use the CHOOSECOLOR structure that you passed to the dialog creation function. Specify the address of the hook procedure in the lpfnHook member and specify the CC_ENABLEHOOK flag in the Flags member.

The default dialog box procedure processes the WM_INITDIALOG message before passing it to the hook procedure. For all other messages, the hook procedure receives the message first. Then, the return value of the hook procedure determines whether the default dialog procedure processes the message or ignores it.

If the hook procedure processes the WM_CTLCOLORDLGmessage, it must return a valid brush handle to painting the background of the dialog box. In general, if the hook procedure processes any WM_CTLCOLOR* message, it must return a valid brush handle to painting the background of the specified control.

Do not call theEndDialogfunction from the hook procedure. Instead, the hook procedure can call thePostMessagefunction to post aWM_COMMANDmessage with the IDABORT value to the dialog box procedure. Posting IDABORT closes the dialog box and causes the dialog box function to return FALSE. If you need to know why the hook procedure closed the dialog box, you must provide your own communication mechanism between the hook procedure and your application.

You can subclass the standard controls of a common dialog box. However, the dialog box procedure may also subclass the controls. Because of this, you should subclass controls when your hook procedure processes the WM_INITDIALOG message. This ensures that your subclass procedure receives the control-specific messages before the subclass procedure set by the dialog box procedure.

转载于:https://www.cnblogs.com/yiruirui/archive/2011/12/14/2287699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值